# LogIn Button using remote\_function

**URL:** <https://www.ruby-forum.com/t/login-button-using-remote-function/171893>\
**Category:** Rails\
**Created:** [July 10, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/login-button-using-remote-function/171893 "2009-07-10T23:13:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlovel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@jlovel](https://www.ruby-forum.com/u/jlovel)\
**Post date:** [July 10, 2009, 11:13pm UTC](https://www.ruby-forum.com/t/login-button-using-remote-function/171893/1 "2009-07-10T23:13:00Z")

</div>

I am trying to make a very simple login page that has light-box like  
features. The only thing I cannot get to work is the behavior of the  
actual login button.  
When you click on it it does nothing. I simply want to submit the info  
given by the client and redirect either to a private page or make the  
client log in again. Here is the code for my log in box view:

User name: \<%= text\_field("userform", "user\_name",:size=\>"20" ) %\>  
Password: \<%= password\_field("userform", "password",:size=\>"20" ) %\>  
 \<%= javascript\_include\_tag :defaults %\> \<%= submit\_tag 'LOG IN', :onclick =\>remote\_function(:url =\> {:action =\> :authenticate, } ) %\>  

And here is the code from my controller for the authentication action I  
am calling on:

class UserController \< ApplicationController  
def authenticate  
@user = User.new(params[:userform])  
valid\_user = User.find(:first,:conditions =\> ["user\_name = ? and  
password = ? ",@user.user\_name, @user.password])  
if valid\_user  
session[:user\_id]=valid\_user.user\_name  
redirect\_to :action =\> ‘private’  
else  
flash[:notice] = “Invalid User/Password”  
redirect\_to :action=\> ‘login’  
end  
end  
def login  
end  
def private  
if !session[:user\_id]  
redirect\_to :action=\> ‘login’  
end  
end  
def logout  
if session[:user\_id]  
reset\_session  
redirect\_to :action=\> ‘login’  
end  
end  
end

When I tried typing the code  
submit\_tag ‘LOG IN’, :onclick =\>remote\_function(:url =\>  
{:action =\> :authenticate, } )  
into irb, I got this error message:  
NoMethodError: undefined method `remote\_function’ for main:Object

There may be a simple error that I am making, as I am pretty new to the  
ruby language. Also, if anyone knows another way to do this that will  
work with the partial rendering I use to display the login box, that  
would be fine, too.

---

<div class="post-metadata">

**Author:** ![jlovel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@jlovel](https://www.ruby-forum.com/u/jlovel)\
**Post date:** [July 10, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/login-button-using-remote-function/171893/2 "2009-07-10T23:37:31Z")

</div>

On Jul 10, 10:13 pm, Julia L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

Because you’re just using an onclick on the submit tag this isn’t  
going to know that you want it to submit these form parameters. Why  
not use remote\_form\_for ?  
It’s also pretty weird to have the tags to load javascript where you  
have put them.

> ```
> redirect_to :action => 'private'
> else
> flash[:notice] = "Invalid User/Password"
> redirect_to :action=> 'login'
> end
> 
> ```

You’ve not told remote\_function that you want to update a page element  
so unless you send back some javascript. You’re just doing a redirect  
in your authenticate action, however that redirect just redirects the  
ajax request you made - it won’t affect the page the user is on at  
all. if you actually wanted to redirect them to the private page you  
would need to use rjs (or generate the appropriate javascript by  
hand). If what you wanted was to redirect them to the private action  
then why use ajax at all ?

> When I tried typing the code  
> submit\_tag ‘LOG IN’, :onclick =\>remote\_function(:url =\>  
> {:action =\> :authenticate, } )  
> into irb, I got this error message:  
> NoMethodError: undefined method `remote\_function’ for main:Object

That’s because functions like remote\_function are only included in  
your views. If you use rails’ script/console there is an object called  
something like helpers which has the various view helper methods  
included.

Fred

---

<div class="post-metadata">

**Author:** ![jlovel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@jlovel](https://www.ruby-forum.com/u/jlovel)\
**Post date:** [July 11, 2009, 12:11am UTC](https://www.ruby-forum.com/t/login-button-using-remote-function/171893/3 "2009-07-11T00:11:01Z")

</div>

Thanks so much for the feedback Fred!  
This is my first rails project that I’m writing, so I need lots of help.  
The java script tags were an error I noticed right after I posted, I  
usually put it in the head. Not intentional.  
But, as far as the many real problems with my code, could you give me  
some idea of how it should look corrected? I’m starting with nothing,  
pretty much, so any hint would help.

Frederick C. wrote:

> On Jul 10, 10:13ï¿½pm, Julia L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 
> 
> Because you’re just using an onclick on the submit tag this isn’t  
> going to know that you want it to submit these form parameters. Why  
> not use remote\_form\_for ?  
> It’s also pretty weird to have the tags to load javascript where you  
> have put them.
> 
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ redirect\_to :action =\> ‘private’  
> > ï¿½ ï¿½ ï¿½ ï¿½ else  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ flash[:notice] = “Invalid User/Password”  
> > ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ ï¿½ redirect\_to :action=\> ‘login’  
> > ï¿½ ï¿½ ï¿½ ï¿½ end
> 
> You’ve not told remote\_function that you want to update a page element  
> so unless you send back some javascript. You’re just doing a redirect  
> in your authenticate action, however that redirect just redirects the  
> ajax request you made - it won’t affect the page the user is on at  
> all. if you actually wanted to redirect them to the private page you  
> would need to use rjs (or generate the appropriate javascript by  
> hand). If what you wanted was to redirect them to the private action  
> then why use ajax at all ?
> 
> > When I tried typing the code  
> > ï¿½submit\_tag ‘LOG IN’, :onclick =\>remote\_function(:url =\>  
> > {:action =\> :authenticate, } )  
> > into irb, I got this error message:  
> > NoMethodError: undefined method `remote\_function’ for main:Object
> 
> That’s because functions like remote\_function are only included in  
> your views. If you use rails’ script/console there is an object called  
> something like helpers which has the various view helper methods  
> included.
> 
> Fred

---

<div class="post-metadata">

**Author:** ![jlovel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@jlovel](https://www.ruby-forum.com/u/jlovel)\
**Post date:** [July 11, 2009, 12:38am UTC](https://www.ruby-forum.com/t/login-button-using-remote-function/171893/4 "2009-07-11T00:38:51Z")

</div>

Also,

I’d recommend following this set of screen learning apps (they are free)

[http://www.buildingwebapps.com/learningrails](http://www.buildingwebapps.com/learningrails)

When you get to lesson 11, it covers user authentication in great  
detail.

---

<div class="post-metadata">

**Author:** ![jlovel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@jlovel](https://www.ruby-forum.com/u/jlovel)\
**Post date:** [July 11, 2009, 12:22pm UTC](https://www.ruby-forum.com/t/login-button-using-remote-function/171893/5 "2009-07-11T12:22:06Z")

</div>

On Jul 10, 11:11 pm, Julia L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Thanks so much for the feedback Fred!  
> This is my first rails project that I’m writing, so I need lots of help.  
> The java script tags were an error I noticed right after I posted, I  
> usually put it in the head. Not intentional.  
> But, as far as the many real problems with my code, could you give me  
> some idea of how it should look corrected? I’m starting with nothing,  
> pretty much, so any hint would help.

I made a few suggestions in a previous message. Did you try any of  
them ? Also when you say nothing happened it would be helpful if you  
could clarify what sort of nothing: nothing changed visually on the  
page, or no ajax request was made, or one was made but your server did  
not respond to the request, something else ? You may find firebug  
useful for diagnosing this sort of thing (and i’d very much recommend  
that you start by working out exactly why things aren’t working)

Fred

---

<div class="post-metadata">

**Author:** ![jlovel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f05b48/32.png) [@jlovel](https://www.ruby-forum.com/u/jlovel)\
**Post date:** [July 11, 2009, 12:37am UTC](https://www.ruby-forum.com/t/login-button-using-remote-function/171893/6 "2009-07-11T00:37:06Z")

</div>

Hi Julia,

First, I’ll touch on some of the things you may or may not know.

IRB is something you use to test ruby code, specific to the ruby  
interpreter.  
script/console or the Rails console is what you use to test rails code,  
specific to rails.

So, it’s better to get used to using the console in rails.

Second, it appears you are trying to create your own authentication MVC.  
I wouldn’t do that, especially if this is your first rails project. I  
would use a plugin called Authentication…

[http://agilewebdevelopment.com/plugins/restful\_authentication](http://agilewebdevelopment.com/plugins/restful_authentication)

After using that, you can alter the way your authentication system works  
or behaves but you won’t have to develop everything from scratch. It  
also applies RESTful behavior to your controllers which is really what  
you want to try and maintain in rails. The more restful your  
controllers are, the easier they are to manage.

Let me know if you have any questions on this. I’d be happy to help.
