Hi I am new to ruby and trying to obtain the user name in the login index.rhtml where I could display "You have logged in [user name]" How could I do this? I have been trying different options after reading the ruby manual but still throw an error. please help -- Posted via http://www.ruby-forum.com/.
woow wrote:> Hi I am new to ruby and trying to obtain the user name in the login > index.rhtml where I could display "You have logged in [user name]" > > How could I do this? I have been trying different options after reading > the ruby manual but still throw an error. > > please helpCan you post some of the things you''ve tried ? How did you get the user name that you''re trying to display? Alan -- Posted via http://www.ruby-forum.com/.
Alan Francis wrote:> Can you post some of the things you''ve tried ? How did you get the user > name that you''re trying to display? > > Alan<%= error_messages_for ''user'' %> You have logged in !! <% user = User.find(params[:name]) %> <% user.name %> Thats what I''ve got but I might be doing some stupid mistake. I am kinda lost in this. -- Posted via http://www.ruby-forum.com/.
"woow" <ruwanb@gmail.com> wrote in message news:ea06811297c8ce8cf521938e99a988f8@ruby-forum.com...> Alan Francis wrote: >> Can you post some of the things you''ve tried ? How did you get the user >> name that you''re trying to display? >> >> Alan > > <%= error_messages_for ''user'' %> > > You have logged in !! > <% user = User.find(params[:name]) %> > <% user.name %> >try <%= user.name %>, <% %> just processes the ruby, <%= %> will process it and output the result
Alan Bullock wrote:> "woow" <ruwanb@gmail.com> wrote in message > news:ea06811297c8ce8cf521938e99a988f8@ruby-forum.com... > try <%= user.name %>, > > <% %> just processes the ruby, <%= %> will process it and output the > resultyeah I believe I tried that before. I did try it again and now it throws this error... "Couldn''t find User without an ID" Thank you for your help. -- Posted via http://www.ruby-forum.com/.
"woow" <ruwanb@gmail.com> wrote in message> "Couldn''t find User without an ID"sorry, I overlooked that. User.find expects a numeric record ID. Try this instead: User.find_by_name(params[:name])
Date: Mon, 17 Jul 2006 18:05:19 +0200> From: woow <ruwanb@gmail.com> > Subject: [Rails] Re: getting the user name > To: rails@lists.rubyonrails.org > Message-ID: <22d293e83f3914a3156525eb499256a1@ruby-forum.com> > Content-Type: text/plain; charset=utf-8 > > Alan Bullock wrote: > > "woow" <ruwanb@gmail.com> wrote in message > > news:ea06811297c8ce8cf521938e99a988f8@ruby-forum.com... > > try <%= user.name %>, > > > > <% %> just processes the ruby, <%= %> will process it and > output the > > result > > yeah I believe I tried that before. I did try it again and > now it throws > this error... > > "Couldn''t find User without an ID"Try using <% user= User.find_by_name(params[:name]) instead of <% user = User.find(params[:name]) %> I believe the default find tries to use the id field.If you want the find to use the name field, you need to use find_by_name Hope that helps Bharat Blog: http://blog.publishedperspectives.com
Alan Bullock wrote:> "woow" <ruwanb@gmail.com> wrote in message >> "Couldn''t find User without an ID" > > sorry, I overlooked that. User.find expects a numeric record ID. Try > this > instead: > User.find_by_name(params[:name])Again the same situation with the same error? Hmmm ... -- Posted via http://www.ruby-forum.com/.
Hopefully this will give more info about whats going on..... This is my login_controller.rb -------------------------------------------- def login session[:user_id] = nil if request.post? user = User.authenticate(params[:name], params[:password]) if user session[:user_id] = user.id redirect_to(session[:original_uri] || { :action => "index" }) else flash[:notice] = "Invalid user/password combination" end end end def logout reset_session flash[:notice] = "Logged out" redirect_to(:action => "login" ) end def index @all_users = User.find_by_name(params[:name]) end ----------------------------------------------------- fallowing is my index.rhtml ---------------------------- <%= error_messages_for ''user'' %> You have logged in !! <% for user in @all_users %> <%= user.name %> <% end %> --------------------------------------------------------- now in the controller if I do def index @all_users = User.find_by_name(:all) end it prints all the user names but how could I make it to print the logged in user name only? -- Posted via http://www.ruby-forum.com/.
login_controller.rb: def index @user = User.find(session[:user_id]) end index.rhtml: <%= @user.name %> On 7/17/06, woow <ruwanb@gmail.com> wrote:> Hopefully this will give more info about whats going on..... > > This is my login_controller.rb > -------------------------------------------- > def login > session[:user_id] = nil > if request.post? > user = User.authenticate(params[:name], params[:password]) > if user > session[:user_id] = user.id > redirect_to(session[:original_uri] || { :action => "index" }) > else > flash[:notice] = "Invalid user/password combination" > end > end > end > > def logout > reset_session > flash[:notice] = "Logged out" > redirect_to(:action => "login" ) > end > > def index > @all_users = User.find_by_name(params[:name]) > end > ----------------------------------------------------- > > fallowing is my index.rhtml > ---------------------------- > <%= error_messages_for ''user'' %> > > You have logged in !! > <% for user in @all_users %> > <%= user.name %> > <% end %> > --------------------------------------------------------- > > now in the controller if I do > > def index > @all_users = User.find_by_name(:all) > end > > it prints all the user names but how could I make it to print the logged > in user name only? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Rick Martinez Director of Technology Complexity Gaming