问题
I have a link
<%= link_to 'Add link', new_link_path(:link => {:item1_id => @item.id}) %>
passing to
def new
@link = Link.new(params[:link])
@items = Item.all
form is
<%= form_for(@link) do |f| %>
<%= f.hidden_field :item1_id %>
<%= f.collection_select :item2_id , Item.all , :id , :name %>
So I want to access like the image linked to the item1_id item. How do I access it? i tried
@item1 = Item.find_by_id(params[:link])
and
@item1 = Item.find_by_id(params[:item_id])
but I don't know what is right.
回答1:
try:
@item1 = Item.find(params[:link][:item1_id])
回答2:
looks like you need:
@item1 = Item.find_by_id(params[:link][:item1_id])
来源:https://stackoverflow.com/questions/7440560/access-params-hash-in-controller