rubygems - How do you use a Rails 3 gem method to update a database model? -
i using thumb_up gem ruby on rails. https://github.com/brady8/thumbs_up
i want users able vote on posts. however, unable figure out how can allow user click button next each post , add vote database.
i can happen in rails console through doing following:
u=user.first m=micropost.first u.vote_for(m) however, how can happen when button clicked in view. assuming have use ajax, how know url need post to make action occur?
any appreciated.
update:
thanks help! still having problem code below.
here routes.rb
resources :microposts post :vote, :on => :member end view:
<%= link_to('vote post!', vote_micropost_path(@micropost), :method => :post) %> controller:
def vote @micropost = micropost.find(params[:id]) current_user.vote_for @micropost # assumes you'll call via ajax. # if ajax call doesn't return "ok", know went wrong render :text => 'ok', :layout => false end however, i'm still getting error: no route matches {:controller=>"microposts", :id=>#, :action=>"vote"}
would know why routes aren't matching correctly?
i assuming rails 3. rails 2's routes little different.
first need define route in config/routes.rb file. many ways. if have route microposts, add "vote" action:
resources :microposts post :vote, :on => :member end (for clarity, "post" above refers http post method , has nothing micropost class.) if use route, need create "vote" method in microposts controller catch it. like
def vote @post = micropost.find(params[:id]) current_user.vote_for @post # assumes you'll call via ajax. # if ajax call doesn't return "ok", know went wrong render :text => 'ok', :layout => false end then in view's ajax post call (assuming example route gave), url with:
vote_micropost_path(@micropost) it /microposts/56/vote
Comments
Post a Comment