无路由匹配:使用嵌套命名空间时缺少必需的[:id]

时间:2015-01-21 20:21:45

标签: ruby-on-rails routing link-to simple-form-for

这是我的 routes.rb 填充

Rails.application.routes.draw do
  namespace :main, path: ':master_url' do
    root 'sites#index'
    namespace :dashboard do
        root 'dashboards#index'
        #get 'masters/index'
      resources :masters
    end
    get "/:action" => 'sites#:action'
  end
  root 'main/sites#index'

end

这是命令佣金路线

                 main_root GET    /:master_url(.:format)                            main/sites#index
       main_dashboard_root GET    /:master_url/dashboard(.:format)                  main/dashboard/dashboards#index
    main_dashboard_masters GET    /:master_url/dashboard/masters(.:format)          main/dashboard/masters#index
                           POST   /:master_url/dashboard/masters(.:format)          main/dashboard/masters#create
 new_main_dashboard_master GET    /:master_url/dashboard/masters/new(.:format)      main/dashboard/masters#new
edit_main_dashboard_master GET    /:master_url/dashboard/masters/:id/edit(.:format) main/dashboard/masters#edit
     main_dashboard_master GET    /:master_url/dashboard/masters/:id(.:format)      main/dashboard/masters#show
                           PATCH  /:master_url/dashboard/masters/:id(.:format)      main/dashboard/masters#update
                           PUT    /:master_url/dashboard/masters/:id(.:format)      main/dashboard/masters#update
                           DELETE /:master_url/dashboard/masters/:id(.:format)      main/dashboard/masters#destroy
                      main GET    /:master_url/:action(.:format)                    main/sites#:action
                      root GET    /                                                 main/sites#index

这是 main / dashboard / masters_controller.rb

class Main::Dashboard::MastersController < ApplicationController
respond_to :html, :js
before_action :all_masters, only: [:index, :create, :update, :destroy]
before_action :set_master, only: [:edit, :update, :destroy]
before_action :init_master
layout 'main'


def new
    @master = Master.new
end
def create
    @master = Master.create(conf_params) 
end

def update
    @master.update_attributes(conf_params)
end

def destroy
    @master.destroy
end

private
def all_masters
    @masters = Master.all
end
def set_master
    @master = Master.find(params[:id])
end
def conf_params
    params.require(:master).permit(:title,:url)
end
def init_master
    @master_url = Master.find_by_url(params[:master_url])
  end
end

关键是我想创建一个关于多站点的应用程序,所以:main命名空间被一个从Master的表中获取的动态URL替换。感谢this post earlier这个问题解决了,但我在主控制器视图中部分渲染表单和链接时遇到了新的问题,我使用ajax为每个链接点击并形成发布的btw,但我认为这个ajax方法不应该破解任何东西。

主要/仪表板/主人/ index.js.erb的

$('#content').html("<%= j (render 'index') %>");
$('#master-list').html("<%= j (render @masters) %>");

... / _ index.html.erb

<!-- Page Heading -->
<div class="row">
    <div class="col-lg-12">
        <h1 class="page-header">
            Master Conference <small>
            <%= link_to new_main_dashboard_master_path, remote: true do %>
                <button class="btn btn-default">Create New</button>
            <% end %>
        </small>
        </h1>
    </div>
</div>

<div class="row">
    <div class="col-lg-6">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-list fa-fw"></i> All Conference</h3>
            </div>
            <div class="panel-body">
                <table class="table table-bordered" id="master-list"></table>
            </div>
        </div>
    </div>
    <div class="col-lg-6" >
        <div id="form-master" style:"display:none;"></div>
    </div>

</div>
<!-- /.row -->

... / _ master.html.erb

从这里我尝试编辑路径:edit_main_dashboard_master_path(编辑),它打破了错误消息所需的id,所以我提供master_url.url,奇怪的是错误消失了

<tr>
  <td><%= master.id %></td>
  <td><%= master.title %></td>
  <td><%= master.url%></td>
  <td>
    <center>
    <%= link_to edit_main_dashboard_master_path(@master_url.url, master), remote: true do %>
      <button class="btn btn-primary btn-xs">Edit</button>
    <% end %>
    <%= link_to main_dashboard_master_path(@master_url.url, master), remote: true, method: :delete, data: {confirm:  'Are you sure?'} do %>
      <button class="btn btn-danger btn-xs">Delete</button>
    <% end %>
    </center>
  </td>
</tr>

... / _ form_master.html.erb

这是现在存在的错误,当我创建新主服务器时它成功运行,但当我点击link_to编辑表单时不会显示错误ID ,然后当我点击链接删除时,它成功销毁 ..

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-pencil fa-fw"></i>Add/Edit Conference</h3>
    </div>
    <div class="panel-body">
        <%= simple_form_for [:main,:dashboard, master], remote: true do |f| %>
            <%= f.input :url, label: 'URL' %>
            <%= f.input :title, label: 'Title' %>
            <%= f.button :submit %>
        <% end %>
    </div>
</div>

发出错误消息

    ActionView::Template::Error (No route matches {:action=>"show", :controller=>"main/dashboard/masters", :format=>nil, :id=>nil,
    :master_url=>#<Master id: 1, url: "tes3", title: "Conference Test 3", created_at: "2015-01-19 19:18:41", updated_at: "2015-01-20 17:41:34">} missing required keys: [:id]):
3:         <h3 class="panel-title"><i class="fa fa-pencil fa-fw"></i>Add/Edit Conference</h3>
    4:     </div>
    5:     <div class="panel-body">
    6:      <%= simple_form_for [:main,:dashboard, master], remote: true do |f| %>
    7:                  <%= f.input :url, label: 'URL' %>
    8:                  <%= f.input :title, label: 'Title' %>
    9:                  <%= f.button :submit %>
  app/views/main/dashboard/masters/_form_master.html.erb:6:in `_app_views_main_dashboard_masters__form_master_html_erb__87590790_31195656'
  app/views/main/dashboard/masters/edit.js.erb:1:in `_app_views_main_dashboard_masters_edit_js_erb

任何帮助表示感谢 顺便说一句,即时通讯使用Rails 4.1

2 个答案:

答案 0 :(得分:0)

好的,这个问题通过改变来解决了 编辑link_to路径,使用:

link_to edit_main_dashboard_master_path(@master_url.url, master), remote: true do

 link_to :controller=>"masters",:action =>"edit",:id => master do

这两行都将返回正确的路径,因为这个问题使用嵌套命名空间,删除'Edit'参数会导致错误返回Stringify键,无论如何,感谢@Pavan

答案 1 :(得分:0)

这里的问题相同。 鉴于命名空间是&#34; ns&#34;。 以下内容:

= link_to "User detail", @user

= link_to "User detail", ns_user_path(@user)

将生成此错误:

ActionView::Template::Error: No route matches {:action=>"show", :controller=>"ns/users", :id=>nil, ...

解决使用:

= link_to "User detail", ns_user_path(id: @user.id)

我希望能有所帮助。