简体   繁体   English

Ruby on Rails在数据库中为零

[英]Nil in Database for Ruby on Rails

I'm extremely new, and going through the basic ruby rails tutorial except tweaking it just to add some extra fields and renaming what it calls articles at contacts. 我是一个非常新的人,除了调整它只是为了添加一些额外的字段并重命名它在联系人中的文章外,还要阅读基本的ruby rails 教程

Problem is, everything is fine, except all my data is being added in as NIL. 问题是,一切正常,除了我的所有数据都以NIL的形式添加。 Here is my controller: 这是我的控制器:

class ContactsController < ApplicationController
   def index
@contacts = Contact.all
   end
def show
  @contacts = Contact.find(params[:id])
end

def new
 end

def create
@contacts = Contact.new(contact_params)

@contacts.save
redirect_to @contacts
 end
end

private
   def contact_params
     params.require(:contacts).permit(:first_name, :last_name, :phone_number,     :notes)
    end

my migrate file: 我的迁移文件:

 class CreateContacts < ActiveRecord::Migration
 def change
   create_table :contacts do |t|
     t.string :first_name
     t.text :last_name
     t.text :phone_number
     t.text :notes

      t.timestamps null: false
    end
  end
 end

And I'm looking in my terminal and I see this: 我正在看我的终端,我看到了:

 Started POST "/contacts" for 127.0.0.1 at 2015-07-16 14:38:54 -0700
Processing by ContactsController#create as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"5l/werImOvbiPAq4rGB6oj8TILHKVVy96GGgRntzP2UwZxj6cSmIDsHzs     RQZXbqHJATp60QMuRg7HWlLY5hf/w==", "contacts"=>{"First_name"=>"Hello",      "Last_name"=>"AAGIN", "Phone_Number"=>"1231313", "Notes"=>"smsda"},     "commit"=>"Save Contacts"}
 Unpermitted parameters: First_name, Last_name, Phone_Number, Notes

I think they're might be something about the 'unpermitted parameters' part, because there seems to be a difference in caps there. 我认为这可能与“不允许的参数”部分有关,因为那里的上限似乎有所不同。

If you see anything wrong, please let me know! 如果您发现任何错误,请告诉我! Thank you. 谢谢。 edit = here's my view: 编辑=这是我的看法:

<h1>New Contact</h1>

<%= form_for :contacts, url: contacts_path do |f| %>
  <p>
    <%= f.label :First_name %><br>
    <%= f.text_field :First_name %>
  </p>
  <p>
    <%= f.label :Last_name  %><br>
    <%= f.text_field :Last_name %>
  </p>
  <p>
     <%= f.label :Phone_Number %><br>
    <%= f.text_field :Phone_Number %>
  </p>
  <p>
    <%= f.label :Notes %><br>
    <%= f.text_area :Notes %>
  </p>

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

<%= link_to 'Back', contacts_path %>

You are not consistent with contacts and contact. 您与联系人和联系人不一致。 Change your line to. 将行更改为。

 params.require(:contact).permit(:first_name, :last_name, :phone_number,     :notes)

Name your variables according to Rails conventions co @contacts should be changed to @contact . 根据Rails约定为变量命名,co @contacts应该更改为@contact

You are not instantiating your new object in the new action, so the form is taking a nil object and not filling in with the new data from the form. 您没有在新操作中实例化新对象,因此该表单将使用nil对象,而不用表单中的新数据填充。 Try something like 尝试类似

def new
    @contact = Contact.new
end

and also, you are using @contacts instead of @contact in the create and show actions, for the show is just a naming thing, semantics.. but for the create you should use the @contact object that you instantiated in the new action. 并且,您在创建和显示操作中使用@contacts而不是@contact,因为显示仅是命名语义,..但是对于创建,您应该使用在新操作中实例化的@contact对象。

Last the syntax for the params is params.require(:contact) no contacts, always read your code in rails and check if the semantics makes sense, it would make you life easy 最后,params的语法是params.require(:contact)没有联系,总是在rails中读取代码并检查语义是否有意义,这将使您的生活变得轻松

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

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