【问题标题】:Namespacing routes file provides routing error命名空间路由文件提供路由错误
【发布时间】:2012-05-01 09:08:56
【问题描述】:

我正在尝试将我的网站命名为所有者/管理员,并遇到路由错误。当我尝试创建新的财务报告时 (http://localhost:3000/en/admin/financial_reports/new) 我明白了

No route matches {:controller=>"financial_reports", :format=>nil}

这是我的 routes.rb(缩短)

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
 root :to => 'sites#index'
 get "logout" => "sessions#destroy", :as => "logout"
 get "login" => "sessions#new", as: "login"
namespace :admin do
    root :to => "base#index"
    resources :financial_reports do
      collection do
        get :monthly
        get :yearly
      end
    end
  end
namespace :owner do
    root :to => "base#index"
  end
match "*path", to: "sites#not_found" # handles /en/fake/path/whatever
end
root to: redirect("/#{I18n.default_locale}") # handles /
match '*path', to: redirect("/#{I18n.default_locale}/%{path}")
end

这是我的控制器 app/controllers/admin/financial_reports_controller.rb

class Admin::FinancialReportsController < BaseController

定义索引 @financial_reports = FinancialReport.all 结束

定义新 @financial_report = FinancialReport.new 结束

定义创建 @financial_report = FinancialReport.new(params[:financial_report]) 如果@financial_report.save flash[:notice] = '财务报告上传成功' 重定向到 root_url 别的 flash[:alert] = "财务报告未上传成功" 渲染“索引” 结尾 结尾 每年 @financial_reports = FinancialReport.yearly 结尾 每月定义 @financial_reports = FinancialReport.monthly 结尾 结束

我的看法

app/views/admin/financial_reports/new.html.erb

<%= form_for(@financial_report) do |f| %>
<p>
  <%= f.label(:report_type) %> <br>
  <%= f.radio_button :report_type, "Monthly" %> Monthly |
  <%= f.radio_button :report_type, "Yearly" %> Yearly
</p>
<p>
  <%= f.file_field :file %>
</p>
<p>
  <%= f.submit "Upload Monthly Record" %>  
</p>
<% end %>

知道为什么我会收到此错误吗?谢谢

【问题讨论】:

    标签: ruby-on-rails rails-routing


    【解决方案1】:
    <%= form_for[:admin, @financial_report] do |f| %>
    ........................
    .............
    ........
    

    http://guides.rubyonrails.org/routing.html

    Rails Namespace vs. Nested Resource

    【讨论】:

    • 非常感谢。就是这样,我不会再忘记了。
    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2016-11-12
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多