【发布时间】:2015-09-02 19:59:48
【问题描述】:
另一个更新:这是我的开发日志。我对两件事感到困惑。
- “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" = $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 %>
这是拆分控制器:
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
我完全被难住了。
非常感谢所有帮助。
【问题讨论】:
-
向我们展示您的控制器。特别是,您是否允许复选框值?另外,在有问题的控制器操作的顶部捕获
params,以确认它们是从视图中发送的。使用相关视图更新您的帖子也没有什么坏处。 -
您的关联不需要单独的控制器。你能告诉我们失败的创建操作的控制器吗?我想这将是拆分控制器。此外,可能没有像您期望的那样在
params中传递复选框,或者它们是不允许的。您应该能够从您的日志和一些调试中确定它是哪个。 -
我只为关联添加了一个控制器,因为我认为我需要将进入该表的 id 列入白名单。
-
控制器只是将视图连接到模型。如果您在视图中创建关联,则可以通过关联将参数列入白名单。查看this Rails Guide 中的第 4.5 节强参数了解更多详细信息。
-
我想我发现了问题。我的日志文件显示“未经许可的参数:geography_ids”。但是我搜索了我的项目,唯一出现的地方是在日志文件中。我试图弄清楚那是从哪里来的。它应该只是“geography_id”