access params hash in controller

瘦欲@ 提交于 2019-12-01 10:09:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!