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