gpt4 book ai didi

ruby-on-rails - StaticPages#manager 中的参数错误

转载 作者:数据小太阳 更新时间:2023-10-29 09:02:11 28 4
gpt4 key购买 nike

我在创建项目时遇到问题。我有下一个错误:

ArgumentError in StaticPages#manager

Showing /home/verevkinra/apps/yurta24/app/views/items/_new.html.erb where line #2 raised:

First argument in form cannot contain nil or be empty

Extracted source (around line #2):
<h1>Items manage</h1>
<%= form_for @item do |f| %>
<%= f.text_field :variable1 %>
<%= f.text_field :variable2 %>
<%= f.text_field :variable3 %>
<%= f.text_field :variable4 %>

Trace of template inclusion: app/views/static_pages/manager.html.erb

Rails.root: /home/verevkinra/apps/yurta24

在此代码的第二行 (app/view/items/new.html.erb) 中,表单中的第一个参数不能包含 nil 或为空:

<h1>Items manage</h1>
<%= form_for @item do |f| %>
<%= f.text_field :variable1 %>
<%= f.text_field :variable2 %>
<%= f.text_field :variable3 %>
<%= f.text_field :variable4 %>
<%= f.text_field :value1 %>
<%= f.text_field :value2 %>
<%= f.text_field :value3 %>
<%= f.text_field :value4 %>
<%= f.text_field :comment %>

<%= f.submit %>
<% end %>

我的 Items_controller.rb 是:

class ItemsController < ApplicationController

def new
@item = Item.new
end
def create
@item = Item.new item_params
@item.save
end

def destroy
@item = Item.find(params[:id])
@item.destroy
end

private

def item_params
params.require(:item).permit(:variable1, :variable2, :variable3, :variable4, :value1, :value2, :value3, :value4, :comment)
end
end

我的数据库迁移文件是:

class CreateItems < ActiveRecord::Migration
def change
create_table @item do |t|
t.string :variable1
t.string :variable2
t.string :variable3
t.string :variable4
t.string :value1
t.string :value2
t.string :value3
t.string :value4
t.string :comment

t.timestamps null: false
end
end
end

我的 routes.rb 有下一行:

resources :items

谢谢

我的 static_pages_controller.rb 是:

class StaticPagesController < ApplicationController
def home
@contact_form = ContactForm.new
end

def manager
@contact_messages = ContactForm.all
@item = Item.new
end
end

@item = Item.new 我现在添加了,还有另一个错误:

NoMethodError in StaticPages#manager

Showing /home/verevkinra/apps/yurta24/app/views/items/_new.html.erb where line #8 raised:

undefined method `value2' for #<Item:0xb3ba6504>

Extracted source (around line #8):
<%= f.text_field :variable4 %>
<%= f.text_field :value1 %>
<%= f.text_field :value2 %>
<%= f.text_field :value3 %>
<%= f.text_field :value4 %>
<%= f.text_field :comment %>

Trace of template inclusion: app/views/static_pages/manager.html.erb

app/views/static_pages/manager.html.erb 是:

<%= render 'contact_forms/new' %>
<%= render 'items/new' %>

最佳答案

First argument in form cannot contain nil or be empty

您正在 app/views/中呈现部分(app/views/items/_new.html.erb) static_pages/manager.html.erb,所以你应该在 static_pages_controller.rbmanager 方法中有 @item = Item.new >

#static_pages_controller.rb
def manager
@item = Item.new
end

undefined method `value2' for Item:0xb3ba6504

items 表中找不到该属性 value2。使用以下命令创建迁移

rails g migration add_value2_to_items value2:string

在迁移文件中将 create_table @item do |t| 更改为 create_table :items do |t| 并执行 rake db:migrate

关于ruby-on-rails - StaticPages#manager 中的参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32861654/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com