ruby - Updating a rails attribute through a model association -
i have 2 models shop , user. shop has_many users , user has_one shop. i'm trying create button on shop/show allows user choose shop. button should send parameter shop_id users_controller#update_shop changes user.shop_id id of shop on page.
the current method in users_controller#update_shop following:
def update_shop @user = user.find(params[:id]) @user.update_attributes(shop_id: params[:shop][:id]) flash[:success] = "added shop!" end and button on show/shop this:
<%= form_for user.update_shop, remote: true |f| %> <%= f.hidden_field :shop_id, value: @shop.id %> <%= f.submit 'add shop', :class => 'button blue-button' %> <% end %> i'm getting error nomethoderror in shopscontroller#show undefined methodupdate_shop' #`. method defined in users controller though.
i understand there more efficient way update user.shop_id through association, tips on doing or getting work appreciated.
i think it's better have link post method in place of form:
<%= link_to 'add shop', update_shop_url(id: @user.id, shop_id: @shop.id, class: 'button blue-button', data: { method: 'post' } %> with setup should change controller method well:
def update_shop ... @user.update_attributes(shop_id: params[:shop_id]) ... end
Comments
Post a Comment