简体   繁体   English

以rails形式添加新元素

[英]adding new element in rails form

What are the the correct steps in adding new text fields to a rails form such that rails will register it? 向rails表单添加新文本字段以使rails进行注册的正确步骤是什么?

current form: 当前形式:

<%= form_for(@order) do |f| %>
  <% if @order.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>

      <ul>
      <% @order.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  # I WANT TO ADD A QUANTITY FIELD TO MY FORM
  <div><%= f.label :quantity %><br />
    <%= f.text_field :quantity, autofocus: true %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

after adding the above code, I modified my database migration. 添加上述代码后,我修改了数据库迁移。

[timestamp]_create_orders.rb [时间戳] _create_orders.rb

class CreateOrders < ActiveRecord::Migration
  def change
    create_table :orders do |t|
        # added this line to the database since I want to have a new field in my form
        t.string :quantity

        t.timestamps
    end
  end
end

After that I tried to go to orders/new to create a new order to see if my form is outputting correctly, but instead I get an undefined method "quantity" error at /orders/new 之后,我尝试转到orders / new来创建新订单,以查看我的表单是否正确输出,但是在/ orders / new上却得到了未定义的方法“ quantity”错误

What am I doing wrong here and what should I do to fix it? 我在这里做错什么,应该怎么解决?

If you wanted to add extra fields to a table (as in adding/editing attributes to/of an existing model) that you have already created, use the migration generator to (2.1) create a standalone migration. 如果您想向表中添加额外的字段(如在现有模型中添加/编辑现有模型的属性),请使用迁移生成器 (2.1)创建独立迁移。 Don't edit a migration file after you have run 运行后不要编辑迁移文件

rake db:migrate

Preffered solution: 首选解决方案:

You can just undo whatever changes you made in that migration file and just use a migration generator. 您可以撤消对该迁移文件所做的任何更改,而只需使用迁移生成器。 Then migrate. 然后迁移。 Here is the sample code (untested) 这是示例代码(未经测试)

rails generate migration add_quantity_to_orders quantity:string
rake db:migrate

Non-preferred solution 非首选解决方案

As a simple fix since I don't know how many migrations you have, I think you should drop everything and recreate the table. 作为一个简单的修复程序,因为我不知道您有多少个迁移,我认为您应该删除所有内容并重新创建表。 You don't have to undo the changes you have made. 您不必撤消所做的更改。

rake db:migrate VERSION=0
rake db:migrate

That way it recreates the tables for you. 这样,它将为您重新创建表。 This is the price you will pay for fiddling with already migrated files. 这是您要摆弄已迁移文件的价格。

I hope this solves your problem. 我希望这能解决您的问题。

Its clearly shown that migration is edited. 它清楚地表明迁移已被编辑。

In Rails App, never edit a migration. 在Rails App中,永远不要编辑迁移。 create a new migration for adding a column. 创建用于添加列的新迁移。

After this. 在这之后。 Make sure you have attr_accessor if you are not on RAils 4.else add permit on each column in controller. 如果您不在RAils 4.else上,请确保具有attr_accessor,否则请在控制器的每一列上添加许可。

params.require(:orders).permit(:quantity) params.require(:订单).permit(:量)

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

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