7

I am trying to get a basic form to work and am struggling because I keep getting the error

 undefined method `profiles_index_path' for #<#<Class:0x4fe1ba8>:0x4fccda0>

I have checked through and can't seem to work out where I am going wrong.

In my view (new.html.erb) I have:

 <%= form_for @profile do |f| %>

 <%= f.text_field :name %>
 <%= f.text_field :city %>
 <%= f.text_field :country %>
 <%= f.text_field :about %>

 <%= f.submit "Create Profile" %>

 <% end %>

In my profiles controller I have:

class ProfilesController < ApplicationController

def new
  @title = "New Profile"
  @profile = Profiles.new
end

def create
  @user = current_user
  @profile = @user.profiles.new(params[:profile])
  if @profile.save
    redirect_to profile_path, :notice => "Welcome to your new profile!"
  else
    render "profiles#new"
  end
end

def edit
  @user = current_user
  @profile = @user.profiles.find(params[:id])
end

def update
  @title = "Update Profile"

  @user = current_user
  @profile = @user.profiles.find(params[:id])

  if @profile.update_attributes(params[:profile])
    redirect_to profile_path
  else
    render action: "edit" 
  end
end

def index
  @user = current_user
  @profile = @user.profiles.all
  @title = "Profile"
end

end

And finally in my profiles model I have

class Profiles < ActiveRecord::Base

belongs_to :user

end

Any help people can offer really would be much appreciated because I am stumped. :)

Sorry forgot to include routes:

  controller :profiles do
   get "newprofile" => "profiles#new"
   get "updateprofile" => "profiles#update"
   get "profile" => "profiles#home"
  end

  resources :profiles, :controller => 'profiles'
3
  • do rake routes to check correct path to index method Commented May 7, 2012 at 18:13
  • What does your routes.rb file look like? Commented May 7, 2012 at 18:14
  • Hey, I have added my relevant routes file so that you can see what I have. The view is also called new.html.erb because it is for creating new profiles. Thanks Commented May 7, 2012 at 18:24

5 Answers 5

6

The problem is indeed the way you've pluralized your model name. Don't do that. It should be a Profile, not a Profiles. There my be some work around to allow you to use a plural model name, but the answer is to stick to Rails convention rather than fighting the framework. Rename your model to Profile and the url_for helpers will understand how to correctly turn a new Profile object into a /profiles URL.

Sign up to request clarification or add additional context in comments.

2 Comments

Amazing this seems to have worked! thanks for your help. Models = singular, controllers = plural are those always the rails rules?
Yes. Your models should always be singular and controllers should always be plural.
1

If you run "rake routes" command, do "profiles_index" appear in your routes? Usually for the index page of a model, the work 'index' is left out so the route is profiles_path

You error probably comes from a view where you've used profiles_index_path instead of profiles_path

5 Comments

Thanks for your response. The view file is called new.html.erb because it's for creating new profiles - would be great to know what I need to change to fix this. Thanks again :)
You are not using 'profiles_index_path' in new.html.erb .. or you haven't posted all the code. I don't think the problem is in that view.
Strange isn't it! This is all the code and at no point do I reference profiles_index_path - yet the error is suggesting I do in the opening line form_for? The url I am trying to load is localhost:3000/newprofile
I can't realize what the problem is. Have you tried a global search through all your project for 'profiles_index_path' ?
Sadly it's definitely not used anywhere currently, the app isn't very extensive so it would be easy to find :)
1

I think it's failing due to the convention not being followed with your model name.

So I think you're problem is mostly around that you aren't following the convention on the model name, which would classically be singular, since each instance represents one profile. I think the form_for helper is trying to figure out what to do with it and failing as a result. So you have two options to try and resolve. Refactor the model name to singular (I'm not clear exacly how difficult that would be) or pass the :url paramater to form_for so it knows where to post to.

<% form_for @profile, :url => path_to_create_action do |f| %>

more information here:

4 Comments

Hi DVG, it is when I go to ../newprofile (which should be profile#new according to my routes). thanks!
What happens if you move resources :profiles, :controller => 'profiles' above the other routes?
still the same error sadly even if I move the resources to the top of the routes file. My model name is plural, @profile is singular though as is the call in form_for. cheers
Thanks for your help DVG, I had to turn my model singular - thanks for first highlighting this!
1

I'm working with Rails 5 and I got the same error and it was specific using the word Media as my model and RoR used Medium as the plural so I got different routes when executing rake routes.

What I did to fix it was:

  1. Delete the model I just have created.

    rails d scaffold Media
    
  2. Edit config/initializers/inflections.rb with:

    ActiveSupport::Inflector.inflections(:en) do |inflect|
        # Here you can put the singular and plural form you expect
        inflect.irregular 'media', 'medias'
    end
    
  3. Now execute the scaffold again:

    rails g scaffold Media
    

Now you must have everything in the way you expected. Because you have overwritten the Pluralizations and Singularizations (Inflections) in Ruby on Rails.

I hope it could be useful.

Comments

0

Have you tried to replace your form_for tag with the following?

<%= form_for @profile, :as => :post do |f| %>

It looks like it's trying to treat it as a GET request to "/profile". And, since it is not finding the index action, it craps out. I think forcing it to do a POST will fix this issue.

2 Comments

hey, sorry forgot to include that. The view file is called new.html.erb - any further help you can offer would be much appreciated. :)
Hey Yosep, thanks for your help. Sadly I am still getting the same error however :( really don't know why it's getting confused!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.