Passing parameters from view to controller

橙三吉。 提交于 2019-11-27 22:19:01

You can add information to the params hash right through the link_to. I'm not sure exactly what you are trying to do but I did something like this recently to add the type of email I wanted when I link to the new email

<%= link_to 'Send Thanks', new_invoice_email_path(@invoice, :type => "thanks") %>

Now my params looks like:

{"type"=>"thanks", "action"=>"new", "controller"=>"emails", "invoice_id"=>"17"}

I can access the type via the params

email_type = params[:type]

Instead of a string, if you pass in the instance variable @rela you will get the object_id in the params hash.

Per the comment below, I'm adding my routes to show why the path new_invoice_email_path works:

resources :invoices do
  resources :emails
end

When a request comes in, the controller (and any model calls) will be processed first and then the view code gets processed last. The view can call methods in the helpers, but can't reference functions back in the controller.

However, once a page is rendered, you can either post information back to the controller as part of a new request or use ajax/JQuery (etc) to make a call to a controller function remotely.

Does that help?

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