I’m facing a problem with a nested route in Rails an I can’t figure out, what I’m doing wrong.
In a nutshell: I use Devise for authentication an registration. Now I want to have the user enter more detailed information about his contact data. To keep the user model small, I want to use a different model called account. As a user only will have one account, I use a on-to-one association and a nested route. But somehow the routing does not work.
This is my user model:
user.rb
class User < ActiveRecord::Base
has_one :account, :dependent => :destroy
attr_accessible :username, :email, :password, :password_confirmation, :remember_me
and this is my account model:
account.rb
class Account < ActiveRecord::Base
attr_accessible :anrede, :land, :nachname, :plz, :stadt, :strasse, :user_id, :vorname
belongs_to :user
end
and my route file looks like this:
routes.rb
devise_for :users, :path => 'members'
resources :users do
resource :account
end
As a user might not have a account yet, I test this in my view:
<% if current_user.account.try %>
<li><%= link_to "Account", user_account_path %></li>
<% else %>
<li><%= link_to "create Account", new_user_account_path %></li>
<% end %>
but when I enter the root path with a signed in user, Rails tells me
Routing Error
No route matches {:action=>"new", :controller=>"accounts"}
but there is a new action in my accounts_controller.rb as I have scaffolded the whole CRUD set (edited create with current_user.build_account) and it’s also the path given by rake routes.
I’m desperately stuck in this! Could anybody help me, please?
EDIT
This is the output of my rake routes:
user_account POST /users/:user_id/account(.:format) accounts#create
new_user_account GET /users/:user_id/account/new(.:format) accounts#new
edit_user_account GET /users/:user_id/account/edit(.:format) accounts#edit
GET /users/:user_id/account(.:format) accounts#show
PUT /users/:user_id/account(.:format) accounts#update
DELETE /users/:user_id/account(.:format) accounts#destroy
EDIT2
this is the error message for the action new form:
NoMethodError in Accounts#new
Showing /home/stonjarks/Work/toytrade_devise/app/views/accounts/_form.html.erb where line #1 raised:
undefined method `accounts_path' for #<#<Class:0xa44d0cc>:0xab6222c>
Extracted source (around line #1):
1: <%= form_for(@account) do |f| %>
2: <% if @account.errors.any? %>
3: <div id="error_explanation">
4: <h2><%= pluralize(@account.errors.count, "error") %> prohibited this account from being saved:</h2>
I solved this using this SO hack, but it’s a strange behavior anyway:
Rails Nested Route For Singular Resource
<%= form_for @account,:url=>{:action=>:create}
But I still don’t get the point of this routing anyway. Despite this, I can’t manage to find a route to show the account:
/users/1/account
ActiveRecord::RecordNotFound in AccountsController#show
Couldn't find Account without an ID
new_user_account_pathis missing a user instance which should be provided for example:new_user_account_path(current_user)If you look at the following line:
You can see that the path demands a :user_id – a user instance.
You can read here for further clarification of your problem.