2

我在处理不尊重验证的对象时遇到了麻烦。我正在构建一个应用程序,用户可以在其中创建一个“ Trip ”模型,然后在他的行程中添加步骤,我称之为“ Traces ”。每个添加的跟踪都会打印出现在trip#show操作中的地图的一个新部分。模型之间的关联是user has_many tripstrip has_many traces

users#show 中,我放置了一个链接到trips#new的“CREATE NEW TRIP”按钮,这里我有 form_for,其字段对应于Trip属性。当我正确填写表格时,一切正常。当某些东西丢失或错误(用于验证)时,我会收到此错误:

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 时也遇到了类似的问题:

添加新轨迹的表格在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

非常感谢

4

1 回答 1

0

我认为当您在渲染中传递语言环境中的 f.object 时,它传递的是数组而不是活动记录对象,<%= render 'shared/error_messages', object: f.object %>。你可以检查你的部分中的检查对象以及它有什么类。尝试检查 object.errors.inspect 尝试参考http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials

于 2015-07-17T18:23:07.457 回答