简体   繁体   English

无法在Rails中创建新对象

[英]Cannot create new object in rails

I am trying to create a basic Item creation in rails but I am having trouble creating new item. 我正在尝试在Rails中创建基本的Item创建,但是在创建新Item时遇到了麻烦。 I want to create item's name , say Wash the dishes . 我要创建物品的name ,比如说Wash the dishes These are the codes that I have: 这些是我拥有的代码:

Routes: 路线:

resources :items

ItemsController: ItemsController:

class ItemsController < ApplicationController
...
  def new
    @item = Item.new
  end

  def create
    @item = Item.new(item_params)

    if @item.save
      flash[:notice] = "Item was saved!"
      redirect_to @item
    else
      flash.now[:alert] = "ERROR. ERROR."
      render :new
    end
  end
...
  private

  def item_params
    params.require(:item).permit(:name, :list_id)
  end

end

items/new.html.erb items / new.html.erb

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

    <%= f.text_field :name %>

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

Lastly, schema: 最后,架构:

  create_table "items", force: :cascade do |t|
    t.string   "name"
    t.integer  "list_id"
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
  ...

I got several different error codes, but this is the one I am currently stuck at (other times it showed different error code or it would simply prints out "ERROR. ERROR." (alert that I setup when save fails) 我得到了几种不同的错误代码,但这是我目前遇到的错误代码(其他时候它显示了不同的错误代码,或者它只会打印出“ ERROR。ERROR。”(警告,当我save失败时设置)

Routing Error
No route matches [POST] "/items/new"

When I go to rake routes: 当我去耙路线时:

         POST  /items(.:format)                           items#create
new_item GET   /items/new(.:format)                      items#new

I followed suggestion from this SO post to check my routes and this is what I have: 我遵循了这个SO帖子的建议来检查我的路线,这就是我所拥有的:

2.2.2 :019 > r = Rails.application.routes
 => #<ActionDispatch::Routing::RouteSet:0x007fff323c3230> 
2.2.2 :020 > r.recognize_path "/items/new"
 => {:controller=>"items", :action=>"new"} 

I have also gone to rails c and I was able to create new item manually. 我也去了rails c并且能够手动创建新项目。 ( i = Item.new(name:"Test 123"); i.save ) i = Item.new(name:"Test 123"); i.save

What did I miss? 我错过了什么?

The problem is with your form. 问题出在您的表格上。 To understand what's wrong, do the following: 要了解问题所在,请执行以下操作:

  1. Start the rails server using rails s 使用rails s启动rails服务器
  2. Go to http://localhost:3000/items/new 转到http:// localhost:3000 / items / new
  3. Instead of filling in the form fields, view the source page 而不是填写表单字段,而是查看源页面
  4. Check the form tag. 检查表单标签。 Its submitting the form data to /items/new . 它将表单数据提交到/items/new ie.the action attribute is set to /items/new . 即action属性设置为/items/new Why is that? 这是为什么?

From the documentation: 从文档中:

When the model is represented by a string or symbol, if the :url option is not specified, by default the form will be sent back to the current url (We will describe below an alternative resource-oriented usage of form_for in which the URL does not need to be specified explicitly). 当模型由字符串或符号表示时,如果未指定:url选项,则默认情况下会将表单发送回当前url(我们将在下面介绍form_for的另一种面向资源的用法,其中URL会无需明确指定)。

<form action="/items/new" accept-charset="UTF-8" method="post">

In your routes.rb , there's no route matching POST /items/new routes.rb ,没有匹配POST /items/new路由

So, modify your form to 因此,将您的表格修改为

<%= form_for :item, url: items_path do |f| %>
  <%= f.text_field :name %>
 <%= f.submit %>
<% end %>

This generates a form tag which post s the data to /items rather than /items/new . 这将生成一个表单标签,该表单标签post数据发布到/items而不是/items/new

Or replace your form with 或将您的表格替换为

<%= form_for @item do |f| %>
 <%= f.text_field :name %>
 <%= f.submit %>
<% end %>

Now, the form will be submitted to /items . 现在,该表单将提交给/items The advantage of using the second version is you can dry out your form for creating a new object and updating an existing object into a single view(partial). 使用第二个版本的优点是您可以使表单变干以创建新对象并将现有对象更新为单个视图(局部)。

For more, see http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object 有关更多信息,请参见http://guides.rubyonrails.org/form_helpers.html#binding-a-form-to-an-object

Try this in items/new.html.erb 在items / new.html.erb中尝试一下

<%= form_for @item do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM