3

正如下面所述的错误消息,我不使用“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

路线.rb:

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

查看: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
4

1 回答 1

7

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

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

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

于 2012-07-05T01:55:57.193 回答