表单字段未保存到数据库中 - 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]"}