未保存复选框项目 (simple_form)
Checkbox items not saving (simple_form)
另一个更新:这是我的开发日志。我对两件事感到困惑。
- "geography_ids"从哪里来(不允许的参数:geography_ids)。我搜索了我的整个项目,它出现的唯一地方是在日志文件中。我认为这是问题的根源。
- 似乎 simple_form 试图添加第四项,从空双引号判断。
这是日志文件:
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" = 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" = LIMIT 1 [["id", 11]]
[1m[36mIssue Load (0.2ms)[0m [1mSELECT "issues".* FROM "issues" WHERE "issues"."id" = 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 table,这意味着在保存拆分时复选框无法保存它们的状态。
这几天我一直在绞尽脑汁。
这是我的模型:
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 指南时,它说要创建第三个 table 本质上将这些协会连接在一起。
所以我运行这次迁移:
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
我完全被难住了。
非常感谢所有帮助。
好的,使用 this post 我能够修复它。
我需要在拆分控制器中将 { geography_ids:[] } 添加到我的白名单中。
感谢您的帮助。
另一个更新:这是我的开发日志。我对两件事感到困惑。
- "geography_ids"从哪里来(不允许的参数:geography_ids)。我搜索了我的整个项目,它出现的唯一地方是在日志文件中。我认为这是问题的根源。
- 似乎 simple_form 试图添加第四项,从空双引号判断。
这是日志文件:
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" = 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" = LIMIT 1 [["id", 11]]
[1m[36mIssue Load (0.2ms)[0m [1mSELECT "issues".* FROM "issues" WHERE "issues"."id" = 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 table,这意味着在保存拆分时复选框无法保存它们的状态。
这几天我一直在绞尽脑汁。
这是我的模型:
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 指南时,它说要创建第三个 table 本质上将这些协会连接在一起。
所以我运行这次迁移:
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
我完全被难住了。
非常感谢所有帮助。
好的,使用 this post 我能够修复它。
我需要在拆分控制器中将 { geography_ids:[] } 添加到我的白名单中。
感谢您的帮助。