Reputation: 13
I have written a method to count the vote for particular section and to display it in view file for section. But when I call this method count is not incremented. Should i make any changes in following method
code for voteme method in controller is as:
def voteme(num)
@section = Section.find(params[:id])
@section.vote += num
end
and code in view file is
<%= link_to "up", :url => voteme_section_path(1), :html => { :method => :post }%>
also can anyone suggest me the code to display updated count value.I have a vote field in section model.
Upvotes: 1
Views: 267
Reputation: 5734
In your view file
<%= link_to "up", voteme_section_path(1), :method => :post %>
But I have a question, you are voting up against a section. So why you are passing 1 to it. you should pass the section object if you would have stored it in @section variable. So its better you can modify the link as
<%= link_to "up", voteme_section_path(@section), :method => :post %>
In your route file I guess you need to do some thing like this
resources :sections do
member do
post 'voteme'
end
end
And in the sections controller, 'voteme' action
def voteme
@section = Section.find_by_id(params[:id])
unless @section.blank?
@section.update_column('vote', @section.vote + 1)
else
flash[:notice] = 'Sorry something goes wrong'
end
redirect_to your-path-that-you want-to-show.
end
Upvotes: 1
Reputation: 47482
def voteme(num)
@section = Section.find(params[:id])
@section.update_attribute('vote', @section.vote+1)
end
Upvotes: 0
Reputation: 1496
Try adding @section.save after you change the value.
This logic should also be in the model, rather than in the controller. The controller should only pass things back and forth between the model and the view.
Upvotes: 0