在Rails数据库中保存签名板JSON

时间:2015-05-28 00:39:47

标签: ruby-on-rails json signature

我使用了精彩的Thomas Bradley Signature Pad插件,但在将JSON签名保存到我的用户表(PSQL)时出现了一些问题。

此处的相关用户控制器操作:

  def edit
    @user = current_user
  end

  def update
    @user = current_user
    if @user.update_attributes(user_params)
      redirect_to dashboard_path
    else
      render :edit
    end
  end

  protected

  def user_params
    params.require(:user).permit(
        :first_name, :last_name, :phone, :email, :password_digest, :address, :city, :province, :signature)
  end

这是我的表格:

        <%= form_for @user, :html => {:class => "signature"} do |f| %>
            <div class="form-group" style="width:205px; height: 60px;">
            <%= f.label :signature, "Signature" %>
                <ul class="sigNav">
                <li class="clearButton"><a href="#clear">Clear</a></li>
            </ul>
              <div class="sig sigWrapper">
                <div class="typed"></div>
                <canvas class="pad" width="200" height="53"></canvas>
                <%= f.hidden_field :signature, :class=>"output", :name=>"output" %>
              </div>
           </div>

            <div class="spacer clearfix"></div>

            <div class="form-group">
                <%= f.submit "Save", :class=>"btn btn-primary" %>
            </div>
        <% end %>

 <script>
  $(document).ready(function () {
  $('.signature').signaturePad({drawOnly:true});
});
</script> 

当我保存表单时,我没有收到任何错误,但单曲JSON没有保存。日志中也没有错误。当我绑定.pry时,我可以看到所有的参数都在进行,除非我键入user_params,签名没有显示..

我的代码中有什么明显错误吗?

编辑:添加用户模型,用户架构和日志:

User.rb:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :patients
  has_many :assessments, dependent: :destroy
  validates :email, presence: true
  validates :password, presence: true
end

用户架构:

  create_table "users", force: :cascade do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "password_digest"
    t.string   "phone"
    t.string   "city"
    t.string   "postal"
    t.string   "province"
    t.string   "address"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
  end

我尝试更新用户时的日志:

Started PATCH "/users/1" for ::1 at 2015-05-28 13:53:39 -0700
Processing by UsersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZbHqJwkOAZdYwAsTBFPIINE38uwidtUevU2/bWmgeBenXGViy3gDRbj1m5Oe3OUEWa1JkNGFHMRb4sMNnidt0A==", "user"=>{"first_name"=>"Jackson", "last_name"=>"", "email"=>"jackson@gmail.com", "phone"=>"", "address"=>"", "city"=>"", "province"=>""}, "signature"=>"[{\"lx\":52,\"ly\":27,\"mx\":52,\"my\":26},{\"lx\":51,\"ly\":26,\"mx\":52,\"my\":27},{\"lx\":47,\"ly\":28,\"mx\":51,\"my\":26},{\"lx\":38,\"ly\":32,\"mx\":47,\"my\":28},{\"lx\":30,\"ly\":34,\"mx\":38,\"my\":32},{\"lx\":20,\"ly\":37,\"mx\":30,\"my\":34},{\"lx\":14,\"ly\":38,\"mx\":20,\"my\":37},{\"lx\":10,\"ly\":39,\"mx\":14,\"my\":38},{\"lx\":9,\"ly\":39,\"mx\":10,\"my\":39},{\"lx\":11,\"ly\":34,\"mx\":9,\"my\":39},{\"lx\":15,\"ly\":27,\"mx\":11,\"my\":34},{\"lx\":20,\"ly\":18,\"mx\":15,\"my\":27},{\"lx\":27,\"ly\":11,\"mx\":20,\"my\":18},{\"lx\":34,\"ly\":5,\"mx\":27,\"my\":11},{\"lx\":41,\"ly\":0,\"mx\":34,\"my\":5},{\"lx\":46,\"ly\":-2,\"mx\":41,\"my\":0},{\"lx\":50,\"ly\":3,\"mx\":50,\"my\":2},{\"lx\":50,\"ly\":14,\"mx\":50,\"my\":3},{\"lx\":50,\"ly\":31,\"mx\":50,\"my\":14},{\"lx\":50,\"ly\":42,\"mx\":50,\"my\":31},{\"lx\":50,\"ly\":51,\"mx\":50,\"my\":42},{\"lx\":50,\"ly\":58,\"mx\":50,\"my\":51},{\"lx\":53,\"ly\":50,\"mx\":53,\"my\":49},{\"lx\":54,\"ly\":43,\"mx\":53,\"my\":50},{\"lx\":58,\"ly\":36,\"mx\":54,\"my\":43},{\"lx\":63,\"ly\":28,\"mx\":58,\"my\":36},{\"lx\":67,\"ly\":21,\"mx\":63,\"my\":28},{\"lx\":70,\"ly\":17,\"mx\":67,\"my\":21},{\"lx\":72,\"ly\":15,\"mx\":70,\"my\":17},{\"lx\":73,\"ly\":13,\"mx\":72,\"my\":15},{\"lx\":74,\"ly\":13,\"mx\":73,\"my\":13},{\"lx\":75,\"ly\":11,\"mx\":74,\"my\":13},{\"lx\":75,\"ly\":14,\"mx\":75,\"my\":11},{\"lx\":75,\"ly\":24,\"mx\":75,\"my\":14},{\"lx\":74,\"ly\":36,\"mx\":75,\"my\":24},{\"lx\":72,\"ly\":47,\"mx\":74,\"my\":36},{\"lx\":71,\"ly\":56,\"mx\":72,\"my\":47},{\"lx\":57,\"ly\":50,\"mx\":57,\"my\":49},{\"lx\":51,\"ly\":41,\"mx\":57,\"my\":50},{\"lx\":45,\"ly\":37,\"mx\":51,\"my\":41},{\"lx\":39,\"ly\":31,\"mx\":45,\"my\":37},{\"lx\":36,\"ly\":29,\"mx\":39,\"my\":31},{\"lx\":33,\"ly\":26,\"mx\":36,\"my\":29},{\"lx\":32,\"ly\":23,\"mx\":33,\"my\":26},{\"lx\":31,\"ly\":21,\"mx\":32,\"my\":23},{\"lx\":30,\"ly\":19,\"mx\":31,\"my\":21},{\"lx\":34,\"ly\":19,\"mx\":30,\"my\":19},{\"lx\":40,\"ly\":19,\"mx\":34,\"my\":19},{\"lx\":47,\"ly\":19,\"mx\":40,\"my\":19},{\"lx\":63,\"ly\":19,\"mx\":47,\"my\":19},{\"lx\":78,\"ly\":20,\"mx\":63,\"my\":19},{\"lx\":90,\"ly\":21,\"mx\":78,\"my\":20},{\"lx\":98,\"ly\":21,\"mx\":90,\"my\":21},{\"lx\":104,\"ly\":21,\"mx\":98,\"my\":21},{\"lx\":106,\"ly\":21,\"mx\":104,\"my\":21}]", "commit"=>"Save", "id"=>"1"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
  Rendered users/edit.html.erb within layouts/application (2.8ms)
  Rendered layouts/_nav.html.erb (1.3ms)
  Rendered layouts/_flash_messages.html.erb (0.1ms)
Completed 200 OK in 651ms (Views: 644.3ms | ActiveRecord: 0.9ms)

1 个答案:

答案 0 :(得分:0)

@TomDalling为我解决了这个问题。当我提交数据时,结果是一个非常简单的问题。

Save a JSON array to database in rails with strong_params

It's because output is not inside the user attributes. You have:

{
  "first_name" => "Jackson",
  "last_name" => "Cunningham",
  // etc.
},
"output" => "asasfafsafs"
But what you actually want is:

{
  "first_name" => "Jackson",
  "last_name" => "Cunningham",
  "output" => "asasfafsafs",
  // etc.
}
So in your HTML form, then name should be <input name="user[output]"> instead of <input name="output">.