1

我想通过引用/用户查看所有公司的列表

users_controller.rb:

  def index
    @companies = Company.all
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @companies }
    end
  end

用户/index.html.haml:

%div{:style => "margin:10px" }  
  %table{ :class => "table table-striped"}
    %thead
      %tr
        %th 
          = t("Logo")
        %th 
          = t("company_name")
        %th 
          = t("website")
        %th
    %tbody
      - @companies.each do |company| 
        %tr
          %td 
            - if company.logo?
              = link_to image_tag( company.logo.url, :width => "60", :height => "60"), company
            - else
              = link_to image_tag( "/lg.png", :width => "60", :height => "60"), company
          %td 
            = link_to company.name, company_path
          %td
            - if company.website?
              = link_to company.website, company.website
            - else
              = link_to company.name, company_path
          %td
            - if can? :read, company
              = link_to '<i class="icon-info-sign icon-white"></i> '.html_safe + t('Show'), company, :class => 'btn btn-small btn-info'
            - if can? :update, company
              = link_to '<i class="icon-pencil icon-white"></i> '.html_safe + t('Edit'), edit_company_path(company), :class => 'btn btn-small btn-pencil'
            - if can? :destroy, company 
              = link_to '<i class="icon-trash icon-white"></i> '.html_safe + t('Destroy'), company, confirm: t('Are you sure'), method: :delete, :class => 'btn btn-small btn-danger'

但是当链接出现错误时:
No route matches {:action=>"show", :controller=>"companies"}

4

2 回答 2

1

定义控制器不会自动创建可以到达该控制器的路由。您需要将必要的定义添加到config/routes.rb. 这是路由指南

于 2012-10-15T10:59:15.740 回答
1

在你users_controller.rb'写的

def show
  respond_to do |format|
    format.html{}
    format.json{}
  end
end

并在您的路线文件中写入

resources :users
于 2012-10-15T11:00:40.213 回答