我的应用程序有一个服务器和域模块,以便服务器具有多个域和域belongs_to服务器。
我正在尝试在服务器控制器中呈现部分:
def get_domain_checkboxes
@domains = Domain.find_by(:server_id => params[:id])
render :partial => 'servers/domain_checkboxes', :layout => nil
end
部分'_domain_checkboxes.html.erb'包含:
<% @domains.each do |domain| %>
domain.url
<% end %>
但是当我转到'servers / 3 / get_domain_checkboxes'路线时出现错误,跟踪说:
Started GET "/servers/3/get_domain_checkboxes" for ::1 at 2015-12-18 05:03:08 +0200
Processing by ServersController#get_domain_checkboxes as HTML
Parameters: {"server_id"=>"3"}
Domain Load (0.3ms) SELECT "domains".* FROM "domains" WHERE "domains"."server_id" IS NULL LIMIT 1
Rendered servers/_domain_checkboxes.html.erb (1.4ms)
Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.4ms)
ActionView::Template::Error (undefined method `each' for nil:NilClass):
1: <% @domains.each do |domain| %>
2: hello
3: <% end %>
app/views/servers/_domain_checkboxes.html.erb:1:in `_app_views_servers__domain_checkboxes_html_erb___2806237560631891313_70206341448100'
app/controllers/servers_controller.rb:71:in `get_domain_checkboxes'
过去一小时都在尝试。我可能缺少一些简单的东西吗?
编辑:
尝试了@domains = Domain.find_by(:server_id => params[:server_id])
,但仍然收到错误:
Started GET "/servers/3/get_domain_checkboxes" for ::1 at 2015-12-18 05:09:43 +0200
Processing by ServersController#get_domain_checkboxes as HTML
Parameters: {"server_id"=>"3"}
Domain Load (0.1ms) SELECT "domains".* FROM "domains" WHERE "domains"."server_id" = ? LIMIT 1 [["server_id", 3]]
Rendered servers/_domain_checkboxes.html.erb (1.6ms)
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.1ms)
ActionView::Template::Error (undefined method `each' for #<Domain:0x007fb465810738>):
1: <% @domains.each do |domain| %>
2: hello
3: <% end %>
app/views/servers/_domain_checkboxes.html.erb:1:in `_app_views_servers__domain_checkboxes_html_erb___2806237560631891313_70206341448100'
app/controllers/servers_controller.rb:71:in `get_domain_checkboxes'
答案 0 :(得分:3)
您需要更改:
@domains = Domain.find_by(:server_id => params[:id])
到
@domains = Domain.where(:server_id => params[:server_id])