2

아래에 설명 된 오류 메시지와 같이 중첩 된 리소스에 "resource : profile"을 정의했기 때문에 복수형으로 "user_profiles_path"를 사용하지 않습니다.중첩 된 경로 및 form_for 및 NoMethodError

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 

NoMethodError in Profiles#new 

하는 라인 # 20 제기 /home/smileymike/rails_projects/bffmapp_v2/app/views/profiles/new.html.erb보기 routes.rb :

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

iew : 정보/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

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 
+0

'form_for ([@ profile, @ user]) '를 시도 했습니까? –

+0

네, 저도 시도했습니다. 이상하게도, 위와 같이 세부 사항을 변경하지 않고 웹 페이지를 계속로드했습니다. 문제는 사라졌습니다. – smileyUser

+0

이전에 그런 일이 있었는지, 개발 모드에서도 routes.rb가 수정되었을 때 앱을 다시 시작해야하는 경우가있었습니다. –

답변

6

에 CURRENT_USER의 예시된다 다형성 r을 사용하기위한 형식 그런 outes 항상 새로운 레코드에 대한 pluralized 경로를 사용합니다. 당신은 당신의 형태로 명시해야합니다

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

좋은 소식 그래도 create 경로가 update 경로와 동일합니다.

+0

그것은 아주 잘 작동합니다. 그러나 바깥 괄호 (괄호)없이 작동했습니다. – smileyUser

관련 문제