# Umm... pretty much lost

**URL:** <https://www.ruby-forum.com/t/umm-pretty-much-lost/125808>\
**Category:** Rails\
**Created:** [January 12, 2008, 7:23am UTC](https://www.ruby-forum.com/t/umm-pretty-much-lost/125808 "2008-01-12T07:23:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 12, 2008, 7:23am UTC](https://www.ruby-forum.com/t/umm-pretty-much-lost/125808/1 "2008-01-12T07:23:01Z")

</div>

I believe I should have done everything correct here. I have a  
user\_controller. I am trying to add a login method, so I add:

# GET /users/login

def login  
if request.post? and params[:user]  
@user = User.new(params[:user])  
user =  
User.find\_by\_username\_and\_password(@user.username,@user.password)  
if user  
session[:user\_id] = user.id  
flash[:notice] = “Welcome online, #{@user}”  
#redirect\_to :action =\> “show”  
redirect\_to :action =\> “index”  
else  
#remove Password from the view  
@user.password = nil  
flash[:notice] = “Invalid username/password combination.”  
end

```
end

```

end

Then I create in /views/users/ the file called login.rhtml.

But for some reason, whenever I go to /public/users/login, Rails  
thinks login is supposed to be a userid, and so it takes me to my  
show.rhtml and show action. The login.rhtml page comes up when I use /  
public/users/login/[anything], though… so why does it not recognize  
it as a proper action? It insteads thinks show is the action by  
default when a userid is present. Any ideas?

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 12, 2008, 7:38am UTC](https://www.ruby-forum.com/t/umm-pretty-much-lost/125808/2 "2008-01-12T07:38:48Z")

</div>

On Jan 11, 10:21 pm, kopf1988 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But for some reason, whenever I go to /public/users/login, Rails  
> thinks login is supposed to be a userid, and so it takes me to my  
> show.rhtml and show action.

I’m guessing you’re using RESTful routes, is that true?

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 12, 2008, 7:41am UTC](https://www.ruby-forum.com/t/umm-pretty-much-lost/125808/3 "2008-01-12T07:41:43Z")

</div>

It appears that way… seems to have been the default that happened  
after scaffolding. So, I added this line to my index.rhtm:

\<%= link\_to ‘Login’, login\_user\_path %\>

And got a no method error of some sort, so I tried this:

\<%= link\_to ‘Login’, :controller =\> “users”, :action =\> “login” %\>

and that makes a link that doesn’t work. I checked routes.rb, and  
there is nothing in there about users other than the standard  
“map.resources :users”.

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 12, 2008, 7:52am UTC](https://www.ruby-forum.com/t/umm-pretty-much-lost/125808/4 "2008-01-12T07:52:49Z")

</div>

On Jan 11, 10:40 pm, kopf1988 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> there is nothing in there about users other than the standard  
> “map.resources :users”.

Use ‘rake routes’ to get information on the route mappings. If you’ve  
got a resource called ‘users’ defined in routes.rb, then ‘rake routes’  
will show you something like this:

user GET /users/:id  
{:action=\>“show”, :controller=\>“users”}

This means that a ‘GET’ request to a URL of the form /users/login will  
treat the ‘/login’ part as a user id and will call the ‘show’ action  
of the ‘users’ controller. Which is exactly the behavior you’re  
getting. If you want to define a ‘login’ action for users that works  
on the whole collection, then you need to add something like this to  
routes.rb:

map.resources :users, :collection =\> {:login =\> :get}

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 12, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/umm-pretty-much-lost/125808/5 "2008-01-12T21:43:54Z")

</div>

But then when I submit the form it thinks I’m supposed to be pointed  
to the create action, instead of the :login action… so I tried to  
add:

map.resources :users, :collection =\> {:login =\> :post}

But that didn’t work. How do I do that part?

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 12, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/umm-pretty-much-lost/125808/6 "2008-01-12T21:43:54Z")

</div>

I probably have to change this part somehow:

\<% form\_for(:user, :url =\> users\_path) do |f| %\>

---

<div class="post-metadata">

**Author:** ![kopf1988](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/74df32/32.png) [@kopf1988](https://www.ruby-forum.com/u/kopf1988)\
**Post date:** [January 12, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/umm-pretty-much-lost/125808/7 "2008-01-12T21:49:46Z")

</div>

\<% form\_for(:user, :url =\> users\_path) do |f| %\>

or, simply \<% form\_for @user do |f| %\> (as long as @user == User.new)

will point to the create action

You need to point it to the login path:

\<% form\_for :user, :url =\> login\_user\_path, :method =\> :get do |f| %\>

With this mapped resource:

map.resources :users, :collection =\> { :login =\> :get }
