我想添加一個表單來在我的rails4應用程序中創建一條記錄。不幸的是,當我提交記錄時,新記錄被添加到數據庫中,但所有列都是空的。我運行rails控制檯並通過哈希(Location.new({...}))添加了一個位置記錄,並且這個工作非常完美,所以我知道控制器和模型工作。在Rails 4中沒有保存到數據庫的值
你能看到任何問題的視圖或此代碼的任何其他部分?我一直試圖找到幾個小時的問題,但我卡住了。這非常令人沮喪 - 請幫助!
位置/ new.html.haml
render 'form'
鏈接添加一條記錄
= link_to 'Add Location', new_location_path
路線
resources :locations
_form.html.haml
= simple_form_for (@location) do |f|
.row
.large-6.columns
= f.input :name
.large-6.columns
= f.input :address
.large-6.columns
Location Name
.row
.large-6.columns
=f.input :state
.row
= f.button :submit
locations_controller.rb
def new
@location = Location.new
end
def create
@location = Location.new(subject_params)
if @location.save
redirect_to @location
else
render 'new'
end
end
private
def subject_params
params.permit(:name, :address, :state)
end
我從沒有機會回答一個問題,當你在網上= P –
:)這也恰好與我。這次我很幸運。 –
沒問題,你值得擁有! –