次のチュートリアルに従いました: 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>
new メソッド (@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)
私はすべてを試し、他のgemを調べましたが、それらには名前空間付きのモデルがあります。モデルがホスト アプリケーションと競合しないようにするためだけに、モデルに名前を付ける必要があると確信しています。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
アドバイス/助けてくれてありがとう