On 10 May 2011, at 21:32, Colin Law <clan...@googlemail.com> wrote:

> On 10 May 2011 21:23, Rodrigo Ruiz <rodrigo.ru...@gmail.com> 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@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> 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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to