【问题标题】:Rails - why my posts form works different from comments form?Rails - 为什么我的帖子表单与评论表单不同?
【发布时间】:2016-01-07 23:37:16
【问题描述】:

posts_controller.rb:

 def create
    @post = current_user.posts.build(post_params)
    if @post.save
      flash[:success] = "Post created!"
      redirect_to root_url
    else
      render 'pages/home'
    end
      end

   private

    def post_params
      params.require(:post).permit(:content)
    end

发帖形式:

<%= form_for(@post) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Compose new post..." %>
  </div>
  <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>

cmets_controller.rb:

def create
    @post = Post.find(params[:comment][:post_id])
    @comment = @post.comments.build(comment_params)
    @comment.user = current_user
    if @comment.save
      flash[:success] = "Comment created!"
      redirect_to post_path(@post)
    else
            flash[:danger] = "Comment failed, try again."
            redirect_to post_path(@post)
        end
      end

  private

    def comment_params
      params.require(:comment).permit(:content)
    end

cmets 形式:

<%= form_for(@comment) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_area :content, placeholder: "Comment..." %>
  </div>
  <%= f.hidden_field :post_id , value: @post.id %>   // WHY DO I NEED THIS?
  <%= f.hidden_field :user_id , value: current_user.id %>  // AND THIS?
  <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>

为什么我需要隐藏字段才能使其与 cmets 一起使用,而帖子不需要它们?我的代码正在运行,我能够在谷歌上搜索一个解决方案,但仍然对 cme​​ts 与帖子的工作方式不同感兴趣。抱歉问了一个愚蠢的问题,但作为初学者,我正在寻找类比:)

【问题讨论】:

    标签: ruby-on-rails ruby forms ruby-on-rails-4


    【解决方案1】:

    这里的基本想法是,当您提交该评论时,它会将这两个 id 以及其他参数发布到您的 cmets create 操作。这样您就可以使用这些 id 来查找模型对象。

    但在你的情况下,你不需要用户 ID,因为它已经在 current_user 帮助器中可用。

    【讨论】:

    • 不知何故,如果没有隐藏字段,它就无法工作......我想知道为什么
    • 由于您已经有与评论相关联的帖子,我认为您也不需要它,您可以发布您收到的错误
    • ActiveRecord::RecordNotFound at /cmets 找不到没有 ID 的帖子
    【解决方案2】:

    首先,您不需要 current_user.id。当前用户通过视图和控制器是一致的,将其放入表单可能会造成安全威胁。 (好玩的用户可以代表其他用户发表评论)

    其次,您需要将 post_id 添加为隐藏字段以供评论,因为 post_params 基本上是标题/正文等,所有用户提交的值。但是,评论需要一个应用程序定义的值,即 post_id。查看 https://github.com/nathanvda/cocoon/wiki/A-guide-to-doing-nested-model-forms 以了解如何使用有助于您的一对多关联的嵌套表单。

    您需要通过posts_controller 中的更新操作来创建cmets。这样您就可以保持相同的模式并且您的代码保持干净。

    本教程应该对您有所帮助: http://www.sitepoint.com/complex-rails-forms-with-nested-attributes/

    【讨论】:

    • 添加:class Post &lt; ActiveRecord::Base belongs_to :user has_many :comments, dependent: :destroy ** accepts_nested_attributes_for :comments, :reject_if =&gt; :all_blank, :allow_destroy =&gt; true ** default_scope -&gt; { order(created_at: :desc) } validates :user_id, presence: true validates :content, presence: true endActiveRecord::RecordNotFound at /cmets 找不到没有 ID 的帖子
    • 您是否对表单/控制器进行了更改?
    • 不,如果没有通过隐藏字段发送的 id,它就无法工作
    • 看看这个控制器并查看:github.com/nathanvda/cocoon_simple_form_demo/blob/master/app/…。您需要遵循相同的模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2023-04-10
    • 2012-05-10
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多