嵌套资源未保存的管理名称空间 [英] admin namespace with nested resources not saving

查看:75
本文介绍了嵌套资源未保存的管理名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在管理命名空间中创建嵌套资源时遇到问题.我对命名空间有些陌生.我已经阅读了Stack上的几篇文章,但无济于事,也许是我错误地实现了这一点.

I am having an issue with creating nested resources in my admin namespace. I am somewhat new to namespaces. I have read several posts on Stack but to no avail, perhaps i am implementing this incorrectly.

我有一个管理模型(设计),并且正在尝试将地址嵌套到我创建的自定义用户显示页面上,这就是问题所在.

I have an admin model (devise), and am trying to nest an address to the custom user show page I have created, this is where the problem is.

尝试为管理员创建新地址时,提交表单时出现此错误:

When trying to create a new address for an admin I get this error when I submit the form:

ActionController::UrlGenerationError at /admin/admins/2CE0/addresses
No route matches {:action=>"show", :admin_id=>"2CE0", :controller=>"admin/admins/admin/addresses"} missing required keys: [:id]

更好的错误页面的屏幕截图:

A Screen Shot of the Better Errors Page:

我的Routes.rb

  namespace :admin do
    devise_for :admins, controllers: { sessions: 'admin/admins/sessions', passwords: 'admin/admins/passwords', registrations: 'admin/admins/registrations', confirmations: 'admin/admins/confirmations', unlocks: 'admin/admins/unlocks', shared: 'admin/admins/shared' }

    resources :admins, controller: 'admins/admin', shallow: true do
      resources :addresses, except: [:index], controller: 'admins/admin/addresses'
    end

  end

  ###Admin Authentication Scope###
  devise_scope :admin do
    authenticated  do
      root to: 'admin/admin_static#home', as: 'authenticated_admin'
    end
  end

耙路输出:

  admin_admin_addresses POST   /admin/admins/:admin_id/addresses(.:format)     admin/admins/admin/addresses#create
new_admin_admin_address GET    /admin/admins/:admin_id/addresses/new(.:format) admin/admins/admin/addresses#new
     edit_admin_address GET    /admin/addresses/:id/edit(.:format)             admin/admins/admin/addresses#edit
          admin_address GET    /admin/addresses/:id(.:format)                  admin/admins/admin/addresses#show
                        PATCH  /admin/addresses/:id(.:format)                  admin/admins/admin/addresses#update
                        PUT    /admin/addresses/:id(.:format)                  admin/admins/admin/addresses#update
                        DELETE /admin/addresses/:id(.:format)                  admin/admins/admin/addresses#destroy

管理员模型:

class Admin < ActiveRecord::Base
  before_create :generate_admin_ident

  # Devise Modules
  devise :database_authenticatable, :registerable,:recoverable, :rememberable,
         :trackable, :validatable, :confirmable, :lockable, :timeoutable

  # Model Relationships
  has_many :addresses, dependent: :destroy

  # Model Validations
  validates_uniqueness_of :admin_ident

  #Unique Identifier Generation
  def generate_admin_ident
    begin
      self.admin_ident = SecureRandom.hex(2).upcase
      other_admin = Admin.find_by(admin_ident: self.admin_ident)
    end while other_admin
  end

  # Vanity URL
  def to_param
    admin_ident
  end

end

管理控制器:-注意,此操作仅控制自定义管理索引和显示页面

The Admin Controller: - Note this only controls a custom admin index and show page

class Admin::Admins::AdminController < ApplicationController
  before_action :authenticate_admin_admin!

  def index
    @admins = Admin.all
  end

  def show
    @admin = Admin.find_by_admin_ident(params[:id])
    @addresses = @admin.addresses
  end

  def new
  end

  def edit
  end

  def create
  end

  def update
  end

  def destroy
  end

end

地址模型:

class Address < ActiveRecord::Base
  before_create :generate_address_ident

  # Model Relationships
  belongs_to :admin

  # Model Validations
  validates_uniqueness_of :address_ident

  #Unique Identifier Generation
  def generate_address_ident
    begin
      self.address_ident = SecureRandom.hex(4).upcase
      other_address = Address.find_by(address_ident: self.address_ident)
    end while other_address
  end

  # Vanity URL
  def to_param
    address_ident
  end

end

地址控制器:

class Admin::Admins::Admin::AddressesController < ApplicationController
  before_action :authenticate_admin_admin!
  before_action :set_address, only: [:show, :edit, :update, :destroy]

  # GET /addresses
  # GET /addresses.json
  def index
    @addresses = Address.all
  end

  # GET /addresses/1
  # GET /addresses/1.json
  def show
  end

  # GET /addresses/new
  def new
    @admin = Admin.find(params[:admin_id])
    @address = Address.new
  end

  # GET /addresses/1/edit
  def edit
  end

  # POST /addresses
  # POST /addresses.json
  def create
    @admin = Admin.find_by_id(params[:admin_id])
    @address = Address.new(address_params)

    respond_to do |format|
      if @address.save
        format.html { redirect_to admin_admin_address_url[:admin, @admins, @address], notice: 'Address was successfully created.' }
        format.json { render :show, status: :created, location: @address }
      else
        format.html { render :new }
        format.json { render json: @address.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /addresses/1
  # PATCH/PUT /addresses/1.json
  def update
    respond_to do |format|
      if @address.update(address_params)
        format.html { redirect_to @address, notice: 'Address was successfully updated.' }
        format.json { render :show, status: :ok, location: @address }
      else
        format.html { render :edit }
        format.json { render json: @address.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /addresses/1
  # DELETE /addresses/1.json
  def destroy
    @address.destroy
    respond_to do |format|
      format.html { redirect_to addresses_url, notice: 'Address was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_address
      @address = Address.find_by_address_ident(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def address_params
      params.require(:address).permit(:admin_id, :street_number, :street_name, :street_type, :grid, :city, :province, :postal_code, :current)
    end
end

最后,当尝试共同创建新的管理员地址时,Rails Server控制台输出:

Started POST "/admin/admins/2CE0/addresses" for ::1 at 2016-05-25 22:54:40 -0600
Processing by Admin::Admins::Admin::AddressesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"i+B7DS85BtWIA9xsAQnzPByayTZ9Z4fI4rTkEcBqfgkQw9CFTFUMaRhUDyQKYgzA9SPNYWJIkyU8lcnyODYKuw==", "address"=>{"admin_id"=>"", "street_number"=>"1234", "street_name"=>"something", "street_type"=>"AVE", "grid"=>"NW", "city"=>"YYC", "province"=>"AB", "postal_code"=>"123 456", "current"=>"0"}, "commit"=>"Create Address", "admin_id"=>"2CE0"}
  Admin Load (0.2ms)  SELECT  "admins".* FROM "admins" WHERE "admins"."id" = $1  ORDER BY "admins"."id" ASC LIMIT 1  [["id", 1]]
  Admin Load (0.4ms)  SELECT  "admins".* FROM "admins" WHERE "admins"."id" = $1 LIMIT 1  [["id", 2]]
   (0.1ms)  BEGIN
  Address Exists (0.2ms)  SELECT  1 AS one FROM "addresses" WHERE "addresses"."address_ident" IS NULL LIMIT 1
  Address Load (0.1ms)  SELECT  "addresses".* FROM "addresses" WHERE "addresses"."address_ident" = $1 LIMIT 1  [["address_ident", "1F185CE6"]]
  SQL (0.1ms)  INSERT INTO "addresses" ("street_number", "street_name", "street_type", "grid", "city", "province", "postal_code", "current", "created_at", "updated_at", "address_ident") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"  [["street_number", "1234"], ["street_name", "something"], ["street_type", "AVE"], ["grid", "NW"], ["city", "YYC"], ["province", "AB"], ["postal_code", "123 456"], ["current", "f"], ["created_at", "2016-05-26 04:54:40.377296"], ["updated_at", "2016-05-26 04:54:40.377296"], ["address_ident", "1F185CE6"]]
   (5.9ms)  COMMIT
Completed 500 Internal Server Error in 39ms (ActiveRecord: 9.5ms)

ActionController::UrlGenerationError - No route matches {:action=>"show", :admin_id=>"2CE0", :controller=>"admin/admins/admin/addresses"} missing required keys: [:id]:

在此先感谢大家,我在这里束手无策,不知道我从哪儿掉进了Rabbit洞!请询问您是否需要更多信息,我已尝试提供尽可能多的信息!

Thank you all in advance, I am in a bind here and not sure where I fell down the Rabbit hole! Please ask if you need more information, I have tried to provide as much as possible!

编辑#1 -添加控制器树的照片

EDIT # 1 - Adds Photo Of Controller Tree

推荐答案

如果您不熟悉命名空间,建议您创建一个新应用,然后使用脚手架生成器在管理员命名空间中创建地址.然后查看脚手架构建对象的方式,并将其与您拥有的内容进行比较.

If you are new to name spacing, I'd recommend creating a new app, and then using a scaffold generator to create an address in an admin names space. Then look at the way the scaffold builds the objects, and compare that to what you have.

rails new name_space_play
cd name_space_play
rails g scaffold Admin::Address street_number street_name street_type grid city province postal_code current

Scaffold将按照Rails开发人员期望的方式构建文件,并且应该向您展示如何操作.

Scaffold will build the files in the way the rails developers expect them to be built, and that should show you how to do it.

这篇关于嵌套资源未保存的管理名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆