i'm rails beginner , created def set_star in controller, change attribute onclick.
class ebmscontroller < applicationcontroller def index @ebms = ebm.all end def destroy @ebm = ebm.find(params[:id]) @ebm.destroy redirect_to categories_path end def create @ebm = ebm.new(params[:ebm]) @ebm.save redirect_to categories_path end def set_star @ebm = ebm.find(params[:id]) @ebm.write_attribute(:star, '1') redirect_to categories_path end end the button looks this:
<% ebm in @ebms %> <tr> <td><%= ebm.number %></td> <td><%= ebm.text %></td> <td><%= ebm.content %></td> <td><%= ebm.star %></td> <td><%= link_to 'star', ebm, :method => :set_star, data: { confirm: 'are sure?' } %></td> </tr> <% end %> but somehow wont work, when click button routing error:
no route matches [post] "/ebms/1" in routes, defined:
resources :ebms so, dont know did wrong! thanks.
in routes.rb add line
put 'ebms/:id/star', to: 'ebms#set_star', as: 'set_star' then change link_to follows
<%= link_to 'star', set_star_path(ebm), method: :put, confirm: 'are sure?' %> as mattherick pointed out, change controller @ebm.update_attribute(:star, '1')
Comments
Post a Comment