未定义的方法 ..._index_path Ruby on Rails

Tom*_*hen 6 ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我试图让一个基本的表格工作并且正在挣扎,因为我不断收到错误

 undefined method `profiles_index_path' for #<#<Class:0x4fe1ba8>:0x4fccda0>
Run Code Online (Sandbox Code Playgroud)

我已经检查过了,似乎无法弄清楚我哪里出错了。

在我看来(new.html.erb)我有:

 <%= 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 %>
Run Code Online (Sandbox Code Playgroud)

在我的配置文件控制器中,我有:

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
Run Code Online (Sandbox Code Playgroud)

最后在我的个人资料模型中我有

class Profiles < ActiveRecord::Base

belongs_to :user

end
Run Code Online (Sandbox Code Playgroud)

人们可以提供的任何帮助真的将不胜感激,因为我很难过。:)

抱歉忘了包括路线:

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

  resources :profiles, :controller => 'profiles'
Run Code Online (Sandbox Code Playgroud)

mea*_*gar 5

问题确实是您将模型名称复数的方式。不要那样做。它应该是一个Profile,而不是一个Profiles。我有一些工作可以让您使用复数模型名称,但答案是坚持 Rails 约定而不是与框架作斗争。将您的模型重命名为Profileurl_for帮助程序将了解如何正确地将新的 Profile 对象转换为/profilesURL。