Rails ::引擎命名空间控制器和模型 [英] Rails::Engine namespacing controller and models
问题描述
我遵循以下教程: http://www.themodestrubyist.com/2010/03/05/rails-3-plugins---part-2---writing-an-engine/
这一切都很好。我给控制器命名空间使用
#app / controller / authr / accounts_controller.rb
模块Authr
class AccountsController< ApplicationController
unloadable
def new
@account = Account.new
结束
$ b def创建
@account =帐户。 new(params [:account])
if @ account.save
redirect_to'/'
else
render:action => :新
结束
结束
结束
结束
在教程中,他没有命名空间模型。我想命名空间我的模型,所以它不会与主机应用程序相冲突。所以我尝试了以下内容:
#app / models / authr / account.rb
模块Authr
班级帐户< 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,用户名%>
<%= f.text_field:uname%>
< / p>
< p>
<%= f.label:密码,'密码'%>
<%= f.password_field:password%>
< / p>
< p>
<%= f.submit提交%>
< / p>
<%end%>
但是,当我使用命名空间模型时,出现以下错误:
未定义方法`authr_accounts_path'for#<#< class:0x1038f54e0>:0x1038f3780>
由新方法(@account = Account.new)创建的对象导致: p>
< 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 |
资源:帐户,:只有=> [:new,:create],
:controller => authr / accounts
end
所以这是一个路由选择。当我不命名空间模型一切正常,但是当我命名空间它不起作用。然后我尝试了以下方法:
#routes.rb
Rails.application.routes.draw do | map |
范围authr,:module => :authr,:as => authr做
资源:账户
结束
结束
现在我得到没有路由错误的表单。但是,当我尝试提交表单时,对象未被保存。
开始POST/ authr / accounts为127.0。 0.1在3月28日星期一18:51:12 +0200 2011
Authr :: AccountsController处理#以HTML格式创建
参数:{commit=>Submit,authenticity_token=> cPH8ZmNmgoT84UMnYBoM38di + / OZQmuGQTrSv3HhFR4 =,utf8=>✓,authr_account=> {uname=>usrrrrrrrrrrrrnmmmmeee,password=>[FILTERED]}} $ b $ SQL(48.0ms)BEGIN
SQL(0.5ms)SHOW TABLES
SQL(13.2ms)描述`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
重定向到http:// localhost:3000 /
我知道我在做@account = Account.new(params [:account]),如果我将它改为Account.new(params [:authr_account]我应该工作,但我想用户params [:帐户]应该工作正确吗?因为控制器也是命名空间的......
然后我发现了一些关于isolated_name的空间,所以我试过这个:
< pre $
#lib / authr / engine.rb
需要authr
需要rails
模块Authr
class发动机< Rails :: Engine
isolate_namespace Authr
#engine_name:authr #deprecated?
end
end
我将路线改为:
Rails.application.routes.draw do | map |
资源:帐户,:只有=> [:new,:create],
:controller => authr / accounts
end
但是这给了我以下错误:
/Library/Ruby/Gems/1.8/gems/authr3-0.1.0/lib/authr/engine.rb:6:undefined method Auth :: Engine:Class(NoMethodError)的`isolate_namespace'
我尝试了一切,宝石和他们有命名空间模型。我确信我需要命名空间模型,以确保它们不与主机应用程序冲突。我想使用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
感谢您的任何建议/帮助
可能是一个错字?
范围authr,:module => :authr,:as => authdo
更改为
范围authr,:module => :authr,:as => authrdo#你缺少一个r
如果它在这篇文章中只是一个错字,它在引擎中是正确的,那么当你使用引擎中的相同作用域从父应用程序运行rake routes时,你会得到什么?
另外,我认为isolate_namespace现在只在边缘导轨上。 3.1将会有很多新引擎的好处,包括这个。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
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.
这篇关于Rails ::引擎命名空间控制器和模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!