Ruby on rails 带有复选框的表单有效,不带-dons';T
一个控制器:Ruby on rails 带有复选框的表单有效,不带-dons';T,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,一个控制器: class ProductsController < ApplicationController def create flat = Flat.find(params[:flat_id]) @flat = FlatDecorator.new(flat) @product = flat.products.new(product_params) if @product.save redirect_to flat else
class ProductsController < ApplicationController
def create
flat = Flat.find(params[:flat_id])
@flat = FlatDecorator.new(flat)
@product = flat.products.new(product_params)
if @product.save
redirect_to flat
else
render 'flats/show'
end
end
private
def product_params
params.require(:product).permit([:name, :flatmate_id, :present, :flat_id])
end
end
class FlatmatesController < ApplicationController
def create
flat = Flat.find(params[:flat_id])
@flat = FlatDecorator.new(flat)
@flatmate = flat.flatmates.new(flatmate_params)
if @flatmate.save
redirect_to flat
else
render 'flats/show'
end
end
private
def flatmate_params
params.require(:flatmate).permit(:name)
end
end
在第二个表单的表单上。调试时,@flatmate
为nil
,@flat
正常
选中复选框后,一切正常
我做错了什么?在Rails中调用render
时,它实际上并没有调用指定的方法,只是渲染它的视图。这意味着它使用的实例变量(@flat、@flatmate、@product等)没有被设置在任何地方,因此被解释为nil
当到达渲染调用时,需要确保定义了视图使用的所有实例变量
注意:看起来您正在使用“flats/show”作为更改相关对象的表单。这在rails中是不寻常的,我建议使用嵌套路线,并将室友、产品等放在平板下。然后将其移动到一个特定于平面的编辑表单,用于这些内容的集合
resources :flats do
resources :flatmates do
collection do
get :edit
post :update
end
end
end
您是在为@flat
或@flatmate
建造表格吗?您的视图属于哪个操作
控制器
?
First argument in form cannot contain nil or be empty
resources :flats do
resources :flatmates do
collection do
get :edit
post :update
end
end
end