【问题标题】:Custom Attribute Setter Method in Nested Forms嵌套表单中的自定义属性设置器方法
【发布时间】:2016-09-22 19:32:11
【问题描述】:

为了自学,我正在 Rails 5 上构建一个简单的评论者博客文章。它是一个视频游戏评论者,用户可以在其中撰写关于他们玩过的最近游戏的评论。用户还可以在评论中添加评论。

我想通过嵌套表单在我的游戏模型上实现自定义属性编写器。当用户第一次列出游戏时,我还希望他们能够当场为该游戏撰写评论。

Game.rb

class Game < ApplicationRecord
  has_many :reviews, dependent: :destroy
  has_many :users, through: :reviews

  validates :title, presence: true

  def reviews_attributes=(reviews_attributes)
    reviews_attributes.values.each do |review_attributes|
      self.reviews.build(review_attributes)
    end
  end
end

games/new.html.erb

<h1>Enter a new Game</h1>

<%= form_for @game do |f| %>
  <%= render 'shared/error_messages', object: @game %>
  <%= render 'new_form', f: f %>
  <br><br>
  Review:
  <br>
  <%= f.fields_for :reviews, @game.reviews.build do |r| %>
  <%= render 'reviews/form', f: r %>

  <%= f.submit "Add Game and/or Review!" %>
  <% end %>
<% end %>

评论/表格部分

  <%= f.label :title %>
  <%= f.text_field :title %>

  <br>
  <%= f.label :content %>
  <%= f.text_area :content %>

  <br>
  <%= f.label :score %>
  <%= f.text_field :score %>

  <%= f.hidden_field :user_id, :value => current_user.id %>

Games_Controller.rb

  def create
    @game = Game.new(game_params)
    if @game.save
      redirect_to @game
    else
      render :new
    end
  end

  private
    def game_params
      params.require(:game).permit(:title, :platform, reviews_attributes: [:rating, :content, :user_id])
    end

由于某种原因,每当我尝试通过我的嵌套表单创建与游戏关联的新评论时,我都会收到无效的评论。我的 error_messages 部分呈现错误消息:“1 个错误禁止保存此内容:评论无效”。

我猜关于评论表单或参数哈希中的数据的某些内容没有被传输。我不确定为什么。我什至尝试使用内置的 Rails 助手建立关联:accepts_nested_attributes_for,但我仍然遇到同样的错误。

为了清楚起见,这是我的仓库的链接:https://github.com/jchu4483/Rails-Assessment-

谢谢,感谢您提供任何帮助或建议。

【问题讨论】:

  • 尝试在你的强参数中添加:id到reviews_attributes:reviews_attributes: [:id, :rating, :content, :user_id]
  • 我刚试过,它给了我同样的错误。

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


【解决方案1】:

您在 game_params 中的评论属性中的属性似乎与表单上的属性不匹配。 Game_params 列出评分、内容、用户 ID。在表格中,您有标题、内容、分数。

【讨论】:

    【解决方案2】:

    现在我认为问题可能是因为嵌套表单与 has_many 和 through 关联。您的评论没有通过验证,因为它也加入了用户模型。您的 Review 模型应该有 accepts_nested_attributes_for User

    class Review < ApplicationRecord
      belongs_to :user
      belongs_to :game
    
      accepts_nested_attributes_for :user
    end
    

    你的表单应该有另一个fields_for给用户

    <%= form_for @game do |f| %>
       <%= f.fields_for :reviews do |r| %>
          <%= r.fields_for :users do |u| %>
             ...
          <% end %>
       <% end %>
       <%= f.submit %>
    <% end %>
    

    在game_params 的控制器中,您还需要为 users_attributes 传递一个数组

    def game_params
      params.require(:game).permit(:title, :platform, reviews_attributes: [:id, :rating, :content, :user_id, user_attributes: [...])
    end
    

    这个 SO 问题的答案可能会有所帮助:https://stackoverflow.com/a/21983998/5531936

    【讨论】:

    • 该错误是否为您提供了源文件和代码行号?你能说出错误到底指的是什么吗?
    • 实际上,没有 ActiveRecord 或 Rails 错误,我的 error_messages 部分正在呈现错误消息:“1 错误禁止保存此内容:评论无效”。我猜关于 Review 表单或 params 哈希中的数据的某些内容没有被传输。
    • hm,您可能应该在您的问题中发布。了解非常有帮助
    • 好的,刚刚根据您的建议更新了我的问题。
    • 但是 user_id 会自动在我的评论/表单中传输 current_user.id %>。所以你是说在我创建新游戏和评论的表单中,我还必须为用户创建一个新字段?
    猜你喜欢
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多