在我的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' }) %>
_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' %>
film.rb:
class Film < ActiveRecord::Base
has_many :showings
belongs_to :certificate
belongs_to :category
def title_info
"#{title}"
end
end
seat.rb:
class Seat < ActiveRecord::Base
belongs_to :screen
has_many :bookings
def seats_available
"#{row_letter}#{row_number}"
end
end
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
但出于某种原因,使用该行:<%= 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
奇怪的是,我使用了很多其他代码,我有一个_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 %>
并在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' }) %>
这很好。
我做错了什么?
答案 0 :(得分:5)
看起来@films
是零。尝试在_form.html.erb中设置@films = Film.all
(而不是@film = Film.all
)。
<强>更新强>:
我建议将查询移动到控制器操作。在模型 - 视图 - 控制器模式中,控制器应该向模型询问数据,而不是视图。
# BookingLookupController
def new
@films = Film.all
@showings = Showing.all
@seats = Seat.all
end
然后,您可以在视图中引用实例变量。
<%= render partial: '/booking_lookup', locals: {films: @films, showings: @showings, seats: @seats, my_path: '/films/booking_lookup' } %>
答案 1 :(得分:0)
在Controller中,选择字段,因为您只想在下拉列表中显示名称
def method_name
@films = Film.select([:id, :title_info])
@showings = Showing.select([:id, :showing_times])
@seats = Seat.select([:id, :seats_available])
end
在页面
<%= render(:partial => '/booking_lookup', :locals=> {:films => @films, :showings => @showings, :seats => @seats, :my_path => '/films/booking_lookup' }) %>
部分
options_from_collection_for_select(films, :id, :title_info, 0 ),:prompt => "Film" %>