简体   繁体   中英

Rails 3 - Passing models to another controller & losing stylesheets on render

I am attempting to build a simple content management system using Twitter's Bootstrap for a small site and I'm running into an issue with the views.

Rails version is 3.0.10

I want to allow the user to create their own pages so I have a Pages controller that has all your standard RESTful methods. Since these can have crud applied to them, they can only be accessed by a logged-in administrator.

So... I have a Public controller that sets the @pages and @page instance variables and uses those to display them in the public Show view.

Here is the Public controller - pretty simple:

  def index
    @pages = Page.all
  end

  def show
    @pages = Page.all
    @page = Page.find(params[:id])    
  end

The reason that there is a @pages instance variable on the 'index' method is because I have a welcome page that loads and I'm passing in @pages to populate the navigation elements dynamically:

    <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 -->

The content of the Public controller's 'show' method (which contains an individual page) is passed into the <%= yield %> statement in the applicaton.html.erb file:

<div class="container">    

  <%= yield %>
  <hr>

  <footer>
    <p>My site</p>
  </footer>

</div> <!-- /container -->

So far, this is working fine.

The problem is that when I click the link that takes me to the public_path(page) - I get the content as it should appear, but I lose all my styles. I'm actually getting a 404 error on my stylesheets:

在此处输入图片说明

All of these style sheets were loading in just fine on the http://localhost:3000/public page, but when it goes to http://localhost:3000/public/1 - that's when all the styles disappear. But, they are both using the same layout.

Here is what the log file shows when the request is made:

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)

Thanks for having a look

I managed to find my own answer...

The Rails helper stylesheet_link_tag was generating a relation attribute of type "stylesheet" when the Less css files needed a relation of type "stylesheet/less".

The fix was specifying the relation like this:

 <%= 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'”标签,防止助手生成关系而导致路由错误。

Try to change path to your resource, something like that:

# config/routes.rb

resources :public, :path => :my_public

This will give you routes

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

This way it will not mess up with requests to the static assets in public directory. But you may want to rename resource and controller instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM