[英]Rails 3 - Passing models to another controller & losing stylesheets on render
我正在尝试使用Twitter的Bootstrap为一个小型网站构建一个简单的内容管理系统,但我遇到了有关视图的问题。
Rails版本是3.0.10
我想允许用户创建自己的页面,因此我拥有一个包含所有标准RESTful方法的Pages控制器。 由于可以将这些对象应用到它们,因此只有登录管理员才能访问它们。
所以...我有一个Public控制器,它设置@pages和@page实例变量,并使用这些变量在公共Show视图中显示它们。
这是公共控制器-非常简单:
def index
@pages = Page.all
end
def show
@pages = Page.all
@page = Page.find(params[:id])
end
之所以在'index'方法上有一个@pages实例变量,是因为我加载了一个欢迎页面,并且我传入了@pages以动态填充导航元素:
<div class="nav-collapse">
<ul class="nav">
<li><%= link_to "Welcome", public_index_path, :class => 'active', :id => 'menu_home' %></li>
<% @pages.each do |page| %>
<li><%= link_to page.title, public_path(page) %></li>
<% end %>
</ul>
</div><!--/.nav-collapse -->
公共控制器的“ show”方法(包含单个页面)的内容传递到applicaton.html.erb文件中的<%= yield %>
语句中:
<div class="container">
<%= yield %>
<hr>
<footer>
<p>My site</p>
</footer>
</div> <!-- /container -->
到目前为止,一切正常。
问题是,当我单击将我带到public_path(page)
的链接时,我获得了应显示的内容,但是我失去了所有样式。 我的样式表实际上收到404错误:
所有这些样式表都可以在http:// localhost:3000 / public页面上很好地加载,但是当它转到http:// localhost:3000 / public / 1时,所有样式都消失了。 但是,它们都使用相同的布局。
这是发出请求时日志文件显示的内容:
Started GET "/public/1" for 127.0.0.1 at 2012-03-03 21:14:49 -0600
Processing by PublicController#show as HTML
Parameters: {"id"=>"1"}
[1m[35mPage Load (1.0ms)[0m SELECT "pages".* FROM "pages"
[1m[36mPage Load (0.0ms)[0m [1mSELECT "pages".* FROM "pages" WHERE "pages"."id" = 1 LIMIT 1[0m
Rendered public/show.html.erb within layouts/application (24.0ms)
Completed 200 OK in 63ms (Views: 51.0ms | ActiveRecord: 1.0ms)
Started GET "/public/stylesheets/bootstrap.css" for 127.0.0.1 at 2012-03-03 21:14:50 -0600
ActionController::RoutingError (No route matches "/public/stylesheets/bootstrap.css"):
Rendered c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.0ms)
Started GET "/public/stylesheets/bootstrap-responsive.css" for 127.0.0.1 at 2012-03-03 21:14:50 -0600
ActionController::RoutingError (No route matches "/public/stylesheets/bootstrap-responsive.css"):
Rendered c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms)
Started GET "/public/stylesheets/elements.less" for 127.0.0.1 at 2012-03-03 21:14:51 -0600
ActionController::RoutingError (No route matches "/public/stylesheets/elements.less"):
Rendered c:/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (3.0ms)
感谢您的浏览
我设法找到了自己的答案...
当Less css文件需要类型为“ stylesheet / less”的关系时,Rails帮助器stylesheet_link_tag会生成类型为“ stylesheet”的关系属性。
修复程序指定了这样的关系:
<%= stylesheet_link_tag 'elements.less', :rel => 'stylesheet/less' %>
<%= stylesheet_link_tag 'main.less', :rel => 'stylesheet/less' %>
如果您正在使用bootstrap-saas gem,请确保从application.html.erb文件中取出“ link href ='assets / css / bootstrap-sensitive.css'rel ='stylesheet'”标签,防止助手生成关系而导致路由错误。
尝试更改资源的路径,如下所示:
# config/routes.rb
resources :public, :path => :my_public
这会给你路线
public_index GET /my_public(.:format) public#index
POST /my_public(.:format) public#create
new_public GET /my_public/new(.:format) public#new
edit_public GET /my_public/:id/edit(.:format) public#edit
public GET /my_public/:id(.:format) public#show
PUT /my_public/:id(.:format) public#update
DELETE /my_public/:id(.:format) public#destroy
这样,它不会弄乱对public
目录中的静态资产的请求。 但是您可能想重命名资源和控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.