Ruby on rails Rails 4:有时保存记录,有时不保存?
我有件奇怪的事发生了。我有一个模型调用配方,有很多:信息和:方向。当我试图保存@recipe时,请查看我的控制器及其许多:信息和:方向;如果我不修改源代码,它有时工作,有时不工作 此外,信息始终保存;问题似乎只与方向有关 这是我的控制器:Ruby on rails Rails 4:有时保存记录,有时不保存?,ruby-on-rails,database,controller,parameter-passing,Ruby On Rails,Database,Controller,Parameter Passing,我有件奇怪的事发生了。我有一个模型调用配方,有很多:信息和:方向。当我试图保存@recipe时,请查看我的控制器及其许多:信息和:方向;如果我不修改源代码,它有时工作,有时不工作 此外,信息始终保存;问题似乎只与方向有关 这是我的控制器: def new @recipe = current_user.recipes.new end def create @recipe = current_user.recipes.new(recipe_p
def new
@recipe = current_user.recipes.new
end
def create
@recipe = current_user.recipes.new(recipe_params)
if @recipe.save
redirect_to @recipe, notice: "Successfully created new recipe"
else
render 'new'
end
end
private
def recipe_params
params.require(:recipe).permit(:category_id, :title, :description, informations_attributes: [:id, :title, :url, :_destroy], directions_attributes: [:id, :title, :url, :step, :_destroy])
end
end
当我尝试从表单中保存时,以下是参数:
参数:{utf8=>✓,
真实性令牌=>wfa+wTI+C3HNVSCA1O922YLSJE3ZABEMMQMBMR+5KRB0B17FKWHSXSVAW1AFCOM8X11UGQGO6DRQUDGHDCVA==,
配方=>{title=>test,description=>test,category_id=>3,
信息属性=>{1431197959831=>{title=>test,
url=>dede,_destroy=>false},1431197959835=>{title=>ded,
url=>dede,_destroy=>false},
方向属性=>{1431197963709=>{title=>dede,
url=>dede,step=>de,_destroy=>false},
1431197963712=>{title=>ded,url=>ded,step=>ded,
_destroy=>false}},commit=>createtheme}
谢马:
create_table "directions", force: :cascade do |t|
t.text "step"
t.integer "recipe_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "url"
t.string "title"
end
create_table "information", force: :cascade do |t|
t.string "url"
t.integer "recipe_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "cached_votes_total", default: 0
t.integer "cached_votes_score", default: 0
t.integer "cached_votes_up", default: 0
t.integer "cached_votes_down", default: 0
t.integer "cached_weighted_score", default: 0
t.integer "cached_weighted_total", default: 0
t.float "cached_weighted_average", default: 0.0
t.string "title"
end
create_table "recipes", force: :cascade do |t|
t.string "title"
t.text "description"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
end
谢谢你的帮助 您的配方模型中是否有用于:信息、方向的嵌套属性?我相信在rails 4中不需要它,因为我们将参数与参数一起传递到控制器中。要求:配方。允许:类别id、标题、描述、信息属性:[id、标题、url、销毁]、方向属性:[:id,:title,:url,:step,:\u destroy]我认为这是必要的:说这是默认关闭Rails 4.2.1,说为了使用带有强参数的accepts_nested_attributes_,您需要指定哪些嵌套属性应该被列入白名单。好的,但不管怎样,我的记录都被正确保存,问题是有时候它没有任何原因就不能工作。注意,信息方法是有效的,方向有时会有问题。我不明白为什么有时它不记录。