为什么我会得到错误`undefined method'map'`?

2 ruby ruby-on-rails ruby-on-rails-3 drop-down-menu

在我的Ruby on Rails应用程序中,我试图在_form.html.erb中显示三个下拉菜单,这些菜单是从文件_booking_lookup.html.erb呈现的,并从模型中的下拉菜单方法获取数据.

_form.html.erb:

<%= render(:partial => '/booking_lookup', :locals=> {:film => @film = Film.all, :showings => @showings = Showing.all, :seats => @seats = Seat.all, :my_path => '/films/booking_lookup' }) %>
Run Code Online (Sandbox Code Playgroud)

_booking_lookup.html.erb:

<%= form_tag my_path, :method=>'post', :multipart => true do %>
<%= select_tag ('title_id'), 
    options_from_collection_for_select(@films, :id, :title_info, 0 ),
    :prompt => "Film" %>

<%= select_tag ('showings_id'), 
    options_from_collection_for_select(@showings, :id, :showing_times, 0 ),
    :prompt => "Showings" %> 

<%= select_tag ('seat_id'), 
    options_from_collection_for_select(@seats, :id, :seats_available, 0 ),
    :prompt => "Seats" %> 

<%= submit_tag 'Search' %>
Run Code Online (Sandbox Code Playgroud)

film.rb:

class Film < ActiveRecord::Base

has_many :showings
belongs_to :certificate
belongs_to :category

def title_info
     "#{title}"
end
end
Run Code Online (Sandbox Code Playgroud)

seat.rb:

class Seat < ActiveRecord::Base
belongs_to :screen
has_many :bookings

def seats_available
    "#{row_letter}#{row_number}"
end
end
Run Code Online (Sandbox Code Playgroud)

showing.rb:

 class Showing < ActiveRecord::Base
  belongs_to :film
  has_many :bookings
  belongs_to :screen

    def showing_times
        "#{show_date.strftime("%e %b %Y")} @ #{show_time.strftime("%H:%M")}"
    end
end
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,该行:<%= select_tag ('title_id'), options_from_collection_for_select(@films, :id, :title_info, 0 ), :prompt => "Film" %>我收到错误:

NoMethodError in Bookings#new 
undefined method `map' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)

奇怪的是我在其他地方使用了很多代码,我有一个_multi_search.html.erb形式:

 <%= form_tag my_path, :method=>'post', :multipart => true do %>
 <!--  Genre: -->
 Search By:
 <%= select_tag ('cat_id'), 
    options_from_collection_for_select(@categories, :id, :category_info, 0 ),
    :prompt => "Genre" %>

<%= select_tag ('cert_id'), 
    options_from_collection_for_select(@certificates, :id, :certificate_info, 0 ),
    :prompt => "Age Rating" %> 

<%= text_field_tag :search_string, nil, placeholder: "ACTOR" %>
or
<%= select_tag ('title_id'), 
    options_from_collection_for_select(@films, :id, :title_info, 0 ),
    :prompt => "Film" %>
<%= submit_tag 'Search' %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

并在application.html.erb中使用:

<%= render(:partial => '/multi_search', :locals=> {:categories => @categories = existing_genres, :certificates => @certificates = Certificate.all, :films => @films = Film.all, :my_path => '/films/multi_find' }) %>

这很好.

我究竟做错了什么?

mes*_*jah 5

它看起来像是@films零.尝试在_form.html.erb中设置@films = Film.all(而不是@film = Film.all).

更新:

我建议将查询移动到控制器操作.在模型 - 视图 - 控制器模式中,控制器应该向模型询问数据,而不是视图.

# BookingLookupController
def new
  @films = Film.all
  @showings = Showing.all
  @seats = Seat.all
end
Run Code Online (Sandbox Code Playgroud)

然后,您可以在视图中引用实例变量.

<%= render partial: '/booking_lookup', locals: {films: @films, showings: @showings, seats: @seats, my_path: '/films/booking_lookup' } %>
Run Code Online (Sandbox Code Playgroud)