Tom Pinchen
Tom Pinchen

Reputation: 2497

undefined method ..._index_path Ruby on Rails

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'

Upvotes: 7

Views: 6164

Answers (5)

alexventuraio
alexventuraio

Reputation: 10174

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.

Upvotes: 1

DVG
DVG

Reputation: 17470

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:

Upvotes: 1

user229044
user229044

Reputation: 239521

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.

Upvotes: 6

Yosep Kim
Yosep Kim

Reputation: 2951

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.

Upvotes: 0

gabitzish
gabitzish

Reputation: 9691

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

Upvotes: 1

Related Questions