pedroooo
pedroooo

Reputation: 583

How can I use Base64 encoding in Ruby on Rails?

I'm working in Base64 in Ruby on Rails and have a question:

For example how would the following be Base64 encoded / decoded?

<a href="myroute?id=<%[email protected]%>"> MY LINK</a>

Upvotes: 13

Views: 25604

Answers (3)

Luis
Luis

Reputation: 545

To create Base64 try this:

def create_base_64 
  begin 
    #for create 
    a = 1
    b = Base64.encode64(a.to_s)
    # for decoding
    c = Base64.decode64(b)
    puts c
  rescue Exception => e 
    puts e 
  end 
end

For more security try to broke it. For example:

b[1] = b[1] << SecureRandom.hex(1)

And other action, you recived the param b:

b = params[:b]
b[2]=""
b[2]=""
c = base64.decode64(b.to_s)

Upvotes: 18

heading_to_tahiti
heading_to_tahiti

Reputation: 795

I don't understand what you are trying the achieve with your anchor tag example. But to decode a base64 in ruby you would do this:

base64_string = "67382hfuisab3y289321787123890......"

decoded_data = StringIO.new(Base64.decode64(base64_string))

and then do whatever you want with decoded_data

Upvotes: 0

Ecnalyr
Ecnalyr

Reputation: 5802

ActiveSupport::Base64.encode64(`whateveryouwanttoencode`)

You don't really provide enough detail about what exactly you want to do. . . The method I provided is how you accomplish Base64 encoding though, is that all you need?

Some documentation: http://www.rubydoc.info/docs/rails/3.0.0/ActiveSupport/Base64

Upvotes: 8

Related Questions