Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用RubyonRails更新模型_Ruby On Rails - Fatal编程技术网

Ruby on rails 使用RubyonRails更新模型

Ruby on rails 使用RubyonRails更新模型,ruby-on-rails,Ruby On Rails,我用post方法获得一个参数的值 Parameters: {"utf8"=>"✓", "authenticity_token"=>"88f1KdoixHkPXIQJs4Vmg0kj9Sxrc+eM0DGSbD0B1xs=", "adverts"=>{"title"=>"fdasfa", "features"=>"", "description"=>"", "areadescription"=>"", "rooms"=>"", "bathroom"=

我用post方法获得一个参数的值

Parameters: {"utf8"=>"✓", "authenticity_token"=>"88f1KdoixHkPXIQJs4Vmg0kj9Sxrc+eM0DGSbD0B1xs=", "adverts"=>{"title"=>"fdasfa", "features"=>"", "description"=>"", "areadescription"=>"", "rooms"=>"", "bathroom"=>"", "price"=>"", "type"=>"", "source"=>"", "originaldate"=>""}, "commit"=>"Save Adverts"}
以及获得特定值:

@advert = params[:adverts]
Adverte与名为advertTable的表中的字段的名称完全相同。
如何将advert变量解析为与我的模型advertTable相同的时间?

也许你应该在教程中学习一下
为了把变量传递给一个对象,你必须告诉它是什么对象。。。然后将参数传递给对象。所以

@advert=Advert.new(params[:adverts])
@advert.save
然而,这不是一个好的做法。。。对于更新和创建,应使用强参数。这样,坏人就不会更新id列或其他非公共列

最好的方法是:

@advert=Advert.new(advert_params)
@advert.save
在更新操作中:

@advert=Advert.find(params[:id]) #this comes from the url
@advert.update(advert_params)
在同一控制器中的“专用”部分下:


谢谢我需要添加
私有def广告参数
现在可以工作了!
private
def advert_params
    params.require(:adverts).permit(:title, :features, :description, :areadescription, :rooms, :bathroom, :price, :type, :source, :originaldate)
end