Rails - How to Pass Parameter to Different Controller to edit Object -


i have store class want enter in toy id simple form attach toy object store. thing passing through params[:id], not params[:toy_id]

store _form.html.erb:

<%= form_tag({controller: "toys", action: "update"}, method: "post") %>     <%= text_field_tag(:toy_id) %>     <%= submit_tag("update") %> <% end %> 

after clicking submit, should go toyscontroller 2 parameters: 1) params[id] store id , 2) params[:toy_id]

toyscontroller=>update:

def update   respond_to |format|     store = store.find(params[:id])     toy = toy.find(params[:toy_id])     toy.store_id = store.id     toy.save      if @toy.update(toy_params)       format.html { redirect_to @toy, notice: 'toy updated.' }       format.json { render :show, status: :ok, location: @toy }     else       format.html { render :edit }       format.json { render json: @toy.errors, status: :unprocessable_entity }     end   end end 

as using form_tag, have pass value of toy_id well. try this:

<%= text_field_tag(:toy_id, @toy.id) %> 

you may want use form_for instead of form_tag. because, form_for method handles grabbing values model, , try route form appropriate action. form_tag responsible makes no assumptions you're doing.

also, updating record, should use put method instead of post method. post creating records.

another method:

you should able send toy_id controller using hidden_field_tag this:

<%= hidden_field_tag :toy_id, @toy.id %> 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -