Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
NewB.. need help with routes
6 Posts
NewB.. need help with routes

I have a model called "user" and a controller called "sessions" with method "new"

def new

@user = User.new()

end

I have used the form_for helper to generate the view for "new"

Please log in.

Username:

Password:

For this to work, I had to add..

map.resource :session map.resource :users

...to routes.rb

Can anyone help me understand why "both" session and users was needed?

Your code is a bit out-of-the-box. What you're after help with is not actually routes per-se, but REST. Start "here":http://guides.rubyonrails.org/getting_started.html and read the links from "the REST section":http://guides.rubyonrails.org/getting_started.html#rest and you will also find "this":http://guides.rubyonrails.org/routing.html#restful-routing-the-rails-default very useful.

i knw the easier and the most obvious way to do it is name the model and the controller same. I eventually did that and as expected it worked. but I was just curious as to how routes can be handled if model name != controller name.

@jason - Out-of-the-box... yes.. I actually used bits from 2 tutorials to get to this code... :D Thanks for the links !

Ah, ok. I see where you're coming from. The Rails answer to your question is "don't" :) A core tenet of Rails is Convention over Configuration - the consequence of which is that it's much easier to just do things the way Rails wants you to, than try and work around it.

If you ever do need to then Rails is nice enough to have reasonable syntaxes for most situations. For the one above you just had to change the view syntax from form_for(@user) to something like form_for @user, :url => :sessions which will generate the right ACTION for your form, and still use the @user model to populate and name the input fields.

Tanmay, the problem you've run into is that #form_for tries very hard to guess what you mean based on the limited information you give it. Remember that when you create a Rails form you have to tell it at least two things: the model whose properties the form should display, and the URL to which the form should post. You gave form_for only the @user instance variable, so it used that as the model and then inferred the controller name from the name of that variable. It also infers the correct HTTP method based on the current template.

Personally (and many people will disagree with me on this), I prefer to always write all of my form_fors in the same format, with explicit parameters:

 users_path, :html => { :method => :post } %>

I find this more descriptive of my intentions, and much easier to read when I come back to the code later.

That said, Jason is right that POSTing a new user form to a sessions controller is possible, but doesn't make much sense. Think of it as Session being your resource (noun) and Create being your action (verb). If I ask a web service to "create a session" I wouldn't expect a user back.

@Jason and Adam - I got what you guys mean... The thing is, I had a deadline to complete this task and dint really bother to get into the basics.. i knw.. very wrong on my part!! :D But 4 days on Rails has made me understand this now.. Thanks a lot for the help !!

6 Posts
Login to add your message