我遵循了以下教程: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 [:帐号]应该正常吗?因为控制器也是命名空间...
然后我发现了一些关于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
感谢您的任何建议/帮助
答案 0 :(得分:1)
可能是一个错字?
scope "authr", :module => :authr, :as => "auth" do
更改为
scope "authr", :module => :authr, :as => "authr" do #you are missing an r
如果它只是这篇文章中的一个错字并且你在引擎中有正确的话,那么当你使用引擎中相同的范围从父应用程序运行“rake routes”时你会得到什么?
另外,我认为isolate_namespace现在只在edge rails中。 3.1预计会有很多新的引擎好东西,包括这个。