Rails::Engine 命名空间控制器和模型

发布于 2024-10-26 21:45:27 字数 4136 浏览 1 评论 0原文

我遵循以下教程: 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>

The object create by the new method (@account = Account.new) results in this :

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

路由文件:(当我没有为模型命名空间时,这会起作用。)

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)

我尝试了一切,并且查看了其他宝石我确信我需要为我的模型命名,以确保它们不会与主机应用程序冲突,但我不知道如何使用。我可以解决这个问题,

我正在使用:

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

感谢您的任何建议/帮助。

I followed the following tutorial: http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/

And it all works great. I namespaced the controller using

#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

And in the tutorial he didn't namespace the model. I want to namespace my model though so it doesn't collide with host apps. So i tried the following:

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

This is my view, with a simple form_for that should go to 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 %>

But when i use my namespaced model i get the following error:

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

The object created by the new method (@account = Account.new) results in this :

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

Routes file: (This works when i dont namespace the model.)

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

So this is a routing thing. When i dont namespace the model all works fine but when i namespace it it doesnt work. Then i tried the following:

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

Now i get the form without the routing error. But when i try to submit the form the object isn't saved.

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/

I know that i'm doing @account = Account.new(params[:account]) and if i change it to Account.new(params[:authr_account] that i should work but i want to user params[:account] that should work right? Because the controller is namespaced as well...

Then i found something about isolated_name space so i tried this:

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

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

and i changed my routes to:

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

But this gives me the following error:

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

I tried everything and i looked at other gems and they have namespaced models. I am convinced that i need to namespace my models just to be sure that they don't conflict with the host application. I want to use restfullroutes but i don't know how i can fix this problem.

I am using:

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

Thanks for any advice / help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2024-11-02 21:45:27

可能是拼写错误?

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

更改为

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

如果这只是本文中的一个拼写错误,并且您在引擎中的设置正确,那么当您在引擎中使用相同范围从父应用程序运行“rake 路由”时,您会得到什么?

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

Possibly a typo?

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

change to

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

If its just a typo in this post and you have it correct in the engine, then what do you get when you run "rake routes" from the parent application using that same scope in the engine?

Also, I think isolate_namespace is only in edge rails right now. 3.1 is slated to have alot of new engine goodies including this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文