我正在开发一个具有以下模型的项目:
storyboards has many frames
frames has one of each puzzles and scenes
scenes
puzzles
我设置它以便框架与故事板相关联,但是我有拼图和场景分开存在。我想分别创建场景和谜题,然后在创建帧时将它们分配给帧。在框架的新形式中,我有两个集合,一个用于拼图,一个用于场景。我希望能够获取所选项目,查找该项目,并在相应的场景/谜题表中保存对帧编号的引用。但是,我一直得到一个空白的参数 我的控制器代码如下:
class FramesController < ApplicationController
before_action :set_storyboard
before_action :set_frame, only: [:show, :edit, :update, :destroy]
def new
@frame = @storyboard.frames.build
@puzzles = Puzzle.all
@scenes = Scene.all
end
def create
puts params.inspect
@frame = @storyboard.frames.build(frame_params)
@puzzle = Puzzle.find(frame_params[:puzzle])
if @frame.save
flash[:notice] = "Frame has been created."
redirect_to [@storyboard, @frame]
else
flash.now[:alert] = "Frame has not been created."
render "new"
end
end
def show
end
private
def set_storyboard
@storyboard = Storyboard.find(params[:storyboard_id])
end
def frame_params
params.require(:frame).permit(:frame_order)
end
def set_frame
@frame = @storyboard.frames.find(params[:id])
end
end
这还不完整,因为我还没有尝试找到场景,我正在尝试 让拼图查找首先工作。 在WEBrick中显示的参数如下:
Started POST "/storyboards/2/frames" for 127.0.0.1 at 2016-03-18
06:33:13 -0500
Processing by FramesController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"8KUEIEa7jVS2BsRFby8Cn8Hb0R5LM+paCNbKbfL5ielq01Q7IJlA3HtZX9+pLmYSPhRJq6xlhU2bR7umbv1T+w==",
"frame"=>{"frame_order"=>"1", "scene"=>"2", "puzzle"=>"1"},
"commit"=>"Create Frame", "storyboard_id"=>"2"}
我的_form.html.erb如下:
<%= simple_form_for([storyboard, frame]) do |f| %>
<%= f.input :frame_order, label: "Frame Order" %>
<%= f.input :scene, collection: @scenes %>
<%= f.input :puzzle, collection: @puzzles %>
<%= f.button :submit, class: "btn-primary" %>
我得到的错误如下:
Failure/Error: @puzzle = Puzzle.find(frame_params[:puzzle])
ActiveRecord::RecordNotFound:
Couldn't find Puzzle with 'id'=
我认为可能是因为我正在使用强力参数,但如果我添加 场景和拼图到frame_params,我也在创建时出错 方法如下:
Failure/Error: @frame = @storyboard.frames.build(frame_params)
ActiveRecord::AssociationTypeMismatch:
Scene(#50941880) expected, got String(#6701160)
从我能够查找的内容中,使用params [:frame] [:puzzle]应该可以工作,并且应该给我一个值1,但相反,我什么都没得到。我不确定我做错了什么。任何帮助或建议将不胜感激。谢谢!
答案 0 :(得分:1)
允许在frame_params中拼图:
def frame_params
params.require(:frame).permit(:frame_order,:puzzle)
end