表单字段未保存到数据库中 - ruby rails
Form fields are not saved into database - ruby on rails
我这里有一个表格https://pastebin.com/JfXr054y
在routes.rb
我有
resources :landslides, only: [:new, :create, :index]
在landslides_controller.rb
我有
def new
@landslide = Landslide.new(landslide_params)
@landslide.save
render plain: @landslide.inspect
end
和
def landslide_params
params.require(:landslide).permit(:total_id, :year_id, :start_date, :end_date, :day_number, :continent, :country, :location, :type, :admin_level, :new_lat, :new_long, :mapped, :spatial_area, :fatalities, :injuries, :notes, :sources)
end
为什么表格没有保存到 table?
new
是执行#save
的错误方法。应该在 create
完成
def new
@landslide = Landslide.new
end
def create
@landslide = Landslide.new(landslide_params)
if @landslide.save
render plain: @landslide.inspect
else
render :new
end
end
此外,您的表单看起来不对。返回参数中表单数据的位置取决于控件的 name
字段,而不是 id
而不是
%input#location.form-control{:type => "Location"}
我希望看到
%input#landslide_location.form-control{:type => "text", :name => "landslide[location]"}
我这里有一个表格https://pastebin.com/JfXr054y
在routes.rb
我有
resources :landslides, only: [:new, :create, :index]
在landslides_controller.rb
我有
def new
@landslide = Landslide.new(landslide_params)
@landslide.save
render plain: @landslide.inspect
end
和
def landslide_params
params.require(:landslide).permit(:total_id, :year_id, :start_date, :end_date, :day_number, :continent, :country, :location, :type, :admin_level, :new_lat, :new_long, :mapped, :spatial_area, :fatalities, :injuries, :notes, :sources)
end
为什么表格没有保存到 table?
new
是执行#save
的错误方法。应该在 create
def new
@landslide = Landslide.new
end
def create
@landslide = Landslide.new(landslide_params)
if @landslide.save
render plain: @landslide.inspect
else
render :new
end
end
此外,您的表单看起来不对。返回参数中表单数据的位置取决于控件的 name
字段,而不是 id
而不是
%input#location.form-control{:type => "Location"}
我希望看到
%input#landslide_location.form-control{:type => "text", :name => "landslide[location]"}