Rails::Engine namespacing controller and models
我遵循了以下教程:http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
而且一切都很好。我使用
命名控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #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 |
并且在教程中他没有命名模型。我想命名我的模型,这样它就不会与主机应用程序发生冲突。所以我尝试了以下方法:
1 2 3 4 5 6 7 | #app/models/authr/account.rb module Authr class Account < ActiveRecord::Base attr_accessor :password validates_confirmation_of :password end end |
这是我的观点,有一个简单的 form_for 应该转到accounts_path
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #app/views/authr/accounts/new.html.erb <%= form_for(@account) do |f|%> <p><center>[wp_ad_camp_2]</center></p><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 %> |
但是当我使用我的命名空间模型时,我收到以下错误:
1 | undefined method `authr_accounts_path' for #<#<class:0x1038f54e0>:0x1038f3780> |
新方法创建的对象(@account = Account.new)导致:
1 | <Authr::Account id: nil, uname: nil, hashed_password: nil, remember_token: nil, remember_expiry: nil, created_at: nil, updated_at: nil> |
路由文件:(当我没有命名模型时这有效。)
1 2 3 4 | Rails.application.routes.draw do |map| resources :accounts, :only => [:new, :create], :controller =>"authr/accounts" end |
所以这是一个路由问题。当我不命名时,模型一切正常,但是当我命名时它不起作用。然后我尝试了以下操作:
1 2 3 4 5 6 | #routes.rb Rails.application.routes.draw do |map| scope"authr", :module => :authr, :as =>"authr" do resources :accounts end end |
现在我得到了没有路由错误的表格。但是当我尝试提交表单时,对象没有保存。
1 2 3 4 5 6 7 8 9 | 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"=>"a?"","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 space的东西,所以我尝试了这个:
1 2 3 4 5 6 7 8 9 10 | #lib/authr/engine.rb require"authr" require"rails" module Authr class Engine < Rails::Engine isolate_namespace Authr # engine_name :authr #deprecated? end end |
然后我将路线更改为:
1 2 3 4 | Rails.application.routes.draw do |map| resources :accounts, :only => [:new, :create], :controller =>"authr/accounts" end |
但这给了我以下错误:
1 | /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,但我不知道如何解决这个问题。
我正在使用:
1 2 3 4 | 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 |
感谢您的建议/帮助
可能是笔误?
1 | scope"authr", :module => :authr, :as =>"auth" do |
改为
1 | scope"authr", :module => :authr, :as =>"authr" do #you are missing an r |
如果这只是这篇文章中的一个错字,而您在引擎中正确地输入了它,那么当您在引擎中使用相同的作用域从父应用程序运行"rake 路由"时,您会得到什么?
另外,我认为isolate_namespace 现在只在边缘Rails中。 3.1 预计会有很多新的引擎好东西,包括这个。