Ruby on rails 有多个带有集合复选框的联接表单,但未保存多个复选框值

Ruby on rails 有多个带有集合复选框的联接表单,但未保存多个复选框值,ruby-on-rails,join,checkbox,nested-attributes,form-for,Ruby On Rails,Join,Checkbox,Nested Attributes,Form For,我正在为编辑日历应用程序制作表单。我有两件事很相似,但都不起作用 使用3种模式:平台、帖子和日历。它们是连接表。平台发布,发布日历 Post/new & Post/edit form: <div class="container"> <div class="form-field"> <%= form_for @post do |f| %> <%= f.label :title %> <%= f.te

我正在为编辑日历应用程序制作表单。我有两件事很相似,但都不起作用

使用3种模式:平台、帖子和日历。它们是连接表。平台发布,发布日历

Post/new & Post/edit form:
    <div class="container">
  <div class="form-field">
    <%= form_for @post do |f| %>
    <%= f.label :title %>
    <%= f.text_field :title, required: true %> <br>
    Title is required.
  </div>

  <div class="form-field">
    <%= f.label :content%>
    <%= f.text_area :content%>
  </div>

  <div class="form-field">
    <%= f.label :link %>
    <%= f.text_field :link %>
  </div>

  <div class="file-field">
    <%= f.label :picture%>
    <%= f.file_field :picture, id: :post_picture%>
  </div>

  <div class="file-field">
    <%= f.label :finalized %>
    <%= f.radio_button :finalized , true%>
    <%= f.label :finalized, "Yes" %>
    <%= f.radio_button :finalized, false %>
    <%= f.label :finalized, "No" %>
  </div>

  <%= f.hidden_field :user_id %> <br>

  <div class="form-field">
    <%= f.fields_for :platform_attributes do |platform| %>
    <%= platform.label :platform, "Social Platforms"%>
    <%= platform.collection_check_boxes :platform_ids, Platform.all, :id, :name %> <br> <br>
  </div>
  <div>
    <h4> Or Create a new platform: </h4>
    <%= platform.label :platform, 'New Platform'%>
    <%= platform.text_field :name%> <br> <br>
  </div>
  <% end %>


  <%= f.submit%>

  <% end %>
</div>
发布/新建&发布/编辑表单:

标题是必需的。


或者创建一个新平台:

我的邮政管理员正在处理复选框问题和“日程安排”问题。它只允许我安排一个日历,不保存更新和添加其他日历

 Posts Controller:
    class PostsController < ApplicationController
      before_action :set_post, only: [:show, :edit, :update, :schedule_post, :destroy]

    def new
        @posts = current_user.posts.select {|p| p.persisted?}
        @post = current_user.posts.build
        @platforms = Platform.all
      end

      def edit
        @calendars = current_user.calendars
        @platforms = Platform.all
      end

      def create
        @post = current_user.posts.build(post_params)
        if @post.save
          redirect_to post_path(@post)
        else
          redirect_to new_post_path
        end
      end

      def update
        @post.update(post_params)
        if @post.save
          redirect_to post_path(@post), notice: 'Your post has been updated.'
        else
          redirect_to edit_post_path(@post)
        end
      end

      def schedule_post
        @calendar_post = CalendarPost.new(calendar_post_params)
        if @calendar_post.save
          binding.pry
          redirect_to post_path(@post)
        else
          render 'show'
        end
      end

      private
      def set_post
        @post = Post.find(params[:id])
      end

      def set_calendars
        @calendars = current_user.calendars
      end

      def post_params
        params.require(:post).permit(:title, :content, :link, :finalized, :picture, :user_id, :platform_attributes => [:platform_ids, :name])
      end

      def calendar_post_params
        params.require(:calendar_post).permit(:post_id, :calendar_id, :date, :time)
      end
    end
Posts控制器:
类PostsController<应用程序控制器
在执行以下操作之前:设置发布,仅:[:显示,:编辑,:更新,:计划发布,:销毁]
def新
@posts=当前_user.posts.select{| p | p.persistend?}
@post=当前_user.posts.build
@platforms=Platform.all
结束
定义编辑
@日历=当前用户日历
@platforms=Platform.all
结束
def创建
@post=当前用户.posts.build(post参数)
如果@post.save
将_重定向到post_路径(@post)
其他的
将\u重定向到新的\u post\u路径
结束
结束
def更新
@post.update(post_参数)
如果@post.save
将_重定向到post_路径(@post),注意:“您的帖子已更新。”
其他的
重定向到编辑帖子路径(@post)
结束
结束
def调度室员额
@calendar\u post=CalendarPost.new(calendar\u post\u参数)
如果@calendar\u post.save
捆绑,撬动
将_重定向到post_路径(@post)
其他的
渲染“显示”
结束
结束
私有的
def set_柱
@post=post.find(参数[:id])
结束
def set_日历
@日历=当前用户日历
结束
def post_参数
参数require(:post).permit(:title,:content,:link,:finalized,:picture,:user\u id,:platform\u attributes=>[:platform\u id,:name])
结束
def calendar_post_参数
参数require(:calendar\u post).permit(:post\u id,:calendar\u id,:date,:time)
结束
结束
我希望用户能够添加一个帖子到多个平台和多个日历,因为有人可能需要的多功能性

在我的Post模型中也有我的setter

class Post < ApplicationRecord
  has_many :calendar_posts
  has_many :calendars, through: :calendar_posts
  has_many :platform_posts
  has_many :platforms, through: :platform_posts
  belongs_to :user




 def platform_attributes=(platform_attributes)
        if platform_attributes['platform_ids']
          platform_attributes.platform_ids.each do |id|
            platform = Platform.find(id: id)
            self.platforms << platform
          end
        end
        if platform_attributes['name'] != ""
          platform = Platform.find_or_create_by(name: platform_attributes['name'])
          self.platforms << platform
        end
      end
class Post
  def post_params
    params.require(:post).permit(:title, :content, :link, :finalized, :picture, :user_id, :platform_ids, :platform_attributes => [:name])
  end

注意:未测试-错误留给读者作为练习;)

嗨。您能否查看一下您的输出(控制台或日志文件),并向我们展示当您在选择了多个平台的情况下发布此表单时,作为参数的内容?这通常可以帮助我们找出问题所在。还有-该表单生成了什么html?注意:请编辑您的问题并在此处添加新代码,而不是在注释中发布,因为注释中的代码格式几乎不可读:P cheers:)另外:我注意到您的permit/require方法中没有
:platform\u id
。。。您可能需要它。只需编辑上面的内容。我试图弄明白为什么提交按钮不起作用,一旦我能做到这一点,我就可以把其他信息传过来。如果你看到一些可能阻止按钮提交的东西,请告诉我。谢谢你提供的信息!我得到这个来处理这些更改,以及对setter/getter属性方法的一些附加更改,以及控制器中的更改。
<div class="form-field">
    <%= f.label :platform_ids, "Social Platforms"%>
    <%= f.collection_check_boxes :platform_ids, Platform.all, :id, :name %> <br> <br>
  </div>
  <div>
    <%= f.fields_for :platform_attributes do |platform| %>
     <h4> Or Create a new platform: </h4>
      <%= platform.label :name, 'New Platform'%>
      <%= platform.text_field :name%> <br> <br>
    <% end %>
    <%# end fields_for %>
  </div>
  def post_params
    params.require(:post).permit(:title, :content, :link, :finalized, :picture, :user_id, :platform_ids, :platform_attributes => [:name])
  end