Similar Question and Solutions on StackOverFlow
####Question: Model inheritance and model specific code in the controller/view
I am using Rails single-table inheritance with a superclass Content and a number of subclasses (such as Article, Comment etc.) I am trying to get away with a single controller, and I have set up the routes thusly:
resources :contents
resources :articles, :controller => "contents"
resources :comments, :controller => "contents"
This way, /articles/new gets routed to contents#new which seems to be what I want.
Within the controller and the views, however, I need to tailor the functionality a bit depending on which actual model I am dealing with. For that purpose, I need to determine the original requested resource or otherwise find out which subclass I am dealing with.
Looking at params for /articles/newwithin the common controller gives {"action"=>"new", "controller"=>"contents"}, which obviously does not provide the information I need.
Either the answer is really obvious or I am using model inheritance wrong. Which one is it? :)
####Answer 1: You can extract the part of the request path you are interested in like this
path = request.fullpath[%r{^/(articles|comments)/}, 1] # articles or comments
Once you have it you can get the model class like that:
model_class = path.classify.constantize # Article or Comment Bests,
Richard
####Best Solution: Untilization of Routings
Brilliant, thanks. Further investigation showed that I can also do resources :comments, :controller => "contents", :type => "Comment" and then refer to params[:type]. But your solution also fits the bill and taught me classify and constantize. Thanks! – Sami Sep 3 '11 at 21:48
居然放在设置里:after_topic_html,如此隐蔽呀。
@huacnlee, @lgn21st, @hooopo, @Rei, @poshboytl, 请教啦。
thin 运不运行基本无影响吧。我就没有运行,dev 和 prod 都正常。
安装了 OpenJDK 后正常运行啦。
之前已经运行,但估计是因为sh:java: not found
的原因。在 Ubuntu 下装 JDK 失败,求解:
DEPRECATION WARNING: ActiveSupport::Memoizable is deprecated and will be removed in future releases,simply use Ruby memoization pattern instead. (called from <top (required)> at /home/kevin/www-root/config/application.rb:13)
sh: java: not found
Removing stale PID file at /home/kevin/www-root/solr/pids/development/sunspot-solr-development.pid
Successfully started Solr ...
除此之年,看了一下系统日志,有时还会提示以下错误:
Started GET "/favicon.ico" for 127.0.0.1 at 2012-03-22 23:40:13 +0800
ActionController::RoutingError (No route matches [GET] "/favicon.ico"):
actionpack (3.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.2) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.2) lib/rails/rack/logger.rb:16:in `call'
/home/kevin/.bundler/ruby/1.9.1/quiet_assets-b699ee6c8aac/lib/quiet_assets.rb:12:in `call_with_quiet_assets'
actionpack (3.2.2) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.2) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.2) lib/action_dispatch/middleware/static.rb:61:in `call'
rack-cache (1.1) lib/rack/cache/context.rb:132:in `forward'
rack-cache (1.1) lib/rack/cache/context.rb:241:in `fetch'
rack-cache (1.1) lib/rack/cache/context.rb:181:in `lookup'
rack-cache (1.1) lib/rack/cache/context.rb:65:in `call!'
rack-cache (1.1) lib/rack/cache/context.rb:50:in `call'
railties (3.2.2) lib/rails/engine.rb:479:in `call'
railties (3.2.2) lib/rails/application.rb:220:in `call'
railties (3.2.2) lib/rails/railtie/configurable.rb:30:in `method_missing'
passenger (3.0.11) lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'
passenger (3.0.11) lib/phusion_passenger/abstract_request_handler.rb:513:in `accept_and_process_next_request'
passenger (3.0.11) lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
passenger (3.0.11) lib/phusion_passenger/rack/application_spawner.rb:206:in `start_request_handler'
passenger (3.0.11) lib/phusion_passenger/rack/application_spawner.rb:171:in `block in handle_spawn_application'
passenger (3.0.11) lib/phusion_passenger/utils.rb:479:in `safe_fork'
passenger (3.0.11) lib/phusion_passenger/rack/application_spawner.rb:166:in `handle_spawn_application'
passenger (3.0.11) lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
passenger (3.0.11) lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
passenger (3.0.11) lib/phusion_passenger/abstract_server.rb:180:in `start'
passenger (3.0.11) lib/phusion_passenger/rack/application_spawner.rb:129:in `start'
passenger (3.0.11) lib/phusion_passenger/spawn_manager.rb:253:in `block (2 levels) in spawn_rack_application'
passenger (3.0.11) lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
passenger (3.0.11) lib/phusion_passenger/spawn_manager.rb:246:in `block in spawn_rack_application'
passenger (3.0.11) lib/phusion_passenger/abstract_server_collection.rb:82:in `block in synchronize'
<internal:prelude>:10:in `synchronize'
passenger (3.0.11) lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
passenger (3.0.11) lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
passenger (3.0.11) lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
passenger (3.0.11) lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
passenger (3.0.11) lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
passenger (3.0.11) lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
passenger (3.0.11) helper-scripts/passenger-spawn-server:99:in `<main>'
访问其它所有功能都能返回 200OK,唯独/topics/
以及/topics/:id/
,浏览器会显示 404,日志中提示以下信息:
Started GET "/topics/2" for 127.0.0.1 at 2012-03-22 23:38:38 +0800
Processing by TopicsController#show as HTML
Parameters: {"id"=>"2"}
Rendered topics/_base.html.erb (1.0ms)
Rendered topics/_topic_info.html.erb (1.0ms)
Rendered topics/_replies.html.erb (0.2ms)
Rendered topics/_editor_toolbar.html.erb (0.3ms)
Rendered app/cells/topics/reply_help_block.zh-CN.html.erb (0.0ms)
Rendered app/cells/topics/sidebar_for_new_topic_node.html.erb (0.6ms)
Rendered topics/show.html.erb within layouts/application (53.2ms)
Completed 500 Internal Server Error in 63ms
ActionView::Template::Error (Connection refused - connect(2)):
52:
53: <div class="sidebar">
54: <%= render_cell :topics, :sidebar_for_new_topic_node, { :action => params[:action], :node => @node } %>
55: <%= render_cell :topics, :sidebar_for_more_like_this, { :topic => @topic} %>
56: </div>
app/cells/topics_cell.rb:48:in `sidebar_for_more_like_this'
app/views/topics/show.html.erb:55:in `_app_views_topics_show_html_erb___4535444006796019447_42839120'
晚上回家去看一下日志。 另外,Ruby China 对 Gems 过度依赖呀,稍微调整一下 Gems 的版本 (小版本的调整),就不能正常运作。
呵呵,这个贴子被挖出来啦。
目前在学习 rails 中,希望自己能先积累一些知识和经验。
不好看,没兴趣。基于 Bootstrap,自己稍微修改一下,是更好的选择。
删除 gem 'wice_grid'和 gem 'mongoid_wice_grid'后,rails s 能正常运行啦:
用'thin'取代'web-brick',发现这套 Gemfile 还挺好。以后有时间逐个击破之。
rails new appname -O
touch config/mongoid.yml
vim config/mongoid.yml
简单的几条命令,rails 就构架成功啦。
Ruby version 1.9.3 (x86_64-linux)
RubyGems version 1.8.15
Rack version 1.4
Rails version 3.2.2
JavaScript Runtime therubyracer (V8)
Action Pack version 3.2.2
Active Resource version 3.2.2
Action Mailer version 3.2.2
Active Support version 3.2.2
Middleware
ActionDispatch::Static
Rack::Lock
#<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000000047d6550>
Rack::Runtime
Rack::MethodOverride
ActionDispatch::RequestId
Rails::Rack::Logger
ActionDispatch::ShowExceptions
ActionDispatch::DebugExceptions
ActionDispatch::RemoteIp
ActionDispatch::Reloader
ActionDispatch::Callbacks
ActionDispatch::Cookies
ActionDispatch::Session::CookieStore
ActionDispatch::Flash
ActionDispatch::ParamsParser
ActionDispatch::Head
Rack::ConditionalGet
Rack::ETag
ActionDispatch::BestStandardsSupport
Warden::Manager
Rack::Mongoid::Middleware::IdentityMap
Application root /home/kevin/hujoycore
Environment development
wice_grid 有点冲突,准备删之。
reez.com fang001.com osaka.co topi.co lanpon.com kanchina.com bingomed.com msgp.com
不胜枚举呀。
需要修改成“编码->UTF8"才正常。
家里用的是 Ubuntu 10.04 + Chrome,Office 用的是 Windows XP + Chrome,都有这样的问题。
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Ruby China绀惧尯</title>
<link>http://ruby-china.org/</link>
<description>Ruby China绀惧尯鏈€鏂板彂璐�.</description>
<language>en-us</language>
<item>
<title>鏈夌敤tw鐨刡ootstrap鍋氱殑寮€婧愮綉绔欏悧锛�</title>
<description></description>
<author>shunai</author>
<pubDate>Thu, 15 Mar 2012 00:45:56 +0800</pubDate>
<link>http://ruby-china.org/topics/1868</link>
<guid>http://ruby-china.org/topics/1868</guid>
</item>
顶一下,有没有有这方面经验的同志?