Active Record Error messages form_tag Rails 3 -


i have validation in model so

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 1 prediction per game allowed, each user" }  end 

the idea being user can make 1 prediction per fixture, , if try , submit prediction same fixture message stating cant submitted..

i using form_tag so

<%= form_tag controller: 'predictions', action: 'create', method: 'post' %>  <%= error_messages_for :prediction %><!-- added -->  <% @fixture_date.sort.each |date, fixture| %> <%= date_format(date) %>   <% fixture.each |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 %> 

controller

def create begin   params[:predictions].each |prediction|     prediction.new(prediction).save!   end   redirect_to root_path, :notice => 'predictions submitted successfully' end end 

at moment im getting rather ugly , not practical

activerecord::recordinvalid in predictionscontroller#create  validation failed: fixture 1 prediction per game allowed, each user 

how error message display on page

i thought work

<%= error_messages_for :prediction %> 

as above doesnt

any appreciated

use save returns boolean , add have model appended errors.

save!, throws exception.


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -