Rails - How to use link_to to change a boolean in the database?
methods, routes, ruby-on-rails
Solution
Try this:
def submitreview
@project = Project.find(params[:id])
@project.update_attributes(submitreview: true)
redirect_to projects_path(submitreview: false)
end
Also, run `rake routes` to make sure that the route is a `PUT` and not a `GET`.
You can simplify your `link_to` as well, lets say the route is `submitreview_projects`:
<%= link_to "Submit Proposal", submitreview_projects_path(@project), method: :put %>
One final note: you might want to change all your `submitreview` to `submit_review` for better readability.
Problem
In my application, I am trying to create a link that will allow users to submit project proposals. When users are ready to submit it for review, users will click the link, which causes the boolean "submitreview" to be marked as true. I followed this post as a guide: Rails 3 how do I use link_to to change the value of a boolean in the db?. Right now, the boolean does NOT change from false to true and it also gets redirected to the following URL: `http://localhost:3000/projects?submitreview=false` Any help would be appreciated, thanks! * UPDATE * I have updated the code below as per suggestions, and have gotten it to update the boolean successfully after I click the link. It should be noted that this "form" that contains the link is also rendered on the new project forms. So my problem now is that when I click to new projects `<%= link_to 'Start your project', new_project_path %>`, I get the following new error below. When I delete the link_to submitreview in the form, no error comes up and the new_project renders great. My guess is that the new_project has not created an ID for the project yet which there's a call in `def submitreview`. Can this be resolved? ``` Routing Error No route matches {:action=>"submitreview", :controller=>"projects", :id=># ... } ``` projects/_form.html.erb ``` <%= link_to "Submit Proposal", submitreview_project_path(@project), method: :put, class: "btn btn-primary" %> ``` projects_controller.rb ``` def submitreview @project = Project.find(params[:id]) @project.update_attributes(submitreview: true) redirect_to project_path(@project) end ``` routes.rb ``` resources :projects do member do get :submitreview put :submitreview end end ``` rake routes ``` submitreview_project GET /projects/:id/submitreview(.:format) projects#submitreview PUT /projects/:id/submitreview(.:format) projects#submitreview ```