如何在一种方法下组合这两个代码? (铁轨3)

时间:2021-01-21 12:19:12

I'm trying to combine these two codes inside the controller into one under "create"

我正在尝试将控制器内的这两个代码合并到一个“创建”下

class PostcommentsController < ApplicationController

   def create
    @micropost = Micropost.find(params[:micropost_id])
    @comment = Postcomment.new(params[:postcomment])
    @comment.micropost = @micropost
    @comment.user = current_user
    if @comment.save
       redirect_to(:back)
    else
      render partial: 'shared/_postcomment_form', locals: { micropost: @micropost }
    end
  end

  def create2
    @discussion = Discussion.find(params[:discussion_id])
    @comment = Postcomment.new(params[:postcomment])
    @comment.discussion = @discussion
    @comment.user = current_user
    if @comment.save
       redirect_to(:back)
    else
      render partial: 'shared/_postcomment_form', locals: { discussion: @discussion }
    end
  end
end

I tried both codes under "create" and they work. I have two models that I'm associating postcomment to: micropost and discussion. I want to apply the correct "create" depending on if it's a micropost or discussion.

我在“创建”下尝试了两个代码并且它们有效。我有两个模型,我将postcomment联系到:micropost和讨论。我想应用正确的“创建”,取决于它是微博还是讨论。

Here's what I'm using in view for discussion

这是我正在讨论的内容

<%= form_for([@discussion, @comment]) do |f| %>
<%= f.text_field :comment_content %>
<button class="btn" type="submit">
Comment
</button>
<% end %>

2 个解决方案

#1


3  

I'll give it a "blind" try cause I don't have access to my environment right now so it won't be tested code, let me know if it works or hangs somewhere...:

我会给它一个“盲目”的尝试因为我现在无法访问我的环境所以它不会被测试代码,让我知道它是否有效或挂在某处...:

def create
  model = params.has_key?(:micropost_id) ? Micropost : Discussion
  @object = model.find(params[model.to_s.foreign_key.to_sym])
  @comment = Postcomment.new(params[:postcomment])
  @comment.write_reflection(@object)
  @comment.user = current_user
  if @comment.save
     redirect_to(:back)
  else
    render partial: 'shared/_postcomment_form', locals: { model.to_s.downcase.to_sym => @object }
  end
end

And in your Comment model:

在您的评论模型中:

def write_reflection(object)
  if object.kind_of?(Micropost)
    self.micropost = object
  else
    self.discussion = object
  end
end

#2


0  

Sparda already answered this question, so I wont repeat his answer.

斯巴达已经回答了这个问题,所以我不会重复他的回答。

But I want to tell you that this is a very bad idea. It's not a good way of doing things. You should use Polymorphic Associations to represent the comments relationship with discussions and microposts.

但我想告诉你,这是一个非常糟糕的主意。这不是一种好的做事方式。您应该使用多态关联来表示与讨论和微博的评论关系。

You are setting yourself up for needless refactoring if things change in the future.

如果将来发生变化,你就会为不必要的重构做好准备。

You have to ask yourself what are you trying to achieve by combining the two actions? What if one day you add another model?

你必须问自己,你想通过结合这两个行动来实现什么目标?如果有一天你添加另一个模型怎么办?

def create
  model = params.has_key?(:micropost_id) ? Micropost : Discussion

def create
  model = case params
  when params.has_key?(:foo) then foo
  ... # where do you stop?

You also have to repeat this in your new action to create the correct objects and avoid nil class errors.

您还必须在新操作中重复此操作以创建正确的对象并避免出现nil类错误。

This is not a sane way of doing things.

这不是一种理智的做事方式。

Check this out: http://railscasts.com/episodes/154-polymorphic-association-revised (requires subscription)

看看这个:http://railscasts.com/episodes/154-polymorphic-association-revised(需要订阅)

Or this older but free episode: http://railscasts.com/episodes/154-polymorphic-association

或者这个较旧但免费的一集:http://railscasts.com/episodes/154-polymorphic-association

#1


3  

I'll give it a "blind" try cause I don't have access to my environment right now so it won't be tested code, let me know if it works or hangs somewhere...:

我会给它一个“盲目”的尝试因为我现在无法访问我的环境所以它不会被测试代码,让我知道它是否有效或挂在某处...:

def create
  model = params.has_key?(:micropost_id) ? Micropost : Discussion
  @object = model.find(params[model.to_s.foreign_key.to_sym])
  @comment = Postcomment.new(params[:postcomment])
  @comment.write_reflection(@object)
  @comment.user = current_user
  if @comment.save
     redirect_to(:back)
  else
    render partial: 'shared/_postcomment_form', locals: { model.to_s.downcase.to_sym => @object }
  end
end

And in your Comment model:

在您的评论模型中:

def write_reflection(object)
  if object.kind_of?(Micropost)
    self.micropost = object
  else
    self.discussion = object
  end
end

#2


0  

Sparda already answered this question, so I wont repeat his answer.

斯巴达已经回答了这个问题,所以我不会重复他的回答。

But I want to tell you that this is a very bad idea. It's not a good way of doing things. You should use Polymorphic Associations to represent the comments relationship with discussions and microposts.

但我想告诉你,这是一个非常糟糕的主意。这不是一种好的做事方式。您应该使用多态关联来表示与讨论和微博的评论关系。

You are setting yourself up for needless refactoring if things change in the future.

如果将来发生变化,你就会为不必要的重构做好准备。

You have to ask yourself what are you trying to achieve by combining the two actions? What if one day you add another model?

你必须问自己,你想通过结合这两个行动来实现什么目标?如果有一天你添加另一个模型怎么办?

def create
  model = params.has_key?(:micropost_id) ? Micropost : Discussion

def create
  model = case params
  when params.has_key?(:foo) then foo
  ... # where do you stop?

You also have to repeat this in your new action to create the correct objects and avoid nil class errors.

您还必须在新操作中重复此操作以创建正确的对象并避免出现nil类错误。

This is not a sane way of doing things.

这不是一种理智的做事方式。

Check this out: http://railscasts.com/episodes/154-polymorphic-association-revised (requires subscription)

看看这个:http://railscasts.com/episodes/154-polymorphic-association-revised(需要订阅)

Or this older but free episode: http://railscasts.com/episodes/154-polymorphic-association

或者这个较旧但免费的一集:http://railscasts.com/episodes/154-polymorphic-association