Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

ruby on rails - No Route Matches: Missing required [:id] when using nested namespace

this is my routes.rb filled with

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

this is command rake routes

                 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

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

the point is i want to make an apps about multi-site like, so the :main namespace replaced by one of dynamic url taken from Master's table. thanks to this post earlier this problem solved, but im facing a new problems when partially render form and link in masters controller view, im using ajax for each link clicked and form posted btw, but i think this ajax method shouldn't break anything.

main/dashboard/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

from here i tried on edit path : edit_main_dashboard_master_path(edit), it breaks with error message required id, so im providing master_url.url, and strangely the error gone

<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

this is the error now exist, when i created a new master it run successfully, but when i clicked the link_to edit the form wont show with error about missing id, then when i clicked the link delete , it destroy sucessfully ..

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

heres the error message

    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

any help are appreciated thanks btw, im using Rails 4.1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Okay this problem was solved by changing the edit link_to path, with:

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

or

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

both of those line will return a correct path, since this question using a nested namespace, removing the 'Edit' parameter makes the error returning Stringify keys gone, anyway thanks @Pavan


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...