2012-07-01 2 views
1

Эта ошибка говорит мне - undefined method' user_profiles_path ', в то время как мои маршруты похожи на' user_profile_path '. Профиль - это однопользовательский дочерний ресурс пользователей. Не уверен, что вызывает эту ошибку. Ошибка повышается на <%= form_for [@user, @profile] do |f| %> в _form.html.erb.Застрял со странной ошибкой: undefined method `user_profiles_path '

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

Я думаю, что вы должны сделать свой вложенный маршрут множественным - или вам нужно будет указать опцию url в вашей форме_for – house9

+0

Уже пробовали оба эти предложения. Не работает. Сделать это множественное число в любом случае не является реальным решением. Он должен быть одним профилем для каждого пользователя. – Humming

+0

Зачем вам нужно определять ресурс профиля как ресурс singleton, когда его только нужно, у пользователя есть один профиль - не может ли это быть применено с использованием отношения: has_one? –

ответ

1

Хорошо эта ошибка известная ошибка, описанная здесь: https://github.com/rails/rails/issues/1769

И я нашел правильный способ указать URL.

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

Синтаксически важно иметь фигурные скобки и важно, что нет пространства между form_for и скобкой.

0

Вы можете исправить это вручную указав URL в виде:

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

Это дает еще одну ошибку, что-то вроде «неправильного количества аргументов». И это еще одна странная вещь. – Humming

+0

Правильно, да. К сожалению, это должен быть 'user_profile_path (@user)'. Я отредактирую исходный пост. – Veraticus

+0

Да даже это :) Не имеет значения для ошибки. – Humming

Смежные вопросы