TheIrishGuy
TheIrishGuy

Reputation: 2573

Difference between these two create methods in a users controller

Is there a difference between

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to root_url, :notice => "Signed up!"
  else
    render :new
  end
end

and

  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        format.html { redirect_to(:users, :notice => 'Registration successfull. Check your email for activation instructions.') }
        format.xml { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

Ignore the error and notice issues, my main question is the difference between using xml format and not using it, they seem to do the exact thing.

Upvotes: 1

Views: 142

Answers (1)

allaire
allaire

Reputation: 6045

Using respond_to with different format than html give you the ability to have the response in the specified format (useful for web-service).

In that case (User creation) I don't think it is really useful, but it's all up to you!

Not using respond_to like your first exemple will simply render html.

More infos about respond_to here:

http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to

Upvotes: 1

Related Questions