as first rails app, i'm trying put simple blog application users can vote on posts. generated blogpost
scaffold integer column (entitled "upvote") keeping track of vote count.
in blogpost
model, created function:
def self.voteup blogpost.upvote += 1 end
on blogpost index view, i'd create link like:
link_to "vote up" self.voteup
but doesn't seem work. possible create link method? if not, can point me in right direction accomplish this?
i assume need increment upvote
column in blogspots table. redirection method controllers job , can give links controller methods only. can create method in blogposts controller this:
def upvote_blog blogpost = blogpost.find(params[:id]) blogpost.upvote += 1 blogpost.save redirect_to blogpost_path end
in index page,
<% @blogposts.each |blogpost| %> ... <%= link_to "vote up", :action => upvote_blog, :id => blogpost.id %> ... <% end %>
Comments
Post a Comment