Forum: Ruby Ruby Hash wierdness

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 Kev Jackson (Guest)
on 2005-12-15 11:14
(Received via mailing list)
Hi

I'm having problems with using a hash.  I put in a class and when I
retrieve it, it's automagically become a string

ie

  <%= button_link _("Export CSV"),
        SearchParams.get_url_hash({ :action => :export_csv, :klass =>
Customer }, params, @search_fields) %>

Customer is a class that is available to my view


def export_csv
    klass = params[:klass]
    cond = SearchParams.get_search_conditions(klass, params,
@search_fields)
    set_csv_headers(klass)
    csv_export(klass, cond)
  end

but here klass (and params[:klass]) is a string?  What I really want to
do is

  <%= button_link _("Export CSV"),
        SearchParams.get_url_hash({ :action => :export_csv(Customer) },
params, @search_fields) %>

Any ideas why my lovely Customer class is being butchered into a string
:(

Kev
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2005-12-15 11:44
(Received via mailing list)
Kev Jackson wrote:
>
> Customer is a class that is available to my view
>
>
> def export_csv
>     klass = params[:klass]

Try

klass = Object.const_get(params[:klass])

>         SearchParams.get_url_hash({ :action => :export_csv(Customer)
> }, params, @search_fields) %>
>
> Any ideas why my lovely Customer class is being butchered into a
> string :(

Probably because there is an HTTP request in between?

    robert
2a0f7bd2c54fbc44329d69555b96f1c5?d=identicon&s=25 Kev Jackson (Guest)
on 2005-12-15 11:47
(Received via mailing list)
>>def export_csv
>>    klass = params[:klass]
>>
>>
>
>Try
>
>klass = Object.const_get(params[:klass])
>
>
beautiful, thank you so much

Kev
Cb2b768a5e546b24052ea03334e43676?d=identicon&s=25 Dr Nic (Guest)
on 2005-12-15 12:38
(Received via mailing list)
Yep, urls only contains strings. Often horribly ugly ones with %20 etc
all through it, but they are strings.

I like the Object.const_get method - cleaner and more specific than an
eval etc.

Nic
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 Andrew Filipowski (Guest)
on 2005-12-15 13:54
(Received via mailing list)
My guess is (and I am new to rails myself) is that the browser is not
smart enough to send an object, it only knows how to post strings
back to you. I have never tried to send a class back and forth I
always just sent the ID and looked up that particular Customer in the
action.

If you can send back a class than this would definitely be more DRY
and I will have to go back and do some code cleanup if I am told
differently in this thread. (only been on rails for about 1.5 months
now)

Andrew
This topic is locked and can not be replied to.