[英]Why Rails model is not writing values to database?
I am new to Rails and my applications is doing everything as intended except writing the right values to de database, each model instance is created but all values passed to it are being null. 我是Rails的新手,我的应用程序正在按计划做所有事情,除了将正确的值写入数据库之外,每个模型实例都已创建,但传递给它的所有值都为null。
Github link to application : https://github.com/aldeano19/railsLearn Github链接到应用程序: https : //github.com/aldeano19/railsLearn
Model 模型
class Oneinfo < ActiveRecord::Base
attr_accessor :name, :number
end
Controller 控制者
class InfoController < ApplicationController
$names = {}
@show = true
def print
if request.request_method_symbol == :get
$names = {}
@show = false
else
@show = true
name = params[:myinfo][:name]
number = params[:myinfo][:number]
$names[name] = number
@newinfo = Oneinfo.new(oneinfo_params)
@newinfo.save
end
end
private
def oneinfo_params
params.require(:myinfo).permit(:name, :number)
end
end
View 视图
<h1>Info print</h1>
<%= form_for :myinfo do |a| %>
<%= a.label :name %>
<%= a.text_field :name %><br>
<br>
<%= a.label :number%>
<%= a.text_field :number%><br>
<br>
<%= a.submit %>
<%end%>
<div id="clear" style="display: <%= @show ? 'inline' : 'none' %> ">
<%= form_for :button do |b|%>
<%link_to 'clear', info_print_path %>
<%end%>
</div>
<% $names.each do |key, val|%>
<%=key%>  
<%=val%><br>
<%end%>
** MYSQL Database after 7 Models have being submited** **提交7个模型后的MYSQL数据库**
+----+------+--------+---------------------+---------------------+
| id | name | number | created_at | updated_at |
+----+------+--------+---------------------+---------------------+
| 1 | NULL | NULL | 2014-10-28 19:28:14 | 2014-10-28 19:28:14 |
| 2 | NULL | NULL | 2014-10-28 23:32:31 | 2014-10-28 23:32:31 |
| 3 | NULL | NULL | 2014-10-28 23:32:37 | 2014-10-28 23:32:37 |
| 4 | NULL | NULL | 2014-10-28 23:33:10 | 2014-10-28 23:33:10 |
| 5 | NULL | NULL | 2014-10-28 23:33:32 | 2014-10-28 23:33:32 |
| 6 | NULL | NULL | 2014-10-28 23:33:34 | 2014-10-28 23:33:34 |
| 7 | NULL | NULL | 2014-10-28 23:33:43 | 2014-10-28 23:33:43 |
+----+------+--------+---------------------+---------------------+
Delete attr_accessor :name, :number
from Oneinfo model. 从Oneinfo模型中删除attr_accessor :name, :number
。
attr_accessor
creates instance methods (reader and writer) for your class. attr_accessor
为您的类创建实例方法(读取器和写入器)。 So, for instance, instead of having: 因此,例如,与其具有:
class Car
# reader
def engine
@engine
end
#writer
def engine=(engine)
@engine = engine
end
end
you can write: 你可以写:
class Car
attr_accessor :engine
end
The clue here is that attr_accessor :name, :number
overrides the default behaviour of your model fields. 这里的提示是attr_accessor :name, :number
会覆盖模型字段的默认行为。 In real cases, you do not need to specify attr_accessor
for any of your model fields, unless you would need to extend your model with some extra fields, which are not defined in your database table. 在实际情况下,您无需为任何模型字段指定attr_accessor
,除非您需要使用一些未在数据库表中定义的额外字段来扩展模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.