Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6061977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:59:54+00:00 2026-05-23T08:59:54+00:00

I am having a problem with rails: i’m trying to do an app for

  • 0

I am having a problem with rails: i’m trying to do an app for password management for personal use and for learning rails and I want the passwords to be encrypted (for now i’m using blowfish algorithm). I’ve installed the crypt gem and written some code, but i am receiving a strange error.

Here’s my code:

app/controller/credentials_controller.rb (scaffolded generate)

def create
  @credential = current_user.credentials.build(params[:credential])

  respond_to do |format|
    if @credential.save
      format.html { redirect_to(@credential, :notice => 'Credential was successfully created.') }
      format.xml  { render :xml => @credential, :status => :created, :location => @credential }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @credential.errors, :status => :unprocessable_entity }
    end
  end
end

app/models/credential.rb (in the db i’ve created a salt:string column)

require 'crypt/blowfish'                           

 class Credential < ActiveRecord::Base  
    before_save :hash_password
    before_update :hash_password
    after_find :unhash_password

    private
    def hash_password
      self.salt = ActiveSupport::SecureRandom.base64(8)
      blowfish = Crypt::Blowfish.new(self.salt)
      self.pass = blowfish.encrypt_block(self.pass)
    end

    def unhash_password
    end
end

app/views/credential/_form.html.erb

<%= form_for(@credential) do |f| %>
 <% if @credential.errors.any? %>
 <div id="error_explanation">
   <h2><%= pluralize(@credential.errors.count, "error") %> prohibited this credential from being saved:</h2>

   <ul>
   <% @credential.errors.full_messages.each do |msg| %>
     <li><%= msg %></li>
   <% end %>
   </ul >
 </div> 
   <% end %>
 <div class="field">
   <%= f.label :servizio %><br />
   <%= f.text_field :servizio %>
 </div>
 <div class="field">
   <%= f.label :url %><br />
   <%= f.text_field :url %>
 </div>
 <div class="field">
   <%= f.label :email %><br />
   <%= f.text_field :email %>
 </div>
 <div class="field">
   <%= f.label :utente %><br />
   <%= f.text_field :utente %>
 </div>
 <div class="field">
   <%= f.label :pass %><br />
   <%= f.text_field :pass %>
 </div>
 <div class="field">
   <%= f.label :note %><br />
   <%= f.text_area :note %>
 </div>
 <div class="field">

<%= collection_select(:credential, :group_id, current_user.groups, :id, :nome,  prompt => 'Seleziona Gruppo') %>    
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

the error is:

`Action Controller: Exception caught
NoMethodError in CredentialsController#create 
 undefined method '%' for true:TrueClass
app/models/credential.rb:17:in 'hash_password'
app/controllers/credentials_controller.rb:47
app/controllers/credentials_controller.rb:46:in 'create'

note: if in credential model

def hash_password
    self.salt = ActiveSupport::SecureRandom.base64(8)
    plainBlock = "ABCD1234"
    blowfish = Crypt::Blowfish.new(self.salt)
    self.pass = blowfish.encrypt_block(plainBlock)
  end

it works, but the password (obviously) is always ABCD1234. the code above, mean that the problem is self.pass in blowfish.encrypt_block function.

What am I doing wrong?

If I skip the before_save function it works as a non-encrypted password, so i exclude routes-related problems.

thank you very much!

best regards!

ps: i’m using Rails 3.0.8
ps: i’m following this page http://crypt.rubyforge.org/blowfish.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:59:55+00:00Added an answer on May 23, 2026 at 8:59 am

    i have the solution. the problems was:

    self.salt = ActiveSupport::SecureRandom.base64(8)
    

    in this case, self.salt has to be 56 bytes length, because blowfish require a 56byte key.

    self.pass = blowfish.encrypt_block(self.pass)
    

    in blofwish, self.pass has to be 8 bytes length

    best regards and thanks for your support

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm having problem to deal with charset in ruby on rails app, specificially in
I'm having a problem in my Ruby on Rails app with a model where
I'm having a problem deploying my Rails app to Heroku, where this error is
I'm having a problem installing Spree in a Rails app. when I run gem
I am having a problem getting an old Ruby on Rails 2 app that
I've got a rails 3.1 app deployed on Heroku Cedar. I'm having a problem
I'm having a problem trying to get a recursive method to work in rails/ruby.
We're converting our Rails 2.3 app to 3.0.11, and were having a problem migrating
I'm having problem with Rails plugin attachment_fu . On every upload, I get validation
I'm working through Agile Web Development with Rails and having a problem with Task

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.