作爲錯誤消息下面所述,我不使用「user_profiles_path」作爲複數,因爲我在嵌套的資源中定義「資源:配置文件」。嵌套的路線和form_for和NoMethodError
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
v 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
你試過'form_for([@ profile,@ user])'嗎? –
是的,我也試過。奇怪的是,我繼續加載網頁而沒有改變上面顯示的細節。問題消失了。 – smileyUser
我以前見過這種情況,有時我必須在routes.rb被修改時重新啓動應用程序,即使在開發模式下也是如此。 –