这些创建方法之间的区别? Ruby on Rails

时间:2018-06-22 00:34:59

标签: ruby-on-rails ruby

我有一个博客,正在通过以下代码创建评论。我注意到两者的工作方式(似乎)完全相同。

以下两种在视图中调用此创建方法的方式是否有优缺点?有更多可用的方法来调用此类事件吗?

PostComment通过has_manybelongs_to关系连接。

  1. <%= simple_form_for([@post, Comment.new]) do |f| %>

  2. <%= simple_form_for([@post, @post.comments.build]) do |f| %>

这是我的comment_controller:

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.create(comment_params)
  if @comment.save
    flash[:success] = "Comment created!"
    redirect_to post_path(@post)
  else
    flash[:danger] = "Error"
    redirect_to post_path(@post)
  end
end

1 个答案:

答案 0 :(得分:2)

double *existingArray = (double*)malloc(sizeof(double)*10); jl_array_t *x = jl_ptr_to_array_1d(array_type, existingArray, 10, 0); .new之间没有真正的区别,因为.buildbuild的别名。

您还可以将newbuild放在您的new控制器操作中:

new

然后只需在表单中使用实例变量:

def new
  @post = Post.new
  @comment = @post.comment.build
end
相关问题