表单字段不保存到数据库 - Ruby on Rails的

问题描述:

我这里有https://pastebin.com/JfXr054y表单字段不保存到数据库 - Ruby on Rails的

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 

为什么表格没有保存到表格中?

+0

检查返回值保存,也许这是因为你的模型的验证。 – yoones

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 

此外,您的窗体看起来不对。在返回PARAMS表单数据的位置取决于控制的name领域,而不是在id

而不是

%input#location.form-control{:type => "Location"} 

我希望看到

%input#landslide_location.form-control{:type => "text", :name => "landslide[location]"} 
+0

我更改为你的,但它仍然无效。我认为形式有问题。 –

+0

这里是我的提交按钮'%button.btn.btn-default {“data-dismiss”=>“modal”,:type =>“submit”} Submit'。我也需要每个领域的特定属性? –

+0

是的,你说得对,你的表格是非标准的。看我的编辑。 – SteveTurczyn