Read params

hi.

I am a rails newbie.

I have am Model user with name and email. How can I acces and modify the
param email before user.save?

<% form_for :user do |f| -%>

Username:
<%= f.text_field :name %>

Email
<%= f.text_field :email %>

<%= submit_tag 'add User' %>

<%end -%>

def new
@user = User.new(params[:user])
return unless request.post?
if @user.save
redirect_to(:action => ‘index’)
end
end

thanks
xinu

If what you want is to verify that the email is unique/valid, put
these two lines in your model.

validates_format_of :email, :with =>
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i, :message => “Invalid
email”
validates_presence_of :email

If you really want to modify whatever the user passed in, you can do
something like this:
params[:user][:email] = “[email protected]

-javier godinez

If you really want to modify whatever the user passed in, you can do
something like this:
params[:user][:email] = “[email protected]

-javier godinez

if I try this I receive following error message:

NoMethodError in UserController#new
You have a nil object when you didn’t expect it!

You might have expected an instance of Array.
The error occurred while evaluating nil.[]=

RAILS_ROOT: script/…/config/…
Application Trace | Framework Trace | Full Trace
app/controllers/user_controller.rb:8:in `new’

What does your controller look like?

this is my controller.

class UserController < ApplicationController

def index
end

def new
@user = User.new(params[:user])
return unless request.post?
if @user.save
redirect_to(:action => ‘index’)
end
end

def edit
end

def list
end

end

it works, but when I add the line @user.name = params[:user][:email] I
receive the error message.

puts params[:user].inspect
{“name”=>“abc”, “email”=>“[email protected]”}

I thing the problem is I, cannt read “email”=>“[email protected]” with
params[:user][:email]

xinu,

def new

end

Goes in your controller!

I thing the problem is I, cannt read “email”=>“[email protected]” with
params[:user][:email]

what happens when you use

params[‘user’][‘email’]

(i.e, String rather Symbol)
?

On 7/12/07, Michael M. [email protected] wrote:

return unless request.post?
  if @user.save
  redirect_to(:action => 'index')
end

end

I think you are trying to access both the user and the email and store
in the user table right? if that is the case you can do something like
this User.new(“user” => params[:user], email => params[:email]).


regards,

|Prashanth|

Michael M. wrote:

hi.

I am a rails newbie.

I have am Model user with name and email. How can I acces and modify the
param email before user.save?

<% form_for :user do |f| -%>

Username:
<%= f.text_field :name %>

Email
<%= f.text_field :email %>

<%= submit_tag 'add User' %>

<%end -%>

def new
@user = User.new(params[:user])
return unless request.post?
if @user.save
redirect_to(:action => ‘index’)
end
end

thanks
xinu

Just to be sure that the problem doesn’t come from another side could
you correct redirect_to into:

             redirect_to(:action => 'index') and return

(you must be sure not to have a double rendering in your flow, one from
the redirect_to and the other at the end of the method which defaults to
a

                render :action => 'new'

)

Don’t know if it can help.

Roberto Gattinoni.

Mark Reginald J. wrote:

You’ve got to make sure you only access params[:user][:email]
on a post, because params[:user] will be nil on a get, and
you’ll get an error.

Thanks to all for your help.
If i move the line "return unless request.post? " to the top of the
function it works well.

def new
return unless request.post? #moved to here
@user = User.new(params[:user])
if @user.save
redirect_to(:action => ‘index’) and return
end

Thanks!
Michael

Michael M. wrote:

  if @user.save

end

it works, but when I add the line @user.name = params[:user][:email] I
receive the error message.

puts params[:user].inspect
{“name”=>“abc”, “email”=>“[email protected]”}

I thing the problem is I, cannt read “email”=>“[email protected]” with
params[:user][:email]

You’ve got to make sure you only access params[:user][:email]
on a post, because params[:user] will be nil on a get, and
you’ll get an error.


We develop, watch us RoR, in numbers too big to ignore.