开发者

Is there any way for the controler to get values from a form in your view before it is saved?

I assume the values must be passed back to the controller for use, but 开发者_如何学Pythoneverything I have tried seems to only get values that have already saved in the db.


When a form is submitted, the controller will always have access to a hash called "params", which will contain all of the submitted data.

For example, if your form contains a textbox with a name "foo"

<input type="text" name="foo" />

the value can be retrieved in the controller using

fooValue = params[:foo]

You can use this to build a new instance of a model, containing the submitted values from the form as follows:

in your form:

<% form_for :person, @person, :url => { :action => "create" } do |f| %>
  <%= f.text_field :first_name %>
  <%= f.text_field :last_name %>
  <%= submit_tag 'Create' %>
<% end %>

then, in your controller:

@newPerson = Person.new(params[:person]; #this will pass the whole group of values within that person form to the "new" method
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜