0
我有一個像這樣活動記錄錯誤消息form_tag的Rails 3中
class Prediction < ActiveRecord::Base
attr_accessible :home_team, :away_team, :home_score, :away_score, :fixture_date, :fixture_id, :user_id
has_one :fixture
validates :fixture_id, :uniqueness => { :scope => :user_id, :message => "only one prediction per game is allowed, for each user" }
end
的想法是,用戶在我的模型驗證,只能使每個燈具的一個預測,如果他們試圖提交另一份預測相同夾具那麼他們得到一個消息,說明他們不能爲已經提交..
我使用的form_tag像這樣
<%= form_tag controller: 'predictions', action: 'create', method: 'post' do %>
<%= error_messages_for :prediction %><!-- Just added this -->
<% @fixture_date.sort.each do |date, fixture| %>
<%= date_format(date) %>
<% fixture.each do |fixture|%>
<%= fixture.home_team %>
<%= text_field_tag "predictions[][home_score]" %>
<%= text_field_tag "predictions[][away_score]" %>
<%= fixture.away_team %>
<%= hidden_field_tag "predictions[][home_team]", fixture.home_team %>
<%= hidden_field_tag "predictions[][away_team]", fixture.away_team %>
<%= hidden_field_tag "predictions[][fixture_date]", fixture.fixture_date %>
<%= hidden_field_tag "predictions[][fixture_id]", fixture.id %>
<%= hidden_field_tag "predictions[][user_id]", current_user.id %>
<% end %>
控制器
def create
begin
params[:predictions].each do |prediction|
Prediction.new(prediction).save!
end
redirect_to root_path, :notice => 'Predictions Submitted Successfully'
end
end
目前即時得到相當難看,而不是實際的
ActiveRecord::RecordInvalid in PredictionsController#create
Validation failed: Fixture only one prediction per game is allowed, for each user
我如何收到錯誤信息顯示
我認爲這會工作在頁面上
<%= error_messages_for :prediction %>
同上但它不
任何幫助表示讚賞
嘗試過,現在表單只是提交併給出成功的消息,但沒有寫入模型,我認爲這是一個好的開始,但不是那裏,我不得不安裝dynamic_form使用error_messages_for,因爲它現在已被棄用......任何想法?謝謝 – Richlewis 2013-05-10 13:53:14
'驗證失敗:夾具每場比賽只允許一個預測,對於每個用戶 '表示你的模型需要一些驗證通過。 – juanpastas 2013-05-10 14:47:42
您應該嘗試爲相同的user_id和相同的fixture_id保存記錄。您可以保存相同的fixture_id,但使用不同的用戶。 – juanpastas 2013-05-10 14:50:40