我已经编辑了这个,因为我以前得到的答案不是一个修复...我有一个县的列表,其中有一个位置列表。我希望能够显示一个县列表,可以点击这些列表以显示另一个页面,其中包含其中的位置列表。我无法理解为什么以下设置不起作用 - 我已经对该信件进行了最初建议的更正并得到与发布的完全相同的错误。有人可以如此善良地解决需要编码的问题以解决这个问题 - 非常感谢任何帮助......我已经设置了我的活动记录
class County < ActiveRecord::Base
has_many :locations
end
class Location < ActiveRecord::Base
belongs_to :county
end
控制器如下:
class LocationsController < ApplicationController
def index
@locations = Location.scoped
@locations = @locations.where(county_id: params[:county_id]) if params[:county_id].present?
end
def show
@location = Location.find(params[:id])
end
end
class CountiesController < ApplicationController
def index
@counties = County.all
end
def show
@county = County.find(params[:id])
end
end
位置索引视图
<h1> Locations: index</h1>
<ul>
<% @locations.each do |location| %>
<li><%= link_to location.name, location %></li>
<% end %>
</ul>
县指数视图
<p>List of all the locations in <%= @county %></p>
<ul>
<%= @counties.each do |county| %>
<li><%= link_to "locations in #{county.id}", locations_path(location_id: location.id) %></li>
<% end %>
</ul>
我的路线是:
resources :counties
resources :locations
尝试查看县时的错误如下:
NameError in Counties#index
Showing /home/ubuntu/workspace/static_site/app/views/counties/index.html.erb where line #7 raised:
undefined local variable or method `location' for #<#<Class:0x00000005a7e108>:0x00000005a7d500>
app/views/counties/index.html.erb:7:in `block in _app_views_counties_index_html_erb__3017893529265312437_33668340'
完整追踪:
activerecord (4.1.5) lib/active_record/relation/delegation.rb:46:in `each'
activerecord (4.1.5) lib/active_record/relation/delegation.rb:46:in `each'
app/views/counties/index.html.erb:6:in `_app_views_counties_index_html_erb__3017893529265312437_33668340'
actionview (4.1.5) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.5) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.5) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.5) lib/action_view/template.rb:143:in `render'
actionview (4.1.5) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.5) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.5) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.5) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.5) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.5) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.5) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.5) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.5) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.5) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.5) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.5) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.5) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.5) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.5) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.5) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.5) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.5) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.5) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.5) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.5) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.5) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.5) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.5) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.5) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.5) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.5) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.5) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.5) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:678:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/flash.rb:254:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.5) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.5) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.5) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.5) lib/rails/engine.rb:514:in `call'
railties (4.1.5) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/usr/local/rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
我已经在这几天了,所以在制定解决方案时会非常感激任何帮助。
答案 0 :(得分:0)
您的县索引视图不会尝试定义location
变量,但您尝试引用location.id
。当然这失败了。你想要location
是什么?
如果目的是链接到位置的索引视图(范围限定为给定的县),则需要修复路由以便location
嵌套...
resources :counties do
resources :locations
end
然后修复您对locations_path
的使用:
link_to "locations in #{county.id}", locations_path(county)
答案 1 :(得分:0)
对于任何从RoR开始的人来说,这个地方坦白无用 - 我提出了一个很容易解决的问题。如果它只是你和一台笔记本电脑,这个东西不容易学习。无论如何,我的问题的答案如下:
<p>List of all counties</p>
<ul>
<% @counties.each do |county| %>
<li><%= link_to "Locations in #{county.name}", county_locations_path(county) %></li>
<% end %>
</ul>
class CountiesController < ApplicationController
def index
@counties = County.all
end
def show
@county = County.find(params[:id])
end
end
resources :counties do
resources :locations
end
class LocationsController < ApplicationController
def index
if params[:county_id]
@county=County.find(params[:county_id])
@locations = @county.locations
else
redirect_to counties_path
end
end
def show
@location = Location.find(params[:id])
end
end
答案 2 :(得分:0)
<h1> Locations for </h1>
<%=@county.name%>
<ul>
<% @locations.each do |location| %>
<li><%= link_to location.name, [@county,location] %></li>
<% end %>
</ul>