我的控制器中有一个更新方法,如下所示:
def edit
@card = Card.find(params[:id])
end
def update
@card = Card.find(params[:id])
if @card.update_attributes(params[card_params])
flash[:success] = "Changes saved"
redirect_to new_card_path(:deck =>@card.deck_id)
else
redirect_to new_card_path(:deck =>@card.deck_id)
end
end
private
def card_params
params.require(:card).permit(:atext, :qtext, :deck_id)
end
我的表格如下:
<%= simple_form_for @card, :html => { :method => :put} do |f| %>
<p> Fields marked with * are required </p>
<div><%= f.label "Question *" %> <br />
<%= f.text_area :qtext, :autofocus => true, :default => @card.qtext %></div>
<div><%= f.label "Answer *" %> <br />
<%= f.text_area :atext, :autofocus => true, :default => @card.atext %></div>
<%= f.hidden_field :deck_id, :value => @card.deck_id %>
<%= f.submit "Save Changes", class: "btn btn-large btn-primary" %>
<%end%>
填写表单后,它会重定向,就好像它更新了属性一样,并且确实出现了 flash[:success] 消息,但这些更改实际上并未存储在数据库中。
有什么建议么?
如果您想知道,我在我的路线中使用“资源:卡片”来访问编辑和更新方法。
编辑*
卡型号的详细信息(根据要求)
class Card < ActiveRecord::Base
belongs_to :deck
belongs_to :recent_deck
belongs_to :favorite_deck
has_one :deck
validates :atext, :qtext, presence: true
after_create :do_setID
private
def do_setID
newID = self.id
self.update_attributes(:card_id => newID)
end
end