Browse the Ruby on Rails Community.

You are here: Forums Ask a Rails expert Using forms in Ruby on Rails...

Replytotopic

Using forms in Ruby on Rails

Posted in Forums : Ask a Rails expert

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

I am currently developing this website on ruby on rails and I got stuck. I created a form, using html (<form name>) 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:
<form name="test">
<input name="name" id="name">
<input name="email" id="email">
</form>

How would I pass the two fields to the controller, assuming I have a database called “People”.

Thanks
PS I am new

 
Profile

Authority 12
Posting Rating 50
Sign in to rate this post

Hello New User,

try this very goog tutorial about beginning rails.

http://articles.sitepoint.com/article/learn-ruby-on-rails

Axel

 
Profile

Authority 0
Posting Rating 0
Sign in to rate this post

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

 
Profile

Authority 12
Posting Rating 59
Sign in to rate this post

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:

<input name="model[name]" />
<input name="model[email]" />

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 =)

 
A09ee27328ddb7f4d7e679023c00d0d1

Authority 75
Posting Rating 0
Sign in to rate this post

You need some knowledge about Form Helpers. A good start point are the Rails Guides

http://guides.rubyonrails.org/form_helpers.html

 
Profile

Authority 37
Posting Rating 0
Sign in to rate this post

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.

 
Bc67c4366da44b0e82fee6ee978ca150

Authority 25
Posting Rating 0
Sign in to rate this post

rails docs may help you…
http://rails.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

http://dizzy.co.uk/ruby_on_rails/cheatsheets/form-helpers

Replytotopic

Other Recent Topics

Ask a Rails expert : nested application ApplicationController get called intead of children::ApplicationController

Ask a Rails expert : Best way to structure a database for a large/static dataset

Ask a Rails expert : Ruby Developer (ROR) - Scottish based (Remote working from within the UK)

Ask a Rails expert : Above Ground Pool Supplies

Ask a Rails expert : How to get url params in observer or model in Rails 3.1

Ask a Rails expert : What can persuade you to hire Junior Ruby devs with significant PHP experience?

Ask a Rails expert : What industry value does the Ruby or Rails Certification currently have?

Ask a Rails expert : Louis Vuitton Damier Azur Canvas specially sale ( www.salecheaplouisvuitton.com )

Ask a Rails expert : ·How to check errors/puts statements from ruby files which are under cronob

Ask a Rails expert : Louis Vuitton cheap Soft Sided Luggagespecial offer( www.salecheaplouisvuitton.com )

Formatting Help
  • *bold*       _italics_      
    bq. (quotes)
  • "DSC":http://www.dsc.net
  • * or # (lists)
or cancel