How to do a puts in user.rb

Hi,

My form_for is loading properly and also storing in the db.

My show user is also fine.

I just want to do a puts “user name is #{user.name}” this gives me
NoMethodError in UsersController#new

When I use self.name it give the same error

but when I do the same at the console, it works fine. What is that I am
missing.

Thanks

On 23 February 2011 15:03, Bhasker H. [email protected]
wrote:

When I use self.name it give the same error

but when I do the same at the console, it works fine. What is that I am
missing.

Show us the code that is failing and the full error message and tell
us which line it is failing on. Then we might be able to help.

Colin

This is what I have and it gives the error.

validates :uname, :presence => true,
:length => { :maximum => 50 }

puts “The name is set to #{user.uname}”

But without the puts it updates the record into the databse. I think
it is
to do with scoping. If have this puts after make salt then I have no
problem

On 23 February 2011 16:03, Bhasker H. [email protected]
wrote:

Please don’t top post, it makes it difficult to follow the thread.
Insert your reply at appropriate points in previous post. Thanks

This is what I have and it gives the error.

validates :uname, :presence => true,
:length => { :maximum => 50 }

puts “The name is set to #{user.uname}”

But without the puts it updates the record into the databse. I think it is
to do with scoping. If have this puts after make salt then I have no
problem

You still have not shown the complete error message as I asked.

Is the puts line immediately after the validates line or is it in
another method? Show us a bit more code around the problem.

Colin

On Feb 23, 2011, at 11:03 AM, Bhasker H. wrote:

This is what I have and it gives the error.

validates :uname, :presence => true,
:length => { :maximum => 50 }

puts “The name is set to #{user.uname}”

But without the puts it updates the record into the databse. I
think it is to do with scoping. If have this puts after make salt
then I have no problem

If you’re inside of user.rb, then user.uname won’t have any context.
Try self.uname or just uname, and see if that works.

Walter

Yeah, I have tried both of them and even :name. Unfortunately the ans
is
no.

How does validates work then ?

Regards,

I am still unable to understand where is the current_user working and
where
not?
It appears like you are using nifty:authentication for authentication,

post the controller method, mention the scope where it is, where are you
using the helper, etc etc. and then we could help you in a better way.

On Wed, Feb 23, 2011 at 4:37 PM, Bhasker H. <

On Wed, Feb 23, 2011 at 10:14 PM, Colin L. [email protected]
wrote:

puts “The name is set to #{user.uname}”

But without the puts it updates the record into the databse. I think it
is
to do with scoping. If have this puts after make salt then I have no
problem

I cannot access my server currently, but I have sent the error message in
the first mail.

Yes, the puts is immediately after the validates. It is in the user.rb
file
and user is my model.

The puts works fine after the before* callback.

Hi Jatin,

The flow is like this.

user_controller -> create new user view -> user updates the form -> then
I
do the validates in the user model -> there immediatly after validate
i do
“puts”.

Here the puts has problem but when I have the puts in the before
callback
(where I generate and store the salt of the password) it does not show
any
error.

Thanks & Regards,

On Thu, Feb 24, 2011 at 12:03 AM, Bhasker H. <
[email protected]> wrote:

This is what I have and it gives the error.

validates :uname, :presence => true,
:length => { :maximum => 50 }

puts “The name is set to #{user.uname}”

I don’t know why you’re getting the error but why do you want to do
this?
even if it doesn’t throw an error,
the line with puts won’t run after validates because the save process
doesn’t work that way.

if you really want to use that line, use it inside an after_validation
callback so it will be run after validation.

after_validation :puts_user_name

def puts_user_name
puts “The name is set to #{user.uname}”
end

Hi,

“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Thu, Feb 24, 2011 at 5:28 PM, Jim Ruther N. [email protected]
wrote:

puts “The name is set to #{user.uname}”
after_validation :puts_user_name

def puts_user_name
puts “The name is set to #{user.uname}”
end

I want to update some fields of the user based on some criteria that the
user inputs.

puts “the name of the user is #{user.name}” the entire line appears in
the
server log window (rails server) window, expcet for the value of
variable.

I will check the after_validation and see if I can use it – thanks for
the
idea.

Can you also expalin how validates get the values from the form_for

Regards,

On 24 February 2011 10:14, Bhasker H. [email protected]
wrote:

This is what I have and it gives the error.

I cannot access my server currently, but I have sent the error message in
the first mail.

No you did not put the complete error message in the first post, the
message would have gone on to say which method was undefined and would
have told you where the problem had occurred.

Yes, the puts is immediately after the validates. It is in the user.rb file
and user is my model.

The validates line does not itself do the validation, it is a macro
that registers a callback to do the validation at the appropriate
point. The validates line itself is executed when the file is loaded,
not when the validation is done. Similarly your puts will not be
executed after the validation, but when the file is loaded (after the
validates macro is run). If the purpose of the puts is to help to
debug your application have a look at the Rails Guide on debugging.
It explains, amongst other techniques, how to use ruby-debug to break
into your code and inspect data and program flow.

Colin

On Thu, Feb 24, 2011 at 5:55 PM, Colin L. [email protected]
wrote:

Colin

I click “sign up” -> form_for gets displayed -> I click “save” then in
user.rb validates takes place. Am I correct to say this.

Regards,

On 24 February 2011 12:09, Bhasker H. [email protected]
wrote:


Can you also expalin how validates get the values from the form_for

It doesn’t, your code does that and then calls save. When you call
save the validations get called at appropriate points around the save
to check the data.

Have a look at the Rails Guide ‘ActiveRecord Validations and Callback’.

Colin

On Thu, Feb 24, 2011 at 8:36 PM, Bhasker H. <
[email protected]> wrote:

save the validations get called at appropriate points around the save
user.rb validates takes place. Am I correct to say this.

simplest way to explain it. i guess.


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Thu, Feb 24, 2011 at 6:19 PM, Jim Ruther N. [email protected]
wrote:

wrote:

I click “sign up” -> form_for gets displayed -> I click “save” then in
user.rb validates takes place. Am I correct to say this.

simplest way to explain it. i guess.

Sorry, i did not get you. I said what I have learnt from the tutorial.
For
brevity, I skipped the controller parts.

On Thu, Feb 24, 2011 at 6:34 PM, Bhasker H. <
[email protected]> wrote:

I mean in which variable validates gets the value from,
The actual line in the program is

validates :uname, :presence => true,
:length => { :maximum => 50 }

Have a look at the Rails Guide ‘ActiveRecord Validations and Callback’.

On 24 February 2011 12:36, Bhasker H. [email protected]
wrote:

save the validations get called at appropriate points around the save
user.rb validates takes place. Am I correct to say this.
After you click ‘save’ it goes off to some controller action.
Somewhere in there it presumably calls @user.save
@user.update_attributes or one of the other ways that a record gets
saved. It is at this point that the validations get called.

Colin

On 24 February 2011 13:19, Bhasker H. [email protected]
wrote:


I mean in which variable validates gets the value from,
The actual line in the program is

validates :uname, :presence => true,
:length => { :maximum => 50 }

As I said that is not the line that does the validations, it just
registers that they are to be done. Did you work through the Rails
Guide as I suggested, that should make it clear.

The validation gets called when you do @user.save or something
similar. The attributes for both the validation and the save are in
@user in this case.

Colin

On Thu, Feb 24, 2011 at 6:50 PM, Colin L. [email protected]nvalid
wrote:



Colin

Yes, you are correct, I understand that. In my users_contoller.rb I
have,

def create
@user = User.new(params[:user])
# Handle a successful save.
if @user.save
redirect_to @user
else
@title = “Sign up”
render ‘new’
end
end

At that time the validates in the model user.rb gets triggered, am I
correct
to say that. It validates the fields based on the code like

validates :uun, :presence => true, length => { :maximum =>12},
:uniqueness
=> true
validates :uname, :presence => true, length => { :maximum => 64} etc…

after this I use puts “the name is #{uname}”. This has problem.

But if I do the puts in “before_save :encryt_password”. Then I have no
errors.

I just want to add some more fields before saving the record, that’s why
it
is important for me.

So, if I know in which variable validates gets the value, might be I can
also use it in the same manner.

Thanks & Regards,
Bhasker.

You received this message because you are subscribed to the Google
Groups

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs