Using post method to change data’s status

Kaycheng
2 min readAug 24, 2021

Creating hide/publish buttons in Rails

The process is:

1. Creating job model and adding attributes

2. Setting routes

3. Setting controller

4. Setting view

5. Organizing our code

Step1. Creating job model and adding attributes

We have a model which is named of job, and there is an attribute which is called is_hidden, and it’s a boolean’s data type.

2. Setting routes

In this case, we want to create a hide/publish button to change the status that the jobs is hidden or is published. So we set routes and use post method here.

3. Setting controller

When we create a method in route, it means we will have a same name’s action in controller file too. So the next step, let’s set jobs_controller.rb.

4. Setting view

Fourth step is setting view to let user can click the button to change the date’s value. And we want to use icon to show the status.

Then the index.html.erb page will looks like this:

By the way, we add font awesome here to show the icon.

5. Organize code

5.1 Using helper to organize code

It's not good to write the if/else logic in view page. So we create jobs_helper.rb to put on our logic.

Then change our index.html.erb page by using the helper methods.

5.2 Creating publish! and hide! method to organize code

We create publish! and hide! method on job model to let our controller looks more fancy!

--

--