I want to update member_id, which triggers update of :location_stock.
I have got these custom actions in costumes_and_cost_records_controller.rb:
def rent
@costumes_and_cost_record = CostumesAndCostRecord.find(params[:costumes_and_cost_record_id])
end
def rent_method
@costumes_and_cost_record = CostumesAndCostRecord.find(params[:costumes_and_cost_record_id])
@costumes_and_cost_record.update_attributes(:member_id => params.permit(:member_id), :location_stock => true)
redirect_to @costumes_and_cost_record
end
I used simple form in view rent.html.erb:
<%= simple_form_for @costumes_and_cost_record do |f| %>
<%= f.association :member, :label => "Member", label_method: :to_s, value_method: :member_id, include_blank: true %>
<%= f.submit "Rent", :controller => :costumes_and_cost_records, :action => :rent_method, :method => :put, :member_id => :member %> <%# updates only member_id, doesnt update :location_stock %>
<%# link_to "Rent", :controller => :costumes_and_cost_records, :action => :rent_method, :method => :put, :member_id => :member_id %> <%# updates :location_stock, sets member_id = NULL %>
<% end %>
Now if I use submit button :member_id updates but controller doesn't update :location_stock.
If I use link_to :location_stock
updates, but :member_id
is set to NULL.
I want to update both attributes. Should I use submit button or link_to and how to fix this issue?
I set routes.rb to make both link_to and submit methods in view work:
resources :costumes_and_cost_records do
post 'show'
get 'rent'
get 'rent_method'
end
Any help is greatly appreciated.
If I understand you correctly you need to call rent_method to update member_id
put 'rent_method'
View changes
<%= simple_form_for @costumes_and_cost_record, method: :put, url: [@costumes_and_cost_record, :rent_method] do |f| %>
<%= f.association :member, :label => "Member", label_method: :to_s, value_method: :member_id, include_blank: true %>
<%= f.submit "Rent" %>
<% end %>
Controller
def rent_method
@costumes_and_cost_record = CostumesAndCostRecord.find(params[:costumes_and_cost_record_id])
@costumes_and_cost_record.update_attributes(:member_id => member_params[:member_id], :location_stock => true)
redirect_to @costumes_and_cost_record
end
def member_params
params.require(:costumes_and_cost_record).permit(:member_id)
end