我遵循以下教程: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>
路由文件:(当我没有命名模型时,这有效。)
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)
我尝试了所有方法,并查看了其他宝石,它们具有命名空间模型。我确信我需要命名我的模型只是为了确保它们不会与主机应用程序冲突。我想使用 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
感谢您的任何建议/帮助