遇到一个奇怪的错误:未定义的方法`user_profiles_path'

时间:2012-07-01 16:13:51

标签: ruby-on-rails

这个错误告诉我 - 未定义的方法'user_profiles_path',而我的路由就像'user_profile_path'。 Profile是用户的单身子资源。不确定导致此错误的原因。 _form.html.erb中的<%= form_for [@user, @profile] do |f| %>引发了错误。

routes.rb中:

devise_for :users, :path_names => { :sign_in => "login", :sign_up => "register" } do   
   get "/login", :to => "devise/sessions#new"
   get "/register", :to => "devise/registrations#new"
   get "/logout", :to => "devise/sessions#destroy"
   get '/account' => 'devise/registrations#edit'
 end

  root :to => "questions#redirect_on_visit" 

  match 'home', :to => "questions#index"

  resources :questions do
    resources :question_responses
  end

  resources :users do
    resource :profile
  end

_form.html.erb:

<%= form_for [@user, @profile] do |f| %>

 <%= f.error_messages %>    

 <div class="field">
    <%= f.label :display_name, "Display Name" %><br />
    <%= f.text_field :display_name, :size => "43" %><br />
 </div>

 <div class="field">
    <%= f.label :current_location, "Current Location" %><br />
    <%= f.text_field :current_location, :size => "43" %><br />
 </div>

 <div><%= f.label :nationality, "Nationality" %><br />
    <%= f.collection_select :nationality, Profile::NATIONALITY, :include_blank => true %>
 </div><br />

 <div><%= f.label :home_place, "Home Place" %><br />
    <%= f.collection_select :home_place, Profile::HOME_PLACE, :include_blank => true %>
 </div><br />

  <div><%= f.label :occupation, "Occupation" %><br />
    <%= f.collection_select :occupation, Profile::OCCUPATION, :include_blank => true %>
  </div><br />

  <div><%= f.label :interest, "Interests" %><br />
    <%= f.collection_select :interest, Profile::INTERESTS, :include_blank => true %>
  </div><br />

  <div><%= f.label :hobby, "Hobbies" %><br />
    <%= f.collection_select :hobby, Profile::HOBBIES, :include_blank => true %>
  </div><br />

  <div class="field">
    <%= f.label :bio, "Short Bio" %><br />
    <%= f.text_area :bio, :size => "50x5" %>
  </div>

  <div class="submit">
    <%= f.submit "Create Profile" %>
  </div>

<% end %>

profiles_controller.rb:

class ProfilesController < ApplicationController

  before_filter :find_user

  def new
    @profile = @user.build_profile    
  end

  def edit

  end

  private

  def find_user
    @user = User.find(params[:user_id])
  end
end

application.html.erb:

<% if user_signed_in? %>
            <% if !current_user.try(:profile) %>
                Signed in as<div><%= link_to current_user.email, new_user_profile_path(current_user.id) %></div><br /><br />
            <% else %>
                Signed in as<div><%= link_to current_user.email, edit_user_profile_path(current_user.id) %></div><br /><br />
            <% end %>           
            Not you? <%= link_to "Sign out", logout_path %>
        <% else %>
            <%= link_to "Sign up", new_user_registration_path %> or <%= link_to "Sign in", new_user_session_path %>
        <% end %>

路线:

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)           

2 个答案:

答案 0 :(得分:1)

好的,此错误是此处报告的已知错误:https://github.com/rails/rails/issues/1769

我找到了指定网址的正确方法。

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

具有大括号的语法非常重要,重要的是form_for和括号之间没有空格。

答案 1 :(得分:0)

您可以通过在表单中​​手动指定URL来更正此问题:

<%= form_for [@user, @profile], :url => user_profile_path(@user) do |f| %>