Hello.. I wanted to extend my webpage''s user interaction by allowing
users to register themselves. The account controller method "register"
takes care of the process. This is the register.rhtml code i wrote in
apps/views/accounts/register.rhtml :
<% form_for :user do |f| %>
<p>
login:<br />
<%= f.text_field :login %>
</p>
<p>
password:<br />
<%= f.password_field :password %>
</p>
<p>
name:<br />
<%= f.text_field :name %>
</p>
<p>
email:<br />
<%= f.text_field :email %>
</p>
<p><%= submit_tag %></p>
<% end %>
The register method in the account controller looks like :
def register
@u = User.new(params[:login])
@u = @current_user
if request.post? and @u.save
@current_user = User.new(params[:login])
flash[:notice] = ''Registration succeeded''
redirect_to :controller => ''story'', :action =>
''index''
end
end
The problem it gives a nil object error for the statement @u.save . I
donot understand since when im breakpointing ...$params shows that it
holds the entire submitted form values ...but $params[:login] => nil
$params[:name] => nil
where is the all the data going and why is it not getting saved. please
help
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
first you do:> @u = User.new(params[:login])then you overwrite @u with:> @u = @current_userbut @current_user is set later:> @current_user = User.new(params[:login])so most likely @u = @current_user sets @u to nil, since @current_user isn''t set before that. In any case you would lose the newly created user. And you should NEVER use variable names like @u that do not tell what they are good for... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thorsten Mueller wrote:> first you do: > >> � � �@u = User.new(params[:login]) > > then you overwrite @u with: >> � � �@u = @current_user > > but @current_user is set later: >> � � � �@current_user = User.new(params[:login]) > > so most likely @u = @current_user sets @u to nil, > since @current_user isn''t set before that. In any case > you would lose the newly created user. > > And you should NEVER use variable names like @u > that do not tell what they are good for...Thorsten.. was getting bugged rewriting the name .. so was using "u" .. i know its a bad habit.. so changed it.. anyways .. i applied the changes and the story saves..thanks.. but all the fields are being set to null. i mean the values the user enters for login password in registration page are all being set to null instead of what was enterd ... weird .. eh ?? is their a prob with the form_for object ? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
no, there is no problem with form_for but you''re using params[:login] instead of params[:user] a form like this has a params hash named as the model the form is made for the single fields are used then like: params[:user][:login] in your case User.new(params[:user]) should work --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks.. its working :) yes i guess i was hung up on login than the actual object name "user" User.find_by_login(params[:login]) should also work fine then ! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > User.find_by_login(params[:login]) should also work fine then ! >If it has it''s own form then maybe. But more likely you have a form with some name and some fields. so if the forms name is login and the fieldname is username then User.find_by_login(params[:login][:username]) or if the form is named user again and the textfield is login, then: User.find_by_login(params[:user][:login]) because find_by_login expects the single string, while User.new expects a hash with all the users attributes. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---