xinu
July 11, 2007, 11:42pm
1
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
xinu
July 11, 2007, 11:59pm
2
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
xinu
July 12, 2007, 12:07am
3
…
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’
xinu
July 12, 2007, 12:49am
4
What does your controller look like?
xinu
July 12, 2007, 7:54am
5
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
July 12, 2007, 10:36am
7
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)
?
xinu
July 12, 2007, 12:56pm
8
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|
xinu
July 12, 2007, 3:08pm
9
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.
xinu
July 12, 2007, 7:06pm
10
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
xinu
July 12, 2007, 12:04pm
11
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.