ravip0711
ravip0711

Reputation: 511

How to pass parameters to the controller with the proper key

I'm trying to figure out how to get the store id on the controller side to delete the store.

Currently with this code my params on the controller side sends the store.id(1) as the value to the key format. I need to retrieve this by store_id: instead.

{..."controller"=>"home", "action"=>"delete_store", "format"=>"1"}

What I need:

{..."controller"=>"home", "action"=>"delete_store", "store_id"=>"1"}

HTML/ERB:

<h4>Your Stores:</h4>
  <% @my_stores.each do |store| %>
    <p><%= store.name %><%= link_to "X", delete_store_path(store.id), method: :delete %></p>
  <% end %>

Controller:

class HomeController < ApplicationController
...
  def delete_store
    # With current code, I would have to do
    # current_user.stores.where(store_id: params[:format] )
    # But to make it proper I need this
    # current_user.stores.where(store_id: params[:store_id] )
  end
end

Upvotes: 0

Views: 40

Answers (1)

James Milani
James Milani

Reputation: 1943

You can get it in the params with a hidden_field_tag:

<%= hidden_field_tag :store_id, store.id %>

This should allow you to do a lookup like:

params[:store_id]

Here is a link to the apidock for those wanting more info on hidden_field_tag. http://apidock.com/rails/ActionView/Helpers/FormTagHelper/hidden_field_tag

Upvotes: 1

Related Questions