开发者

Send an extra parameter through a form in rails 3

Is there a way to send an extra parameter through a form in rails 3?

For example:

<%= form_for @post do |f| %>
<%= f.hidden_field :extraparam, :value => "22" %>
<% end %>

but lets say :extraparam isn't part of the post model..

I have an unknown attribute error in the crea开发者_开发知识库te method of the controller when I try this, any ideas?

(I want to use the param value itself in the controller for some extra logic)


Call hidden_field_tag directly. See: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-hidden_field_tag

These helpers exist for all the major form field types, and are handy when you need to go beyond your model's methods.


The following worked for me in passing extra parameters from the view back to the controller that were a part of my model and not part of my model.

    <%= hidden_field_tag :extraparam, value %>

Example usage

<%= hidden_field_tag :name, "John Smith" %>


Ya Paul is right. Hidden_field is associated with your model whereas the extra _tag fields are not. I'm not sure of your needs but It's generally recommended in the RoR community to avoid passing a ton of hidden_fields like you might do in a php application.

Ive seen some code where ids were getting passed around in hidden fields which rails takes care on its own if you know the best practices and take full advantage of the framework. Of course I'm just saying this as general info as there are sometimes better ways at accomplishing the same functionality. Good luck on your apps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜