Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top