2

I am trying to add voting to my site. I have improvements that are created and displayed inside of the projects show page. I'm trying to allow users to vote on improvements but I am getting an error that I think is related to how I'm linking the like button.

in my routes.rb file:

  resources :projects do

    resources :improvements do      
        member do
            put "like" => "improvements#upvote"
            put "unlike" => "improvements#downvote"
        end

    end
   end

In my view:

<%= link_to like_improvement_path(improvement), class: "like", method: :put do %>

Rails recommended me to write:

<%= link_to project_like_improvement_path(improvement), class: "like", method: :put do %>

But this doesn't work. So I tried doing this in my routes.rb:

  resources :projects do

    resources :improvements

  end

   resources :improvements do       
        member do
            put "like" => "improvements#upvote"
            put "unlike" => "improvements#downvote"
        end
   end

Using the original link_to, the voting works, but clicking on the vote button takes me to the improvements show page. I want to stay on the projects page.

2 Answers2

0

If:

<%= link_to like_improvement_path(improvement), class: "like", method: :put do %>

Works then in the improvements controller's (upvote?) action simply do a:

redirect_to projects_path At the end of whatever else you do model wise. Change "projects_path" to the correct route for the page.

ekr990011
  • 724
  • 2
  • 7
  • 23
0

I had the same problem. The solution with your original routes.rb:

<%= link_to like_project_improvement_path(improvement.project, improvement), class: "like", method: :put do %>

You can write a redirect_to inside the upvote and downvote methods in the improvements controller.

nscherzer
  • 53
  • 5