0

这是我的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

这是命令rake 路由

                 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

这是主/仪表板/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 之一替换。由于这篇文章早些时候这个问题解决了,但是当我在主控制器视图中部分渲染表单和链接时遇到了一个新问题,我对每个点击的链接使用 ajax 并顺便发布了表单,但我认为这个 ajax 方法不应该破坏任何东西。

主/仪表板/masters/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(edit),它打破了错误消息所需的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

这是现在存在的错误,当我创建一个新的主人时它成功运行,但是当我点击链接_编辑表单时不会显示关于缺少 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

4

2 回答 2

0

好的,通过更改编辑链接到路径解决了这个问题,其中:

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

或者

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

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

于 2015-02-04T05:10:35.147 回答
0

这里同样的问题。鉴于命名空间是“ns”。以下:

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

我希望会有所帮助。

于 2016-03-31T15:09:27.460 回答