我的模型没有正确保存,也没有在我编写的before_save验证之前。
我的表格如下:
<%= form_for @troll, url: {action: 'create'} do |f| %>
<%= f.label :email, 'Email (not stored)' %>
<%= f.text_field :email, :required => true, :prompt => 'Email Address' %><br/><br/>
<%= f.label :apikey, 'API Key (not stored)' %>
<%= f.text_field :apikey,:required => true, :prompt => 'API Key' %><br/><br/>
<%= f.label :zone_id, 'Zone ID' %>
<%= f.text_field :zone_id,:required => true, :prompt => 'Zone Id' %></br><br/>
<%= f.label :ip_addr, 'IPv4 Address' %>
<%= f.text_field :ip_addr,:required => true, :prompt => 'IPv4 Address' %><br/><br/>
<%= f.label :timeframe, 'Select a timeframe.' %>
<%= f.select :timeframe, [["15 mins", 15],["60 mins", 60]] %><br/><br/>
<%= f.submit troll_index_path, :value => "Commence Troll!" %>
<% end %>
#create动作:
def create
puts params[:troll]
@troll = Troll.new(troll_params)
@troll.save
puts @troll.inspect
render '/troll/show', :troll => @troll
end
更进一步:
def troll_params
params.require(:troll).permit(:email, :apikey, :zone_id, :ip_addr, :timeframe)
end
我的'show'动作和终端节目:
#<Troll id: nil, created_at: nil, updated_at: nil, ip_addr: "aaa", zone_id: nil, unix_timestamp: nil>
因此信息不会被保存也不会被验证。我的模型如下:
class Troll < ActiveRecord::Base
require 'resolv'
attr_accessor :apikey, :email, :zone_id, :unix_timestamp, :time_interval, :timeframe
validates :ip_addr, :format => {:with => Resolv::IPv4::Regex,:message => "Not an valid IPv4 format"}
before_save :convert_to_unix
def convert_to_unix(time_interval)
timeValue = self.time_interval.to_i
timeStamp = DateTime.now - timeValue/1440.0
self.unix_timestamp = timeStamp.to_i
end
end
我的数据库架构非常简单。出于隐私原因,我只想保存一些数据。
class CreateTrolls < ActiveRecord::Migration
def change
create_table :trolls do |t|
t.string :ip_addr
t.string :zone_id
t.integer :unix_timestamp
end
end
end
我看到params正确地遇到了,但是当我保存到DB时,它没有验证ip_attr或者执行unix时间戳转换。
它也显示zone_id为零,即使它在params中就在那里!任何方向都非常赞赏。
答案 0 :(得分:0)
尝试将提交按钮更改为:
f.submit "Commence Troll"
答案 1 :(得分:0)
您最好使用以下内容:
#app/views/trolls/new.html.erb
<%= form_for @troll do |f| %>
<%= f.label :email, 'Email (not stored)' %>
<%= f.text_field :email, :required => true, :prompt => 'Email Address' %>
<%= f.label :apikey, 'API Key (not stored)' %>
<%= f.text_field :apikey, required: true, placeholder: 'API Key' %>
<%= f.label :zone_id, 'Zone ID' %>
<%= f.text_field :zone_id, required: true, placeholder: 'Zone Id' %>
<%= f.label :ip_addr, 'IPv4 Address' %>
<%= f.text_field :ip_addr, required: true, placeholder: 'IPv4 Address' %>
<%= f.label :timeframe, 'Select a timeframe.' %>
<%= f.select :timeframe, [["15 mins", 15],["60 mins", 60]] %><br/><br/>
<%= f.submit "Commence Troll!" %>
<% end %>
#app/controllers/trolls_controller.rb
class TrollsController < ApplicationController
def create
@troll = Troll.new troll_params
redirect_to @troll if @troll.save
end
private
def troll_params
params.require(:troll).permit(:email, :apikey, :zone_id, :ip_addr, :timeframe)
end
end
#app/models/troll.rb
class Troll < ActiveRecord::Base
require 'resolv'
attr_accessor :apikey, :email, :time_interval, :timeframe
validates :ip_addr, format: { with: Resolv::IPv4::Regex, message: "Not an valid IPv4 format"}
before_save :convert_to_unix
private
def convert_to_unix(time_interval)
timeValue = self.time_interval.to_i
timeStamp = DateTime.now - timeValue/1440.0
self.unix_timestamp = timeStamp.to_i
end
end
-
你有:zone_id
&amp; <{1}}声明中的unix_timestamp
,意味着它们不会被保存。
如果您运行上面的代码,如果发生任何错误,它应该返回到您的attr_accessor
操作,允许您解决它们。