在rails中的视图中显示外键的标题

时间:2014-04-21 20:36:04

标签: ruby-on-rails

我有以下代码:

# file app/models/admin/categoria.rb

class Admin::Categoria < ActiveRecord::Base
    has_many :posts
end

# file app/models/admin/post.rb

class Admin::Post < ActiveRecord::Base
    belongs_to :categoria, foreign_key: "categoria_id"
end

我试图从Categorias表中显示标题(titulo),如下面的代码:

# file app/views/admin/posts/index.html.erb

<% @posts.each do |post| %>
  <tr>
    <td><%= post.titulo %></td>
    <td><%= post.slug %></td>
    <td><%= post.created_at %></td>
    <td><%= post.categoria_id %></td>
  </tr>
<% end %>

显示ID的数量。如果我尝试:

<td><%= post.categoria.titulo %></td>

我得到了错误&#34;未定义的方法`categoria&#39;&#34;。这是堆栈跟踪:

activemodel (4.1.0) lib/active_model/attribute_methods.rb:435:in `method_missing'
activerecord (4.1.0) lib/active_record/attribute_methods.rb:206:in `method_missing'
app/views/admin/posts/index.html.erb:20:in `block in _app_views_admin_posts_index_html_erb___3127847778944241957_70050289571840'
activerecord (4.1.0) lib/active_record/relation/delegation.rb:46:in `each'
activerecord (4.1.0) lib/active_record/relation/delegation.rb:46:in `each'
app/views/admin/posts/index.html.erb:15:in `_app_views_admin_posts_index_html_erb___3127847778944241957_70050289571840'
actionview (4.1.0) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.0) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.0) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.0) lib/action_view/template.rb:143:in `render'
actionview (4.1.0) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.0) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.0) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.0) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.0) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.0) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.0) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.0) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.0) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.0) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.0) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.0) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/usr/local/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (4.1.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.0) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.0) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.0) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.0) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.1.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.1.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.0) lib/action_dispatch/routing/route_set.rb:676: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.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.0) 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.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.0) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.0) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.0) 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.0) 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.0) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.0) lib/rails/engine.rb:514:in `call'
railties (4.1.0) 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/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/usr/local/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/usr/local/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

我如何展示&#34; titulo&#34;来自分类表?

2 个答案:

答案 0 :(得分:2)

更改您的model定义,如下所示:

# file app/models/admin/categoria.rb

class Admin::Categoria < ActiveRecord::Base
    has_many :posts, class_name: "Admin::Post"
end

# file app/models/admin/post.rb

class Admin::Post < ActiveRecord::Base
    belongs_to :categoria, class_name: "Admin::Categoria", foreign_key: "categoria_id"
end

由于您的模型在Admin模块中被命名空间。我在关联中添加了class_name选项。

答案 1 :(得分:0)

您需要执行categoria belongs_to:post and post has_one:categoria

此外,您不需要声明外键。如果您声明关系,Rails将自动在帖子中查找列categoria_id。