开发者

Using rails3 namespace question

I am a new guy, a java programmer.

Using namespace encountered difficulties in rails3.

rails g scaffold ns::e1 name:string  
rails g model ns::e2 name:string e1:references  
rake db:migrate

edit app/models/ns/e1.rb

class Ns::E1 < ActiveRecord::Base  
  has_many :e2s  
end

edit app/views/ns/e1s/show.html.erb

<p id="notice"><%= notice %></p>  
<p>  
  <b>Name:</b>  
  <%=@ns_e1.name%>  
</p>  
<%= form_for([@ns_e1, @ns_e1.e2s.build]) do |f| %>  
  <div class="field">  
    <%= f.label :name %>  
    <br/>  
    <%= f.text_field :name %>  
  </div>  
  <div class="actions">  
    <%= f.submit %>  
  </div>  
<% end 开发者_开发知识库%>  
<%= link_to 'Edit', edit_ns_e1_path(@ns_e1)%>|  
<%= link_to 'Back', ns_e1s_path %>  

Error is undefined method `ns_e1_ns_e2s_path'

How to configure routes.rb ? Thanks

Thanks ream88, I found a solution.

Edit the routes.rb.

namespace :ns do
  resources :e1s do
    resources :e2s, :as => :ns_e2s
  end
end


I've used named-spaced models since I started with Rails back in 2007, and unfortunately Rails isn't very good at handling name-spaces. Its very better now with Rails 3, but you still have to write more code than usual. And I'm always ending up, writing my own routes without namespaces. For example:

I have following two models:

class MyAwesomeBlog::User < AR::Base
end

class MyAwesomeBlog::Post < AR::Base
end

# routes.rb
resources :users
resources :posts

Is this an option for you?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜