0
我有我的控制器更新方法如下:ROR控制方法「更新」不更新數據庫
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]消息確實會出現,但這些更改實際上並不存儲在數據庫中。
有什麼建議嗎?
如果你想知道,我在我的路線中使用「resources:cards」來訪問編輯和更新方法。
編輯*卡模型(如需要)
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
你可以添加 「一卡通」 模式 –
卡模型的細節加入 – user2943464