Rodrigo Ruiz
2011-May-10 20:23 UTC
Help with ''new'' form code - ''render'' vs ''redirect_to''
Hi, I''m having some trouble to understand the following code: def new @user = User.new @title = "Sign up" end def create @user = User.new(params[:user]) if @user.save flash[:success] = "Welcome to the Sample App!" redirect_to @user else @title = "Sign up" @user.password = "" @user.password_confirmation = "" render ''new'' end end Since I do ''@user = User.new'' in the ''new'' method, why do i need to do it again ''@user = User.new(params[:user])'' instead of ''@user.update_attributes(params[:user])'' in the ''create'' method? Also I don''t get the difference between ''render'' and ''redirect_to'', what I read was that ''render'' doesn''t clean out the variables, whereas ''redirect_to'' does, so back to the first question... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-May-10 20:32 UTC
Re: Help with ''new'' form code - ''render'' vs ''redirect_to''
On 10 May 2011 21:23, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, I''m having some trouble to understand the following code: > def new > @user = User.new > @title = "Sign up" > end > > def create > @user = User.new(params[:user]) > if @user.save > flash[:success] = "Welcome to the Sample App!" > redirect_to @user > else > @title = "Sign up" > -eTHPNk8gMIsNG5+lYJWuzQ@public.gmane.org = "" > -eTHPNk8gMIsNG5+lYJWuzQ@public.gmane.org_confirmation = "" > render ''new'' > end > end > Since I do ''@user = User.new'' in the ''new'' method, why do i need to do it > again ''@user = User.new(params[:user])'' instead of > ''@user.update_attributes(params[:user])'' in the ''create'' method?Each action in the controller is a new request to the web server so the create action has no knowledge of @user set up in the new action. It can only go by what is in params.> Also I don''t get the difference between ''render'' and ''redirect_to'', what > I read was that ''render'' doesn''t clean out the variables, whereas > ''redirect_to'' does, so back to the first question...Render will re-show the ''new'' page, with the form for filling in the new user so the user can fix the errors @user still has the values from the data was submitted last time so the form will show the same data again. The redirect causes the ''show'' *action* to be run for the user, which results in that user being shown. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Walter Davis
2011-May-10 20:36 UTC
Re: Help with ''new'' form code - ''render'' vs ''redirect_to''
On May 10, 2011, at 4:23 PM, Rodrigo Ruiz wrote:> Hi, I''m having some trouble to understand the following code: > > def new > @user = User.new > @title = "Sign up" > end > > def create > @user = User.new(params[:user]) > if @user.save > flash[:success] = "Welcome to the Sample App!" > redirect_to @user > else > @title = "Sign up" > @user.password = "" > @user.password_confirmation = "" > render ''new'' > end > end > > Since I do ''@user = User.new'' in the ''new'' method, why do i need > to do it again ''@user = User.new(params[:user])'' instead of ''@user.update_attributes > (params[:user])'' in the ''create'' method?Because of the stateless nature of the HTTP protocol. The first request is for a form. The second request is a post of the filled form. In between those two, there could be moments or hours or days, and the server has long since lost interest in that object you create. In the New method, you use that User.new to spin up the form inputs, to set your defaults, and that''s about all. In the Create method, you create a completely different empty user, fill it with the result of the POST, validate those inputs, and save if it passes. It''s not really an update at all. Walter> Also I don''t get the difference between ''render'' and > ''redirect_to'', what I read was that ''render'' doesn''t clean out the > variables, whereas ''redirect_to'' does, so back to the first > question... > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 > .-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-May-10 21:17 UTC
Re: Help with ''new'' form code - ''render'' vs ''redirect_to''
On 10 May 2011, at 21:32, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 10 May 2011 21:23, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi, I''m having some trouble to understand the following code: >> def new >> @user = User.new >> @title = "Sign up" >> end >> >> def create >> @user = User.new(params[:user]) >> if @user.save >> flash[:success] = "Welcome to the Sample App!" >> redirect_to @user >> else >> @title = "Sign up" >> @user.password = "" >> @user.password_confirmation = "" >> render ''new'' >> end >> end >> Since I do ''@user = User.new'' in the ''new'' method, why do i need to do it >> again ''@user = User.new(params[:user])'' instead of >> ''@user.update_attributes(params[:user])'' in the ''create'' method? > > Each action in the controller is a new request to the web server so > the create action has no knowledge of @user set up in the new action. > It can only go by what is in params. > >> Also I don''t get the difference between ''render'' and ''redirect_to'', what >> I read was that ''render'' doesn''t clean out the variables, whereas >> ''redirect_to'' does, so back to the first question... > > Render will re-show the ''new'' page, with the form for filling in the > new user so the user can fix the errors @user still has the values > from the data was submitted last time so the form will show the same > data again. The redirect causes the ''show'' *action* to be run for the > user, which results in that user being shown. >To be complete explicit, calling render just tells rails which template to use, whereas redirect_to tells the browser that they should make a separate http request to the specified location Fred> Colin > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rodrigo Ruiz
2011-May-16 17:46 UTC
Re: Help with ''new'' form code - ''render'' vs ''redirect_to''
if it has no knowledge of @user in the ''new'' action, why do I need to do @user = User.new? What is the point, if it will be lost for other actions? I thought that all that i wrote with ''@'' in ruby was an instance variable, meaning it is accessible to the whole instance, meaning any of its methods. On Tue, May 10, 2011 at 4:32 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 10 May 2011 21:23, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi, I''m having some trouble to understand the following code: > > def new > > @user = User.new > > @title = "Sign up" > > end > > > > def create > > @user = User.new(params[:user]) > > if @user.save > > flash[:success] = "Welcome to the Sample App!" > > redirect_to @user > > else > > @title = "Sign up" > > @user.password = "" > > @user.password_confirmation = "" > > render ''new'' > > end > > end > > Since I do ''@user = User.new'' in the ''new'' method, why do i need to do > it > > again ''@user = User.new(params[:user])'' instead of > > ''@user.update_attributes(params[:user])'' in the ''create'' method? > > Each action in the controller is a new request to the web server so > the create action has no knowledge of @user set up in the new action. > It can only go by what is in params. > > > Also I don''t get the difference between ''render'' and ''redirect_to'', > what > > I read was that ''render'' doesn''t clean out the variables, whereas > > ''redirect_to'' does, so back to the first question... > > Render will re-show the ''new'' page, with the form for filling in the > new user so the user can fix the errors @user still has the values > from the data was submitted last time so the form will show the same > data again. The redirect causes the ''show'' *action* to be run for the > user, which results in that user being shown. > > Colin > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2011-May-16 17:51 UTC
Re: Help with ''new'' form code - ''render'' vs ''redirect_to''
On Mon, May 16, 2011 at 12:46 PM, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> if it has no knowledge of @user in the ''new'' action, why do I need to do > @user = User.new? What is the point, if it will be lost for other actions? > >@user is used in @user = User.new so that you have your new user object available for the form builder on the view.> I thought that all that i wrote with ''@'' in ruby was an instance variable, > meaning it is accessible to the whole instance, meaning any of its methods. >It is but as others have mentioned before, and maybe in other words: whatever you create only applies to the current request. The next time the browser hits the controller, it is fresh and you start over (and maybe use params or sessions to keep the state as needed).> > > On Tue, May 10, 2011 at 4:32 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> On 10 May 2011 21:23, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > Hi, I''m having some trouble to understand the following code: >> > def new >> > @user = User.new >> > @title = "Sign up" >> > end >> > >> > def create >> > @user = User.new(params[:user]) >> > if @user.save >> > flash[:success] = "Welcome to the Sample App!" >> > redirect_to @user >> > else >> > @title = "Sign up" >> > @user.password = "" >> > @user.password_confirmation = "" >> > render ''new'' >> > end >> > end >> > Since I do ''@user = User.new'' in the ''new'' method, why do i need to >> do it >> > again ''@user = User.new(params[:user])'' instead of >> > ''@user.update_attributes(params[:user])'' in the ''create'' method? >> >> Each action in the controller is a new request to the web server so >> the create action has no knowledge of @user set up in the new action. >> It can only go by what is in params. >> >> > Also I don''t get the difference between ''render'' and ''redirect_to'', >> what >> > I read was that ''render'' doesn''t clean out the variables, whereas >> > ''redirect_to'' does, so back to the first question... >> >> Render will re-show the ''new'' page, with the form for filling in the >> new user so the user can fix the errors @user still has the values >> from the data was submitted last time so the form will show the same >> data again. The redirect causes the ''show'' *action* to be run for the >> user, which results in that user being shown. >> >> Colin >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rodrigo Ruiz
2011-May-16 18:04 UTC
Re: Help with ''new'' form code - ''render'' vs ''redirect_to''
Now I think I got it, thank you. I need @user = User.new for the line ''form_for(@user)'' in the view ''new.html.erb'' right? And what is the point of making ''@user'' instead of ''user'' (local variable) if it will be lost anyway for other requests? On Mon, May 16, 2011 at 1:51 PM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote:> > > On Mon, May 16, 2011 at 12:46 PM, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > >> if it has no knowledge of @user in the ''new'' action, why do I need to do >> @user = User.new? What is the point, if it will be lost for other actions? >> >> > @user is used in @user = User.new so that you have your new user object > available for the form builder on the view. > > >> I thought that all that i wrote with ''@'' in ruby was an instance variable, >> meaning it is accessible to the whole instance, meaning any of its methods. >> > > It is but as others have mentioned before, and maybe in other words: > whatever you create only applies to the current request. The next time the > browser hits the controller, it is fresh and you start over (and maybe use > params or sessions to keep the state as needed). > > >> >> >> On Tue, May 10, 2011 at 4:32 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>wrote: >> >>> On 10 May 2011 21:23, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> > Hi, I''m having some trouble to understand the following code: >>> > def new >>> > @user = User.new >>> > @title = "Sign up" >>> > end >>> > >>> > def create >>> > @user = User.new(params[:user]) >>> > if @user.save >>> > flash[:success] = "Welcome to the Sample App!" >>> > redirect_to @user >>> > else >>> > @title = "Sign up" >>> > @user.password = "" >>> > @user.password_confirmation = "" >>> > render ''new'' >>> > end >>> > end >>> > Since I do ''@user = User.new'' in the ''new'' method, why do i need to >>> do it >>> > again ''@user = User.new(params[:user])'' instead of >>> > ''@user.update_attributes(params[:user])'' in the ''create'' method? >>> >>> Each action in the controller is a new request to the web server so >>> the create action has no knowledge of @user set up in the new action. >>> It can only go by what is in params. >>> >>> > Also I don''t get the difference between ''render'' and ''redirect_to'', >>> what >>> > I read was that ''render'' doesn''t clean out the variables, whereas >>> > ''redirect_to'' does, so back to the first question... >>> >>> Render will re-show the ''new'' page, with the form for filling in the >>> new user so the user can fix the errors @user still has the values >>> from the data was submitted last time so the form will show the same >>> data again. The redirect causes the ''show'' *action* to be run for the >>> user, which results in that user being shown. >>> >>> Colin >>> >>> -- >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >>> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Kahn
2011-May-16 18:22 UTC
Re: Help with ''new'' form code - ''render'' vs ''redirect_to''
On Mon, May 16, 2011 at 1:04 PM, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Now I think I got it, thank you. > > I need @user = User.new for the line ''form_for(@user)'' in the view > ''new.html.erb'' right? > > Correct> And what is the point of making ''@user'' instead of ''user'' (local variable) > if it will be lost anyway for other requests? >Try it and you will understand.... if you make it ''user'' the form wont see it. Remember, ''user'' inside a controller method is local to that method. If you have any class and want to make a variable visible for the whole class, not just the immediate method, you need to use @var (or another method). Think of the view as being in the same class as the controller but not inside the controller method.> > On Mon, May 16, 2011 at 1:51 PM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote: > >> >> >> On Mon, May 16, 2011 at 12:46 PM, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: >> >>> if it has no knowledge of @user in the ''new'' action, why do I need to do >>> @user = User.new? What is the point, if it will be lost for other actions? >>> >>> >> @user is used in @user = User.new so that you have your new user object >> available for the form builder on the view. >> >> >>> I thought that all that i wrote with ''@'' in ruby was an instance >>> variable, meaning it is accessible to the whole instance, meaning any of its >>> methods. >>> >> >> It is but as others have mentioned before, and maybe in other words: >> whatever you create only applies to the current request. The next time the >> browser hits the controller, it is fresh and you start over (and maybe use >> params or sessions to keep the state as needed). >> >> >>> >>> >>> On Tue, May 10, 2011 at 4:32 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>wrote: >>> >>>> On 10 May 2011 21:23, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> > Hi, I''m having some trouble to understand the following code: >>>> > def new >>>> > @user = User.new >>>> > @title = "Sign up" >>>> > end >>>> > >>>> > def create >>>> > @user = User.new(params[:user]) >>>> > if @user.save >>>> > flash[:success] = "Welcome to the Sample App!" >>>> > redirect_to @user >>>> > else >>>> > @title = "Sign up" >>>> > @user.password = "" >>>> > @user.password_confirmation = "" >>>> > render ''new'' >>>> > end >>>> > end >>>> > Since I do ''@user = User.new'' in the ''new'' method, why do i need to >>>> do it >>>> > again ''@user = User.new(params[:user])'' instead of >>>> > ''@user.update_attributes(params[:user])'' in the ''create'' method? >>>> >>>> Each action in the controller is a new request to the web server so >>>> the create action has no knowledge of @user set up in the new action. >>>> It can only go by what is in params. >>>> >>>> > Also I don''t get the difference between ''render'' and ''redirect_to'', >>>> what >>>> > I read was that ''render'' doesn''t clean out the variables, whereas >>>> > ''redirect_to'' does, so back to the first question... >>>> >>>> Render will re-show the ''new'' page, with the form for filling in the >>>> new user so the user can fix the errors @user still has the values >>>> from the data was submitted last time so the form will show the same >>>> data again. The redirect causes the ''show'' *action* to be run for the >>>> user, which results in that user being shown. >>>> >>>> Colin >>>> >>>> -- >>>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> To unsubscribe from this group, send email to >>>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> For more options, visit this group at >>>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>>> >>>> >>> -- >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rodrigo Ruiz
2011-May-16 19:42 UTC
Re: Help with ''new'' form code - ''render'' vs ''redirect_to''
Thank you. On Mon, May 16, 2011 at 2:22 PM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote:> > > On Mon, May 16, 2011 at 1:04 PM, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > >> Now I think I got it, thank you. >> >> I need @user = User.new for the line ''form_for(@user)'' in the view >> ''new.html.erb'' right? >> >> Correct > > >> And what is the point of making ''@user'' instead of ''user'' (local variable) >> if it will be lost anyway for other requests? >> > > Try it and you will understand.... if you make it ''user'' the form wont see > it. Remember, ''user'' inside a controller method is local to that method. If > you have any class and want to make a variable visible for the whole class, > not just the immediate method, you need to use @var (or another method). > Think of the view as being in the same class as the controller but not > inside the controller method. > >> >> On Mon, May 16, 2011 at 1:51 PM, David Kahn <dk-rfEMNHKVqOwNic7Bib+Ti1W1rNmOCjRP@public.gmane.org>wrote: >> >>> >>> >>> On Mon, May 16, 2011 at 12:46 PM, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: >>> >>>> if it has no knowledge of @user in the ''new'' action, why do I need to do >>>> @user = User.new? What is the point, if it will be lost for other actions? >>>> >>>> >>> @user is used in @user = User.new so that you have your new user object >>> available for the form builder on the view. >>> >>> >>>> I thought that all that i wrote with ''@'' in ruby was an instance >>>> variable, meaning it is accessible to the whole instance, meaning any of its >>>> methods. >>>> >>> >>> It is but as others have mentioned before, and maybe in other words: >>> whatever you create only applies to the current request. The next time the >>> browser hits the controller, it is fresh and you start over (and maybe use >>> params or sessions to keep the state as needed). >>> >>> >>>> >>>> >>>> On Tue, May 10, 2011 at 4:32 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>wrote: >>>> >>>>> On 10 May 2011 21:23, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>>> > Hi, I''m having some trouble to understand the following code: >>>>> > def new >>>>> > @user = User.new >>>>> > @title = "Sign up" >>>>> > end >>>>> > >>>>> > def create >>>>> > @user = User.new(params[:user]) >>>>> > if @user.save >>>>> > flash[:success] = "Welcome to the Sample App!" >>>>> > redirect_to @user >>>>> > else >>>>> > @title = "Sign up" >>>>> > @user.password = "" >>>>> > @user.password_confirmation = "" >>>>> > render ''new'' >>>>> > end >>>>> > end >>>>> > Since I do ''@user = User.new'' in the ''new'' method, why do i need >>>>> to do it >>>>> > again ''@user = User.new(params[:user])'' instead of >>>>> > ''@user.update_attributes(params[:user])'' in the ''create'' method? >>>>> >>>>> Each action in the controller is a new request to the web server so >>>>> the create action has no knowledge of @user set up in the new action. >>>>> It can only go by what is in params. >>>>> >>>>> > Also I don''t get the difference between ''render'' and >>>>> ''redirect_to'', what >>>>> > I read was that ''render'' doesn''t clean out the variables, whereas >>>>> > ''redirect_to'' does, so back to the first question... >>>>> >>>>> Render will re-show the ''new'' page, with the form for filling in the >>>>> new user so the user can fix the errors @user still has the values >>>>> from the data was submitted last time so the form will show the same >>>>> data again. The redirect causes the ''show'' *action* to be run for the >>>>> user, which results in that user being shown. >>>>> >>>>> Colin >>>>> >>>>> -- >>>>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>>> For more options, visit this group at >>>>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>>>> >>>>> >>>> -- >>>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> To unsubscribe from this group, send email to >>>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> For more options, visit this group at >>>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>>> >>> >>> -- >>> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to >>> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.