这两个创建方法在用户控制器中的区别

时间:2021-06-14 21:28:34

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.

忽略错误并注意问题,我的主要问题是使用xml格式与不使用它之间的区别,它们似乎做了确​​切的事情。

1 个解决方案

#1


1  

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

使用不同于html格式的respond_to使您能够以指定的格式获得响应(对Web服务很有用)。

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.

不像你的第一个例子那样使用respond_to只会渲染html。

More infos about respond_to here:

关于respond_to的更多信息:

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

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

#1


1  

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

使用不同于html格式的respond_to使您能够以指定的格式获得响应(对Web服务很有用)。

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.

不像你的第一个例子那样使用respond_to只会渲染html。

More infos about respond_to here:

关于respond_to的更多信息:

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

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