我对rails很新。这必须是一个非常基本的问题,但我无法找到答案。
我的问题:按提交似乎不会更新我的记录。
update_query = 'UPDATE tweets SET favorite_count = ?, retweet_count = ? WHERE id = ?'
for item in parsed[1]:
# parsed[1][0] = {'favorite_count': 0, 'id': 838564895905964033, 'retweet_count': 229}
curr_fav_count = item['favorite_count']
curr_rt_count = item['retweet_count']
curr_id = item['id']
c.execute(update_query, (curr_fav_count, curr_rt_count, curr_id))
这是我的.rb文件
<%= simple_form_for(@select_statement) do |f| %>
<%= render 'form_error', f:f %>
<div class="form-inputs">
<%= f.input :title, :label => 'Required: Short title for this sql SELECT statment' %>
<%= f.input :description, required: false, :label => 'Optional: Long description of your sql SELECT statment' %>
<%= f.input :sql_select_statement, :label => 'Required: Your sql SELECT statment' %>
<%= f.input :user_email %>
<%= f.input :favorite, :input_html => { :checked => false } %>
<%= f.button :submit %>
</div>
<% end %>
现在app / controllers / select_statements_controller.rb
class RalphValidator < ActiveModel::Validator
def validate(record)
byebug if ralph_test_byebug
xyz = 123
end
end
class EmailValidator < ActiveModel::Validator
def validate(record)
# byebug if ralph_test_byebug
email_regex = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[:base] << 'email address missing or invalid!!! Internal system error!!!' unless record.user_email =~ email_regex
# record.errors[:base] << 'surface mail address missing!!!' unless record.user_email =~ email_regex
# xyz=123
# record.errors.add :user_email, 'email address missing!!!' unless record.user_email =~ email_regex
end
end
class SelectStatement < ActiveRecord::Base
# See http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html
include ActiveModel::Validations
attr_accessor :user_email
before_validation(on: :create) do
byebug if ralph_test_byebug
xyz = 123
end
validates_with RalphValidator
validates :sql_select_statement, presence: true
# validates :user_email, presence: true, email: true
validates :favorite, inclusion: [true, false]
byebug if ralph_test_byebug
validates_with EmailValidator, fields: [:user_email]
# see http://www.informit.com/articles/article.aspx?p=2220311&seqNum=2
before_save :ralph_before_save
def ralph_before_save
byebug if ralph_test_byebug
xyz=123
end
if true
def initialize(args)
byebug if ralph_test_byebug
# See http://stackoverflow.com/questions/23050181/ruby-class-new-gives-class-not-initialized-error-in-rails-console
super
byebug if ralph_test_byebug
@initialize_args = args # May be unnecessary
@user_email = args[:user_email] # Probably unnecessary
byebug if ralph_test_byebug
xyz=123
end
end
if true
# See http://guides.rubyonrails.org/active_record_callbacks.html#after-initialize-and-after-find
after_initialize do |s_hash|
zyx = new_record?
byebug if ralph_test_byebug
s_hash[:user_email] = @user_email
byebug if ralph_test_byebug
# xyz=123
end
end
end
我的基本问题:Rails在哪里将表单的字段插入记录?我试过,例如,before_validate,validates_with,before_save,...
每次我在上面的代码中点击一个byebug断点 - 除了与user_email相关联的属性 - self或@select_statement只是满了nils。
显然,我遗漏了一些非常基本的东西。
答案 0 :(得分:0)
因此,令我惊讶的是,Rails 4增加了一个全新的安全功能:强参数。
看起来我期望的行为 - 行属性将自动填充 - 在Rails 4中不再正确,并且必须手动处理行/记录分配。
我发现学习强参数的一个很好的起点是: http://blog.trackets.com/2013/08/17/strong-parameters-by-example.html
这是我的小道信仰。我欢迎评论和纠正。