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
精彩评论