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| -%>
<p><label for="name">Username:</label><br/>
<%= f.text_field :name %></p>
<p><label for="email">Email</label><br/>
<%= f.text_field :email %></p>
<p><%= submit_tag ''add User'' %></p>
<%end -%>
def new
@user = User.new(params[:user])
return unless request.post?
if @user.save
redirect_to(:action => ''index'')
end
end
thanks
xinu
--
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
-~----------~----~----~----~------~----~------~--~---
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] =
"some-HMywgCz3680N+BqQ9rBEUg@public.gmane.org"
-javier godinez
On 7/11/07, Michael M.
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
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| -%>
> <p><label
for="name">Username:</label><br/>
> <%= f.text_field :name %></p>
>
> <p><label
for="email">Email</label><br/>
> <%= f.text_field :email %></p>
>
> <p><%= submit_tag ''add User'' %></p>
> <%end -%>
>
>
>
> def new
> @user = User.new(params[:user])
> return unless request.post?
> if @user.save
> redirect_to(:action => ''index'')
> end
> end
>
> thanks
> xinu
>
> --
> 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
-~----------~----~----~----~------~----~------~--~---
...> If you really want to modify whatever the user passed in, you can do > something like this: > params[:user][:email] = "some-HMywgCz3680N+BqQ9rBEUg@public.gmane.org" > > -javier godinezif 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'' -- 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 -~----------~----~----~----~------~----~------~--~---
What does your controller look like? On 7/11/07, Michael M. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > ... > > If you really want to modify whatever the user passed in, you can do > > something like this: > > params[:user][:email] = "some-HMywgCz3680N+BqQ9rBEUg@public.gmane.org" > > > > -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'' > > -- > 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 -~----------~----~----~----~------~----~------~--~---
xinu, def new # ... end Goes in your controller! On 7/11/07, Michael M. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > ... > > If you really want to modify whatever the user passed in, you can do > > something like this: > > params[:user][:email] = "some-HMywgCz3680N+BqQ9rBEUg@public.gmane.org" > > > > -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'' > > -- > 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 -~----------~----~----~----~------~----~------~--~---
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"=>"abc-gJEnWOaiUB4@public.gmane.org"}
I thing the problem is I, cannt read
"email"=>"abc-gJEnWOaiUB4@public.gmane.org" with
params[:user][:email]
--
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
-~----------~----~----~----~------~----~------~--~---
> I thing the problem is I, cannt read "email"=>"abc-gJEnWOaiUB4@public.gmane.org" with > params[:user][:email]what happens when you use params[''user''][''email''] (i.e, String rather Symbol) ? -- 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 -~----------~----~----~----~------~----~------~--~---
Michael M. wrote:> 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"=>"abc-gJEnWOaiUB4@public.gmane.org"} > > I thing the problem is I, cannt read "email"=>"abc-gJEnWOaiUB4@public.gmane.org" 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. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 7/12/07, Michael M. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 > >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| http://munichlinux.blogspot.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 -~----------~----~----~----~------~----~------~--~---
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| -%> > <p><label for="name">Username:</label><br/> > <%= f.text_field :name %></p> > > <p><label for="email">Email</label><br/> > <%= f.text_field :email %></p> > > <p><%= submit_tag ''add User'' %></p>> <%end -%>> > > > def new > @user = User.new(params[:user]) > return unless request.post? > if @user.save > redirect_to(:action => ''index'') > end > end > > thanks > xinuJust 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. -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James 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 -- 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 -~----------~----~----~----~------~----~------~--~---