1

Ruby on Rails 4

My form is submitting attributes into a table, one attribute (:permission) is an array of strings. I got the error that it cannot insert an array to type string so I tried adding to_s and the error is gone but now it rollsback. I am not sure how to see the error or how to fix this.

My Form:

<%= f.label :permission, "Permission Level" %>
<%= f.select :permission, [ ["Read Only", "read"], ["IP Voice Telephony", "ip_voice"], ["IP Video Telephony", "ip_video_telephony"], ["Enterprise Gateways", "enterprise_gateways"], ["Consumer ATAs", "consumer_atas"], ["IP PBX", "ip_pbx"], ["Master of All", "all"] ], {prompt: "Select Permission Level"}, {:multiple => true, class: "input-lg"} %>

My controller:

def user_params
  params.require(:user).permit(:name, :email, :password, :password_confirmation, :admin, :permission => [].to_s)
end

My Log:

Started POST "/users" for 24.147.48.156 at 2014-04-23 13:06:05 -0700
Processing by UsersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"spaGUvxtoK2ipPrSkmGmMuIChVcEtzrWJbt/b1oByG4=", "user"=>{"name"=>"Tester", "email"=>"t
[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "permission"=>["", "ip_voice", "ip_video_telephon
y"], "admin"=>"0"}, "commit"=>"Create User"}
(0.1ms)  begin transaction
User Exists (0.1ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
(0.1ms)  rollback transaction

I am not sure how to insert the array or how to view the error, any help appreciated.

User model:

class User < ActiveRecord::Base
before_save { self.email = email.downcase }
before_create :create_remember_token
has_many :questions
serialize :permission

validates :name, presence: true, length: { maximum: 50 }
validates :permission, presence: true

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
                            uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }                         


has_secure_password

def User.new_remember_token
    SecureRandom.urlsafe_base64
end

def User.hash(token)
    Digest::SHA1.hexdigest(token.to_s)
end

private 

    def create_remember_token
        self.remember_token = User.hash(User.new_remember_token)
    end
end

1 Answer 1

2

I think you are looking to serialize permission attribute

In your user model,

class User < ActiveRecord::Base
  serialize :permission
end

This tells Rails to convert the permission attribute which is dealt as Array object on Rails layer to be String object in database layer.


edit 1:

Try editing the user_params method.

def user_params
  params.require(:user).permit(:name, :email, :password, :password_confirmation, :admin, :permission => [] )
end
Sign up to request clarification or add additional context in comments.

1 Comment

It rollsback the same log is shown. The error is permission cannot be blank

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.