【问题标题】:Has_many through checkboxes (simple_form) not savingHas_many 通过复选框(simple_form)不保存
【发布时间】:2015-01-25 02:09:33
【问题描述】:

我正在尝试使用 has_many 将 cmets 排序到事件中:通过使用复选框的关联,但是未保存所选事件。这是我的模型:

class Comment < ActiveRecord::Base
has_many :categorizations 
has_many :events, :through => :categorizations     
end

class Event < ActiveRecord::Base
has_many :categorizations 
has_many :comments, :through => :categorizations    
end

class Categorization < ActiveRecord::Base
belongs_to :comment
belongs_to :event
end

我的 cmets 表单如下所示:

<%= simple_form_for [@post, @comment] do |f| %>
  <%= f.input :title %>
  <%= f.association :events, :as => :check_boxes %>
<%= f.submit "Save" %>

阅读 this answer 后,我将其添加到我的事件和评论控制器中,但没有运气:

def comment_params
  params.require(:comment).permit(:post_id, :title, :categorization_ids => [])
end

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    试试:

    def comment_params
      params.require(:comment).permit(:post_id, :title, :event_ids => [])
    end
    

    如果不重新创建它或查看服务器日志,很难知道发生了什么,希望这会解决它。

    【讨论】:

    • 如果你不介意我还有一个问题。评论和事件都属于帖子。关于我如何将表单中列出的事件限制为仅来自 cmets 帖子的任何线索?
    • 您需要在表单中传入一个集合并编写一个执行排序的范围
    • = f.ass..:事件,集合:scope_goes_here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2016-02-18
    • 2012-02-01
    • 2014-04-22
    • 2014-02-25
    • 2011-08-02
    相关资源
    最近更新 更多