【问题标题】:Rails::Engine namespacing controller and modelsRails::Engine 命名空间控制器和模型
【发布时间】:2011-03-28 09:24:01
【问题描述】:

我按照以下教程进行操作:http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/

这一切都很好。我使用

为控制器命名了
#app/controller/authr/accounts_controller.rb

module Authr
  class AccountsController < ApplicationController
    unloadable

    def new
      @account = Account.new
    end

    def create
      @account = Account.new(params[:account])
      if @account.save
        redirect_to '/'
      else
        render :action => :new
      end
    end
  end
end

在教程中,他没有为模型命名。我想命名我的模型,但它不会与主机应用程序冲突。所以我尝试了以下方法:

#app/models/authr/account.rb
module Authr
    class Account < ActiveRecord::Base
        attr_accessor :password
        validates_confirmation_of :password
    end
end

这是我的观点,一个简单的 form_for 应该去accounts_path

#app/views/authr/accounts/new.html.erb
<%= form_for(@account) do |f|%>
    <p>
        <%= f.label :uname, "Username"%>
        <%= f.text_field :uname%>
    </p>
    <p>
        <%= f.label :password, 'Password'%>
        <%= f.password_field :password%>
    </p>
    <p>
        <%= f.submit "Submit"%>
    </p>
<% end %>

但是当我使用我的命名空间模型时,我收到以下错误:

undefined method `authr_accounts_path' for #<#<class:0x1038f54e0>:0x1038f3780>

由新方法 (@account = Account.new) 创建的对象导致:

<Authr::Account id: nil, uname: nil, hashed_password: nil, remember_token: nil, remember_expiry: nil, created_at: nil, updated_at: nil>

Routes 文件:(当我没有命名模型时,这有效。)

Rails.application.routes.draw do |map|
    resources :accounts,  :only => [:new, :create],
                           :controller => "authr/accounts"
end

所以这是一个路由的事情。当我没有命名空间时,模型一切正常,但是当我命名它时它不起作用。然后我尝试了以下方法:

#routes.rb
Rails.application.routes.draw do |map|
  scope "authr", :module => :authr, :as => "authr" do
    resources :accounts
  end
end

现在我得到了没有路由错误的表单。但是当我尝试提交表单时,对象并没有保存。

Started POST "/authr/accounts" for 127.0.0.1 at Mon Mar 28 18:51:12 +0200 2011
  Processing by Authr::AccountsController#create as HTML
  Parameters: {"commit"=>"Submit", "authenticity_token"=>"cPH8ZmNmgoT84UMnYBoM38di+/OZQmuGQTrSv3HhFR4=", "utf8"=>"✓", "authr_account"=>{"uname"=>"usrrrrrrrrrrrrnmmmmeee", "password"=>"[FILTERED]"}}
  SQL (48.0ms)  BEGIN
  SQL (0.5ms)  SHOW TABLES
  SQL (13.2ms)  describe `accounts`
  AREL (0.3ms)  INSERT INTO `accounts` (`updated_at`, `created_at`, `remember_expiry`, `uname`, `remember_token`, `hashed_password`) VALUES ('2011-03-28 16:51:12', '2011-03-28 16:51:12', NULL, NULL, NULL, NULL)
  SQL (0.4ms)  COMMIT
Redirected to http://localhost:3000/

我知道我正在做 @account = Account.new(params[:account]) 并且如果我将其更改为 Account.new(params[:authr_account] 我应该工作但我想用户 params[: account] 应该可以正常工作吗?因为控制器也是命名空间的......

然后我发现了有关 isolated_name 空间的一些东西,所以我尝试了这个:

#lib/authr/engine.rb
  require "authr"
  require "rails"

module Authr
  class Engine < Rails::Engine
    isolate_namespace Authr
    # engine_name :authr #deprecated?
  end
end

我将路线更改为:

Rails.application.routes.draw do |map|
    resources :accounts,  :only => [:new, :create],
                          :controller => "authr/accounts"
end

但这给了我以下错误:

/Library/Ruby/Gems/1.8/gems/authr3-0.1.0/lib/authr/engine.rb:6: undefined method `isolate_namespace' for Authr::Engine:Class (NoMethodError)

我尝试了所有方法,并查看了其他 gem,它们具有命名空间模型。我确信我需要为我的模型命名,以确保它们不会与主机应用程序冲突。我想使用 restfullroutes 但我不知道如何解决这个问题。

我正在使用:

Daniel-Zs-MacBook-Pro:gem_test Daniel$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
Daniel-Zs-MacBook-Pro:gem_test Daniel$ rails -v
Rails 3.0.3

感谢您的任何建议/帮助

【问题讨论】:

  • 感谢 Cowboycoded 我更新了我的问题。仍在弄清楚为什么参数不匹配...

标签: ruby-on-rails-3 namespaces rubygems rails-engines


【解决方案1】:

可能是笔误?

scope "authr", :module => :authr, :as => "auth" do

改成

scope "authr", :module => :authr, :as => "authr" do #you are missing an r

如果它只是这篇文章中的一个错字,并且您在引擎中将其正确,那么当您使用引擎中的相同范围从父应用程序运行“rake routes”时会得到什么?

另外,我认为isolate_namespace 现在只在边缘轨道中。 3.1 预计会有很多新的引擎好东西,包括这个。

【讨论】:

  • 感谢您对此事的看法。我在我的应用程序中修复了这个问题,这确实是一个错字,所以现在我得到了表格。我遇到的下一个问题是信息没有保存到数据库中: 参数:{"commit"=>"Submit", "authenticity_token"=>"cPH8ZmNmgoT84UMnYBoM38di+/OZQmuGQTrSv3HhFR4=", "utf8"=>"✓", "authr_account"=>{"uname"=>"username1test", "password"=>"[FILTERED]"}} 我知道我在做 @account = Account.new(params[:account]) 但这应该像“正常”一样工作,因为控制器也是命名空间的,对吧?
猜你喜欢
  • 2012-07-08
  • 1970-01-01
  • 2014-05-19
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 2011-05-23
相关资源
最近更新 更多