ruby - Rails 4 use link_to or button_to to run method? -
i implementing simple voting system , clicking on button +1 added. example, if question has 5 votes, increase. have written method already, not sure how execute clicking on link_to. need reconfigure routes?
questions_controller.rb
def self.ping @question = question.find(params[:id]) @question.increment!(:amplify) render_to |format| if @question.save format.html { redirect_to @question } end end end
routes.rb
resources :questions post '/ping' => 'questions#ping', as: 'ping'
your routes need support id
:
post '/ping/:id' => 'questions#ping', as: 'ping'
or better yet, if want scoped within question:
resources :questions post '/ping' => 'questions#ping', as: ping end
however, don't think want class method ping
in questions_controller. think want instance method:
def ping @question = question.find(params[:id]) @question.increment!(:amplify) if @question.save render_to |format| format.html { redirect_to @question } end end end
if doesn't work, errors see in logs?
Comments
Post a Comment