开发者

How can you pass an object from the form_for helper to a method?

So let's say I have a form which is being sent somewhere strange (and by strange we mean, NOT the default route:

<% form_for @form_object, :url => {:controller => 'application',
                  :actio开发者_Go百科n => 'form_action_thing'} do |f| %>
  <%= f.text_field :email %>
  <%= submit_tag 'Login' %>
<% end %>

Now let's say that we have the method that accepts it.

def form_action_thing
  User.find(????? :email ?????)
end

My questions are thus:

  1. How can I make the object @form_object available to the receiving method (in this case, form_action_tag)?
    • I've tried params[:form_object], and I've scoured this site and the API, which I have to post below because SO doesn't believe I'm not a spammer (I'm a new member), as well as Googled as many permutations of this idea as I could think of. Nothing. Sorry if I missed something, i'm really trying.
  2. How do I address the object, once I've made it accessible to the method? Not params[:form_object], I'm guessing.

EDIT

Thanks so much for the responses, guys! I really appreciate it. I learned my lesson, which is that you shouldn't deep-copy an object from a form, and that the parameters of a form are actually included when you submit it.

I will admit it's sort of disheartening to not know stuff that seems so obvious though...


you need to pass the "id" of your "@form_object" in the url and then lookup that object (assuming you have a model and using ActiveRecord)


It depends on how do you set up your routes. If you're using the default /:controller/:action/:id route, you can pass it as a parameter in the URL. Note that not the whole @form_object can/should be passed, but it's id or some other attribute to identify it instead. In this case, you should make your URL:

<% form_for @form_object, :url => {:controller => 'application',
                  :action => 'form_action_thing', :email => some_email} do |f| %>
  <%= f.text_field :email %>
  <%= submit_tag 'Login' %>
<% end %>

And in your controller

def form_action_thing
  @user = User.find_by_email(params[:email])
end


You can pass parameters through the url, but when submitting a form the only thing that should (probably) be passed through the url is the record id for a RESTful record.

And it appears you didn't find out yet where your form data can be found in the params.

So

  • All the data from your form should end up in params[:form_object]. The actual value for :form_object is selected by Rails, it's probably coming from the object's class (too lazy to look that up right now)
  • In any case, you can easily find out where your form values are submitted by looking at your console/log output. All the params for each requests are dumped there.
  • The form fields will be inside the params like params[:form_object][:email] - each field that is submitted has an entry corresponding to the field name.
  • The params hash not contain all the original values from your @form_object. There will be only those values that you included in the form.
  • If you need to pass non-editable values to the controller with your form, use hidden_field(s) These will be submitted with the form, but are not visible to the user.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜