我的主页表单与控制器之间创建数据库对象的东西在我尝试加载页面时给出了标题中的错误。不确定是什么导致它,这里是相关的文件/代码:
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 %>
控制器/ colours_controller.rb
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
控制器/ pages_controller.rb
class PagesController < ApplicationController
def home
@colours = Colours.new
end
private
def colours_params
params.require(:colours).permit(:colour, :bold, :bright, :num)
end
end
配置/ routes.rb中
Rails.application.routes.draw do
root to: 'pages#home'
resources :colour, only: [:home, :create]
end
分贝/迁移/ 20161215012944_create_colours.rb
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
模型/ colours.rb
class Colours < ActiveRecord::Base
end
我一直在寻找和问人们一整天都没有运气,希望有人能在这里看到我的错误并向我解释如何纠正它。谢谢!
更新将form_for标记中的“:colors”更改为“@colours”,现在收到以下错误:
undefined method `colours_index_path' for #<#<Class:0x007fe711dbfab0>:0x007fe704401058>
Did you mean? colours_path
更新2 在控制台中,'bundle exec rails routes'返回:
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
colour_index POST /colour(.:format) colour#create
更新3 在控制台中运行rails路由返回:
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
答案 0 :(得分:0)
您的表单是问题所在:
<%= form_for :colours do |f| %>
更改它以便它使用您在主动作中定义的实例变量。
<%= form_for @colours do |f| %>