Ruby On Rails - creating an object does not respect validations and showing error messages -


i'm having troubles handling objects not respect validation. i'm building app in user can create "trip" model , add steps trip, called "traces". each added trace prints new part of map present in trip#show action. association between models user has_many trips , trip has_many traces

in users#show put "create new trip" button linking trips#new , here have form_for field corresponding trip attributes. when fill form correctly ok. when missing or wrong (for validations) error:

nomethoderror in trips#create undefined method `model_name' array:class 

------ in trips_controller.rb

def create   @trip = current_user.trips.build(params[:trip])   if @trip.save     # handle successful save     flash[:success] = 'trip created!'     redirect_to user_trip_path(@trip, user_id: current_user.id)     else     @trip = []     @feed_items = []     render 'new'   end end 

------ in app/views/trip, in new.html.erb

h1>create trip</h1> <div class="row">   <div class="col-md-6 col-md-offset-3 general-input">     <%= form_for ([current_user, @trip]) |f| %>      <%= render 'shared/error_messages', object: f.object %>     <%= f.label :name,'give name trip ;)' %>     <%= f.text_field :name %>     <%= f.label :trip_start, 'choose starting point!' %>     <%= f.text_field :trip_start %>     <%= f.label :departure, 'when leaving?' %>     <%= f.date_select :departure, :start_year => date.current.year   %>     <%= f.label :arrive, 'and coming home?' %>     <%= f.date_select :arrive, :start_year => date.current.year %>      <%= f.submit 'create new trip', class: 'btn btn-primary btn-lg' %> <% end %> 

edit 1: problem solving removing @trace=[ ] trips_controller.rb


edit 2:

i have similar problem creation of new trace:

the form adding new trace in trip#show page. when try create trace not respects validation (e.g. if leave blank "destination" field) error:

nomethoderror in posts#create undefined method `any?' nil:nilclass 

when i'm on trip page form traces placed, url like:

 http://localhost:3000/users/2/trips/8 

but when create not valide trace switchs path like:

 http://localhost:3000/trips/8/posts 

i suppose i'm doing wrong handling error messages. misunderstood something, because i'm new rails , web programming in general.

here code parts, hoping helps understand mistake:

------ in traces_controller.rb

def create  @trip= trip.find(params[:trip_id])  @trace = @trip.traces.create(params[:trace])   if @trace.save    flash[:success] = 'trace created!'    redirect_to user_trip_path(@trip, user_id: current_user.id)    else    @trace=[]    render 'trips/show'  end end 

------ in app/views/shared, in add_trace_form.html.erb

<p>complete trip adding route!</p>  <%= form_for ([@trip, @trip.traces.build]) |f| %>   <%= render 'shared/error_messages', object: f.object %>   <div class="block post-form">   <div class="field ">   <%= f.text_field :end, placeholder: 'where going next?' %>   <%= f.label :arr_day, 'when?' %>   <%= f.date_select :arr_day, :start_year => date.current.year   %>    <%= f.submit 'add route', class: 'btn btn-primary btn-landing' %>    </div>   </div> <% end %> 

------ in app/views/shared, in error_messages.html.erb

 <% if object.errors.any? %>     <div id="error_explanation">     <div class="alert alert-error">     form contains <%= pluralize(object.errors.count, "error") %>.     </div>     <ul>      <% object.errors.full_messages.each |message| %>         <li>* <%= message %></li>     <% end %>     </ul>   </div> <% end %> 

------ in routes.rb

resources :users     resources :trips end  resources :trips   resources :traces end  resources :traces 

thanks lot

i think when passing f.object in locales in render passing array not active record object ,<%= render 'shared/error_messages', object: f.object %>. can u check inspecting object in partial , class has. try inspecting object.errors.inspect try refering http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials


Comments