Ruby on rails 主页中的命名错误:未定义的方法';颜色';对于#<;颜色:0x007fe71261c708>;

Ruby on rails 主页中的命名错误:未定义的方法';颜色';对于#<;颜色:0x007fe71261c708>;,ruby-on-rails,Ruby On Rails,当我尝试加载页面时,我的主页表单和它用来创建数据库对象的控制器之间的某些东西在标题中给了我错误。不确定原因,以下是相关文件/代码: home.html.erb <%= form_for :colours do |f| %> <span class="form-group"> <%= f.text_field :colour, required: true, class: "balloon", id: "colour", placeholder:

当我尝试加载页面时,我的主页表单和它用来创建数据库对象的控制器之间的某些东西在标题中给了我错误。不确定原因,以下是相关文件/代码:

home.html.erb

<%= form_for :colours do |f| %>
    <span class="form-group">
      <%= f.text_field :colour, required: true, class: "balloon", id: "colour", placeholder: "Colour category (red, blue..)" %><label for="colour">Colour</label>
    </span>
    <span class="form-group">
      <%= f.number_field :bold, required: true, class: "balloon-num", id: "bold", min: 1, max: 12, placeholder: "1-12" %><label for="bold">Boldness</label>
    </span>
    <span class="form-group">
      <%= f.number_field :bright, required: true, class: "balloon-num", id: "bright", min: 1, max: 12, placeholder: "1-12" %><label for="bright">Brightness</label>
    </span>
    <span class="form-group">
      <%= f.number_field :num, required: true, class: "balloon-num", id: "num", min: 1, max: 5, placeholder: "1-5" %><label for="num"># Colours</label>
    </span>
    <span class="form-group">
     <button class="button" id="generate" type="submit"><i class="fi-arrow-right"></i></button>
    </span>
  <% end %>
class ColoursController < ApplicationController
  def home
    @colours = Colours.new
  end
  def create
    @colours = Colours.new(colours_params)
    if @colours.save
      redirect_to root_path
    else
      redirect_to root_path, notice: "Error."
    end
  end
  private
  def colours_params
    params.require(:colours).permit(:colour, :bold, :bright, :num)
  end
end
class PagesController < ApplicationController
    def home
        @colours = Colours.new
    end
    private
    def colours_params
        params.require(:colours).permit(:colour, :bold, :bright, :num)
    end
end
Rails.application.routes.draw do
  root to: 'pages#home'
  resources :colour, only: [:home, :create]
end
class CreateColours < ActiveRecord::Migration[5.0]
  def change
    create_table :colours do |t|
      t.string :colour
      t.integer :bold
      t.integer :bright
      t.integer :num
      t.timestamps
    end
  end
end
class Colours < ActiveRecord::Base

end
db/migrate/20161215012944\u创建颜色。rb

<%= form_for :colours do |f| %>
    <span class="form-group">
      <%= f.text_field :colour, required: true, class: "balloon", id: "colour", placeholder: "Colour category (red, blue..)" %><label for="colour">Colour</label>
    </span>
    <span class="form-group">
      <%= f.number_field :bold, required: true, class: "balloon-num", id: "bold", min: 1, max: 12, placeholder: "1-12" %><label for="bold">Boldness</label>
    </span>
    <span class="form-group">
      <%= f.number_field :bright, required: true, class: "balloon-num", id: "bright", min: 1, max: 12, placeholder: "1-12" %><label for="bright">Brightness</label>
    </span>
    <span class="form-group">
      <%= f.number_field :num, required: true, class: "balloon-num", id: "num", min: 1, max: 5, placeholder: "1-5" %><label for="num"># Colours</label>
    </span>
    <span class="form-group">
     <button class="button" id="generate" type="submit"><i class="fi-arrow-right"></i></button>
    </span>
  <% end %>
class ColoursController < ApplicationController
  def home
    @colours = Colours.new
  end
  def create
    @colours = Colours.new(colours_params)
    if @colours.save
      redirect_to root_path
    else
      redirect_to root_path, notice: "Error."
    end
  end
  private
  def colours_params
    params.require(:colours).permit(:colour, :bold, :bright, :num)
  end
end
class PagesController < ApplicationController
    def home
        @colours = Colours.new
    end
    private
    def colours_params
        params.require(:colours).permit(:colour, :bold, :bright, :num)
    end
end
Rails.application.routes.draw do
  root to: 'pages#home'
  resources :colour, only: [:home, :create]
end
class CreateColours < ActiveRecord::Migration[5.0]
  def change
    create_table :colours do |t|
      t.string :colour
      t.integer :bold
      t.integer :bright
      t.integer :num
      t.timestamps
    end
  end
end
class Colours < ActiveRecord::Base

end
更新3控制台中的运行轨道路线返回:

Prefix Verb   URI Pattern                Controller#Action
        root GET    /                          pages#home
colour_index GET    /colour(.:format)          colour#index
             POST   /colour(.:format)          colour#create
  new_colour GET    /colour/new(.:format)      colour#new
 edit_colour GET    /colour/:id/edit(.:format) colour#edit
      colour GET    /colour/:id(.:format)      colour#show
             PATCH  /colour/:id(.:format)      colour#update
             PUT    /colour/:id(.:format)      colour#update
             DELETE /colour/:id(.:format)      colour#destroy

您的表单存在以下问题:

<%= form_for :colours do |f| %>

更改它,使其使用您在home操作中定义的实例变量

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

您的表单存在问题:

<%= form_for :colours do |f| %>

更改它,使其使用您在home操作中定义的实例变量

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


谢谢!但是我现在遇到了一个新的错误,“未定义的方法‘colors_index_path’for#你的意思是什么?colors_path”home.html文件在你的颜色视图或页面视图中?页面视图,这是一个单页面站点,我还需要创建颜色视图吗?当你添加“资源:颜色”时在路由文件中,rails希望所有资源都被定义为方法(索引、显示、编辑、更新、删除)。如果你不想定义它们,你只需要指定你想要的。资源:颜色,仅限:[:主页,:创建]。我想这可能会解决你的问题。现在它给了我一个相反的错误,说“colors\u path”没有定义,问我是不是指“color\u index\u path”。我还应该提到的是,我把一切都用“颜色”而不是“颜色”来表示,因为谷歌的结果让我相信这可以解决这个问题(它没有),所以我不确定这个“颜色路径”是从哪里来的。谢谢!但是我现在遇到了一个新的错误,“未定义的方法‘colors_index_path’for#你的意思是什么?colors_path”home.html文件在你的颜色视图或页面视图中?页面视图,这是一个单页面站点,我还需要创建颜色视图吗?当你添加“资源:颜色”时在路由文件中,rails希望所有资源都被定义为方法(索引、显示、编辑、更新、删除)。如果你不想定义它们,你只需要指定你想要的。资源:颜色,仅限:[:主页,:创建]。我想这可能会解决你的问题。现在它给了我一个相反的错误,说“colors\u path”没有定义,问我是不是指“color\u index\u path”。我还应该提到,我把所有的东西都用“颜色”而不是“颜色”来表示,因为谷歌的结果让我相信这可以解决这个问题(它没有),所以我不确定这个“颜色路径”是从哪里来的。你不应该有一个复数名称的模型。您的型号应称为
color
,而不是
colors
。您的型号不应使用复数名称。您的型号应命名为
color
,而不是
colors