复选框项目未保存(simple_form)

时间:2015-09-02 19:59:49

标签: ruby-on-rails simple-form

另一个更新:这是我的开发日志。我对两件事感到困惑。

  1. “geography_ids”来自哪里(未经许可的参数:geography_ids)。我搜索了我的整个项目,它出现的唯一地方就是在日志文件中。我认为这是问题的根源。
  2. 似乎simple_form正在尝试添加从空双引号判断的第四项。
  3. 这是日志文件:

    Started PATCH "/splits/11" for 127.0.0.1 at 2015-09-03 10:28:38 -0500
    Processing by SplitsController#update as HTML
      Parameters: {"utf8"=>"✓", "authenticity_token"=>"h5/1qn3KJm8lktOjNGvEH/POZHqx8msCIwtoi42ZYVtbEimbyPiEPwDgdIwzrfBYDZZPPxku0uj7flcsRf6b9g==", "split"=>{"name"=>"Domestic Canada Foreign", "description"=>"", "geography_ids"=>["1", "2", "3", ""], "issue_id"=>"1"}, "commit"=>"Update Split", "id"=>"11"}
      [1m[36mSplit Load (0.3ms)[0m  [1mSELECT  "splits".* FROM "splits" WHERE "splits"."id" = $1 LIMIT 1[0m  [["id", 11]]
    Unpermitted parameter: geography_ids
      [1m[35m (0.1ms)[0m  BEGIN
      [1m[36m (0.1ms)[0m  [1mCOMMIT[0m
    Redirected to http://phoenix.dev/splits/11
    Completed 302 Found in 4ms (ActiveRecord: 0.4ms)
    
    
    Started GET "/splits/11" for 127.0.0.1 at 2015-09-03 10:28:38 -0500
    Processing by SplitsController#show as HTML
      Parameters: {"id"=>"11"}
      [1m[35mSplit Load (0.2ms)[0m  SELECT  "splits".* FROM "splits" WHERE "splits"."id" = $1 LIMIT 1  [["id", 11]]
      [1m[36mIssue Load (0.2ms)[0m  [1mSELECT  "issues".* FROM "issues" WHERE "issues"."id" = $1 LIMIT 1[0m  [["id", 1]]
      Rendered splits/show.html.erb within layouts/application (1.4ms)
    Completed 200 OK in 610ms (Views: 608.6ms | ActiveRecord: 0.4ms)
    
    
    Started GET "/assets/user-2.jpg" for 127.0.0.1 at 2015-09-03 10:28:39 -0500
    
    
    Started GET "/assets/user-13.jpg" for 127.0.0.1 at 2015-09-03 10:28:39 -0500
    
    
    Started GET "/assets/user-1.jpg" for 127.0.0.1 at 2015-09-03 10:28:39 -0500
    

    更新:我用我最近的更改更新了我的模型和控制器。我发现了一些复杂的细微差别。但是,根本问题仍然存在。数据不会进入geographies_splits表,这意味着在保存拆分时复选框无法保存其状态。

    我这几天一直在讨厌这个问题。

    以下是我的模特:

    class Split < ActiveRecord::Base
      belongs_to :issue
      has_and_belongs_to_many :geographies 
    end
    
    
    lass Geography < ActiveRecord::Base
      has_and_belongs_to_many :splits
    end
    

    当我阅读关于has_and_belongs_to_many关联的Rails指南时,它说要创建一个基本上将这些连接在一起的第三个表。

    所以我进行了这次迁移:

    class CreateGeographiesSplits < ActiveRecord::Migration
      def change
        create_table :geographies_splits do |t|
          t.belongs_to :geography, index: true
          t.belongs_to :split, index: true
    
          t.timestamps null: false
        end
     end
    end
    

    它没有说创建模型或控制器,所以我最初没有。在继续努力的同时,我确实创建了一个控制器和模型。

    我正在使用simple_form来显示我的复选框,并且它们正确显示。但是,当我创建拆分时,带有地理内容的复选框不会保存到数据库中,当我回到拆分时,它们将被取消选中。

    以下是创建拆分的表单代码,我试图说明它的地理位置:

    <%= simple_form_for(@split) do |f| %>
      <%= f.error_notification %>
    
      <div class="form-inputs form-width">
        <%= f.input :name %>
        <%= f.input :description %>
        <%= f.association :geographies, as: :check_boxes %>
        <%= f.association :issue, label_method: :name, value_method: :id, include_blank: true %>
      </div>
    
      <div>
        <p>More robust Query UI here</p>
      </div>
    
    <div>
      <p>Select printer UI here</p>
    </div>
    
    
      <div class="form-actions">
        <%= f.button :submit, :class => 'btn btn-primary btn-small' %>
        <%= link_to 'Cancel', splits_path, :class => 'btn btn-default btn-small'  %>
      </div>
    
    
    <% end %>
    

    这是Splits控制器:

        class SplitsController < ApplicationController
      before_action :set_split, only: [:show, :edit, :update, :destroy]
    
      # GET /splits
      # GET /splits.json
      def index
        @splits = Split.all
      end
    
      # GET /splits/1
      # GET /splits/1.json
      def show
      end
    
      # GET /splits/new
      def new
        @split = Split.new
      end
    
      # GET /splits/1/edit
      def edit
      end
    
      # POST /splits
      # POST /splits.json
      def create
        @split = Split.new(split_params)
    
        respond_to do |format|
          if @split.save
            format.html { redirect_to @split, notice: 'Split was successfully created.' }
            format.json { render :show, status: :created, location: @split }
          else
            format.html { render :new }
            format.json { render json: @split.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # PATCH/PUT /splits/1
      # PATCH/PUT /splits/1.json
      def update
        respond_to do |format|
          if @split.update(split_params)
            format.html { redirect_to @split, notice: 'Split was successfully updated.' }
            format.json { render :show, status: :ok, location: @split }
          else
            format.html { render :edit }
            format.json { render json: @split.errors, status: :unprocessable_entity }
          end
        end
      end
    
      # DELETE /splits/1
      # DELETE /splits/1.json
      def destroy
        @split.destroy
        respond_to do |format|
          format.html { redirect_to splits_url, notice: 'Split was successfully destroyed.' }
          format.json { head :no_content }
        end
      end
    
      private
        # Use callbacks to share common setup or constraints between actions.
        def set_split
          @split = Split.find(params[:id])
        end
    
        # Never trust parameters from the scary internet, only allow the white list through.
        def split_params
          params.require(:split).permit(:name, :description, :geography_id, :issue_id, :geographies_splits_id)
          end
        end
    

    我完全难过了。

    非常感谢所有帮助。

1 个答案:

答案 0 :(得分:1)

好的,使用this post我能够修复它。

我需要将{geography_ids:[]}添加到拆分控制器中的白名单。

感谢您的帮助。