2017-04-24 137 views
0

我這裏有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

回答

1

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

相關問題