Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 223
Tom Pinchen Avatar asked May 07 '12 18:05

Tom Pinchen


1 Answers

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.

like image 159
meagar Avatar answered Oct 16 '22 22:10

meagar