Reputation: 4016
I am very new to Rails, and been asking lots of questions recently, I'm getting increasingly frustrated with figuring out how to work things in rails (i.e logging to console, routes, etc..), now I'm trying to figure out how to communicate between an HTML form (and please raw HTML, not form helpers or any of that voodoo magic, I really am trying very hard to like rails, so one thing at a time), if you want to add helpers, please explain what they are doing, where they go, how to use, etc...
I've tried all the examples on the first/second google page (gruesome I know! second google page?!) What I am looking for is an example, and an explanation.
Also, how does it fit in with routes? (i.e what would be the proper way of setting up a controller/view, and routing them without the controller in the URL)
Please keep in mind, I am not using any models, just controllers, and views.
Your thoughts are much appreciated.
Upvotes: 1
Views: 67
Reputation: 908
Oh no! Sorry that Rails is giving you grief, especially about form helpers! It's a shame because Rails' form helpers are pretty great, and IMHO are a super useful part of Rails in reducing code duplication, and allowing you to not worry about the specific field names that you are submitting to a server. (They're hard to get accustomed to, but worth it.)
Let me give you a summary of Rails form helpers. For starters, let's take this a simple example
<form action='/users' method='POST'>
<input type='text' name='username' />
<input type='password' name='password' />
<input type='submit' name='Create user' />
</form>
Ah, the most basic user signup form imaginable. This will POST to /users, which is REST-convention for "create a user".
The Rails Routing guide tells you that if you have a controller UsersController
(in app/controllers/users_controller.rb -- the filename must be exactly this) and a route resources :users
, then this will call the code in the create
method of UsersController, and you will have access to the form POST data via the params
hash, i.e. params[:username]
, params[:password]
, etc.
It is in that controller action where you would create an instance of your User
model and save it to the database. (If you aren't using models, you're missing out on most of the magic of Rails's forms.)
Okay. So far so good, right?
Let's spice things up a bit by using Rails's form helpers.
A quick rewrite of the form using basic Rails form helpers would look something like this.
<%= form_tag '/users', method: 'POST' do %>
<%= text_field_tag 'username' %>
<%= password_tag 'password' %>
<%= submit_tag 'Create User' %>
<% end %>
So far, no Rails magic - it's a straight translation into HTML.
Let's add some Rails marrying.
Again, assuming you have a User
model with a username
column and a password
attribute (attr_accessor :password
), and your UsersController
's #new
method looks like this:
def new
@user = User.new
end
Then, in this form (which lives in app/views/users/new.html.erb
), you can use that unpersisted User
object to make the form more intelligent:
<%= form_for @user do |f| %>
<%= f.text_field :username %>
<%= f.password :password %>
<%= f.submit 'Create User' %>
<% end %>
Note the do |f|
part of the form_for
line. The f
is a form object, which remembers the object it describes. So, you can call f.text_field :username
, and the field will magically have the existing value of @user.username
populated as the value
of the <input>
.
Then, if you have a UsersController#create
method that looks something like this:
def create
# Instantiate a new user with the inputted parameters from the form
@user = User.new(params[:user])
if @user.save
# user is saved successfully! redirect somewhere....
else
# user couldn't be saved successfully, because of some rule that you
# define on the User model - like making sure that usernames are
# unique, or something like that.
#
# so, we can use the attributes on @user to prepopulate the new user form
render :new
end
end
Anyway, I hope that gives you a good overview of why Rails form helpers can be useful, and how to use them with your models. Let me know if you have any questions!
Upvotes: 3