开发者

Trouble on submitting a namespaced Account using its User ActiveRecord association

I am using Ruby on Rails 3 and I am trying to submit namespaced Account using an ActiveRecord association.

In the router I have

resources :users

namespace "users" do
  resources :accounts
end

In the User model I have:

class User < ActiveRecord::Base
  has_one :account,
    :class_name  => "Users::Account"
end

In the User controller I have

class UsersController < ApplicationController
  def new
    @user = User.new
    ...
  end
end

In order to submit the User Account, in the form view I tryed to do like the following:

<%= form_for([:users, @user.account]}) 开发者_开发知识库do |f| %>
  ...
<% end %>

but that doesn't work. So what I have to do to make it to work?


Some things look mixed up. You have view for users, but you are creating User::Account there?! In controller you didn't create that account. Perhaps:

@user = User.new :account => Users::Account.new

But pay attention that you either need to move your logic in Users::AccountController or if you want to create both User and it's account in the same view, than you use nested_forms for account and than you should post form to your @user model with accepting_nesting_attributes for account too.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜