Humming
Humming

Reputation: 453

Stuck with a strange error: undefined method `user_profiles_path'

This error tells me - undefined method 'user_profiles_path', while my routes are like 'user_profile_path'. Profile is a singleton child resource of users. Not sure what is causing this error. The error is raised by <%= form_for [@user, @profile] do |f| %> in _form.html.erb.

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 %>

Routes:

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)           

Upvotes: 1

Views: 821

Answers (2)

Humming
Humming

Reputation: 453

Okay this error is a known bug as reported here: https://github.com/rails/rails/issues/1769

And I found the right way to specify URL.

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

Syntactically important to have braces and important that there is no space between form_for and parenthesis.

Upvotes: 1

Veraticus
Veraticus

Reputation: 16084

You can correct this by manually specifying the URL in the form:

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

Upvotes: 0

Related Questions