嵌套路由和form_for以及NoMethodError

时间:2012-07-05 00:55:35

标签: ruby-on-rails ruby-on-rails-3 controller form-for nested-resources

正如下面所述的错误消息,我不使用“user_profiles_path”作为复数,因为我在嵌套资源中定义了“resource:profile”。

NoMethodError in Profiles#new

显示/home/smileymike/rails_projects/bffmapp_v2/app/views/profiles/new.html.erb第20行:

undefined method `user_profiles_path' for #<#<Class:0x90266ac>:0xa041294>

型号:

class User < ActiveRecord::Base
  has_one :profile

class Profile < ActiveRecord::Base
  attr_accessible :name, :surname
  belongs_to :user

routes.rb中:

  resources :users do
    resource :profile  (note: has_one)
  end

view:profiles / new.html.erb

<div class="row">
  <div class="span6 offset3">
    <%= form_for([@user, @profile]) do |f| %>
      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :surname %>
      <%= f.text_field :surname %>

      <%= f.submit "Create my profile", class: "btn btn-large btn-primary" %>
    <% end %>
  </div>
</div>

路由

    user_profile POST   /users/:user_id/profile(.:format)      profiles#create
 new_user_profile GET    /users/:user_id/profile/new(.:format)  profiles#new
edit_user_profile GET    /users/:user_id/profile/edit(.:format) profiles#edit
                  GET    /users/:user_id/profile(.:format)      profiles#show
                  PUT    /users/:user_id/profile(.:format)      profiles#update
                  DELETE /users/:user_id/profile(.:format)      profiles#destroy
            users GET    /users(.:format)                       users#index
                  POST   /users(.:format)                       users#create
         new_user GET    /users/new(.:format)                   users#new
        edit_user GET    /users/:id/edit(.:format)              users#edit
             user GET    /users/:id(.:format)                   users#show
                  PUT    /users/:id(.:format)                   users#update
                  DELETE /users/:id(.:format)                   users#destroy
         sessions POST   /sessions(.:format)                    sessions#create
      new_session GET    /sessions/new(.:format)                sessions#new
          session DELETE /sessions/:id(.:format)                sessions#destroy
             root        /                                      static_pages#home
           signup        /signup(.:format)                      users#new
           signin        /signin(.:format)                      sessions#new
          signout DELETE /signout(.:format)                     sessions#destroy
             help        /help(.:format)                        static_pages#help
            about        /about(.:format)                       static_pages#about
          contact        /contact(.:format)                     static_pages#contact

控制器:

class ProfilesController < ApplicationController
    def show
    end

  def new
    @user = current_user
    @profile = current_user.build_profile()
  end

  def edit
  end

  def create
  end

  def update
  end

  def destroy
  end
end
下面的

是profiles_controller.rb

中current_user的说明
module SessionsHelper
    def sign_in(user)
    cookies.permanent[:remember_token] = user.remember_token
    self.current_user = user
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user=(user)
    @current_user = user
  end

  def current_user
    @current_user ||= User.find_by_remember_token(cookies[:remember_token])
  end

  def sign_out
    self.current_user = nil
    cookies.delete(:remember_token)
  end
end

1 个答案:

答案 0 :(得分:6)

使用像这样的多态路由的表单将始终使用复数路径来创建新记录。您需要在表单中明确:

form_for([@user, @profile], :url => user_profile_path(@user))

好消息是,create路线与update路线相同。