【发布时间】:2015-07-17 16:38:14
【问题描述】:
我在处理不遵守验证的对象时遇到了麻烦。 我正在构建一个应用程序,用户可以在其中创建“Trip”模型,然后将步骤添加到他的行程中,我称之为“Traces”。每个添加的 trace 都会打印 trip#show 操作中存在的地图的一个新部分。 模型之间的关联是user has_many trips和trip has_many traces
在 users#show 我放了一个链接到 trips#new 的“CREATE NEW TRIP”按钮,这里我有 form_for,其字段对应于 旅行属性。 当我正确填写表格时,一切正常。当某些东西丢失或错误(用于验证)时,我会收到此错误:
NoMethodError in Trips#create
undefined method `model_name' for Array:Class
--------在trips_controller.rb
中def create
@trip = current_user.trips.build(params[:trip])
if @trip.save
# handle a 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
------ 在 app/views/trip 中,在 new.html.erb
中h1>Create a trip</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3 general-input">
<%= form_for ([current_user, @trip]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.label :name,'Give a name to your trip ;)' %>
<%= f.text_field :name %>
<%= f.label :trip_start, 'Choose your starting point!' %>
<%= f.text_field :trip_start %>
<%= f.label :departure, 'When are you leaving?' %>
<%= f.date_select :departure, :start_year => Date.current.year %>
<%= f.label :arrive, 'And coming back home?' %>
<%= f.date_select :arrive, :start_year => Date.current.year %>
<%= f.submit 'Create a new trip', class: 'btn btn-primary btn-lg' %>
<% end %>
编辑 1:解决从 trips_controller.rb 中删除 @trace=[ ] 的问题
编辑 2:
我在创建新 Trace 时也有类似的问题:
添加新trace的表格在trip#show页面。 当我尝试创建不遵守验证的跟踪时(例如,如果我将“目标”字段留空)我收到此错误:
NoMethodError in Posts#create
undefined method `any?' for nil:NilClass
当我在放置 Traces 表单的 Trip 页面上时,URL 是这样的:
http://localhost:3000/users/2/trips/8
但是当我创建一个无效跟踪时,它会切换到如下路径:
http://localhost:3000/trips/8/posts
我想我在处理错误消息时做错了。我可能误解了一些东西,即使我是 Rails 和 Web 编程的新手。
这是一些代码部分,希望有助于理解我的错误:
------在 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
------ 在 app/views/shared,在 add_trace_form.html.erb
<p>Complete your trip adding a route!</p>
<%= form_for ([@trip, @trip.traces.build]) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="block post-form">
<div class="field ">
<%= f.text_field :end, placeholder: 'Where are you 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 %>
------ 在 app/views/shared 中,在 error_messages.html.erb
<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |message| %>
<li>* <%= message %></li>
<% end %>
</ul>
</div>
<% end %>
--------在routes.rb
中resources :users do
resources :trips
end
resources :trips do
resources :traces
end
resources :traces
非常感谢
【问题讨论】:
-
尝试在创建操作中删除
@trip = []以消除第一个错误。 -
成功了,非常感谢 :) 所以,只是想知道,我什么时候需要在 else 情况下放置一个空向量?
标签: ruby-on-rails ruby-on-rails-3 validation missing-template