Discussion Forums
- Topic List
- Most Recent Posts
- Sign In for more options
I am currently developing this website on ruby on rails and I got stuck. I created a form, using html () but now I want to pass the data entered in the form to the Controller to save it to the db. If I used rails to create the form I know how to pass the form input to the controller, but since i used html, I dont know how to. If i did my form this way:
How would I pass the two fields to the controller, assuming I have a database called "People".
Thanks PS I am new
Hello New User,
try this very goog tutorial about beginning rails.
http://articles.sitepoint.com/article/learn-ruby-on-rails
Axel
thanks for the link I actually already went through that tutorial and many others. Again, most tutorials explain how to pass data to the Controller if you used rails to build the form. I used plain html to build the form and would like to know how the data input in the form can be passed to the rails controller. Thanks
The data is passed with the HTTP POST, the controller receives a hash on the params variable. For example on your form, you access the data on the controller through the params[:name] and the params[:email].
You can do this:
def create @model = Model.new(:name => params[:name], :email => params[:email]) @model.save end
When you use the rails built in method to generate the form, if you look the html source code you will see:
When the form is submitted, the params hash will be:
params = { :model => { :name => 'some value', :email => 'another value' } }
Then, you can DRY up your controller
def create @model = Model.new(params[:model]) @model.save end
Because the params[:model] will return the the hash and pass it direct to the constructor of your model, populating your data.
I think it's explain the Rails magic =)
You need some knowledge about Form Helpers. A good start point are the Rails Guides
Your going to likely run into this error http://stackoverflow.com/questions/435373/session-problem-in-rails-2-2-actioncontrollerinvalidauthenticitytoken-in-user, if you don't use the form helpers or at least know what form fields the controller requires to be present in order for you to accomplish this.
