Init attributes and set value

Discussion in 'Ruby' started by Stephan Meier, Oct 26, 2008.

  1. Hey all,

    sorry for the noob question.

    But how can i init a big amount of attributes with empty strings.
    And overwrite the empty string if the field of the form is filled with
    some value.

    I try update_attributes(hash) with no success.

    Thanks for your help! :)
    --
    Posted via http://www.ruby-forum.com/.
    Stephan Meier, Oct 26, 2008
    #1
    1. Advertising

  2. On 26.10.2008 16:54, Stephan Meier wrote:

    > But how can i init a big amount of attributes with empty strings.


    I'd use nil as the marker for uninitialized. This happens to be the
    default and saves the overhead of all the empty strings. You can deal
    with those nils later, i.e. when changing or outputting them, e.g.

    def modify_whatever(s)
    (@whatever ||= "") << s
    end

    def print_whatever
    print @whatever || ""
    end

    You can even automate this by writing your own attr_accessor method that
    will define getters like

    def foo
    @foo || ""
    end

    > And overwrite the empty string if the field of the form is filled with
    > some value.


    Form, what form?

    > I try update_attributes(hash) with no success.


    Can you be more specific about what you are really trying to do? Is
    this web, UI or what?

    Kind regards

    robert
    Robert Klemme, Oct 26, 2008
    #2
    1. Advertising

  3. Hey,

    thanks for your answer.

    I have a form (html rails form) and want to generate a search url with
    every textfield.

    The URL looks like http://example.com?value1=&value2=&value3=& ....

    The url has about 20 values but its not necessary every value has to be
    filled.
    So i want to update only these values from my "search form" wich have a
    value and the rest with an empty string. :)

    thanks for your help :)
    --
    Posted via http://www.ruby-forum.com/.
    Stephan Meier, Oct 26, 2008
    #3
  4. On Sun, Oct 26, 2008 at 12:16 PM, Stephan Meier <> wrote:
    > Hey,
    >
    > thanks for your answer.
    >
    > I have a form (html rails form) and want to generate a search url with
    > every textfield.
    >
    > The URL looks like http://example.com?value1=&value2=&value3=& ....
    >
    > The url has about 20 values but its not necessary every value has to be
    > filled.
    > So i want to update only these values from my "search form" wich have a
    > value and the rest with an empty string. :)
    >
    > thanks for your help :)


    I'm sure Robert helped answer your question and I know this isn't the
    Rails list, but this will hopefully help even further.

    http://railscasts.com/episodes/111

    HTH,
    Michael Guterl
    Michael Guterl, Oct 26, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Cheng
    Replies:
    1
    Views:
    8,207
    Juan T. Llibre
    Feb 24, 2006
  2. Replies:
    1
    Views:
    657
    Jules
    Aug 18, 2005
  3. Jess
    Replies:
    4
    Views:
    440
  4. news.aon.at
    Replies:
    11
    Views:
    644
    Ian Collins
    Jan 29, 2011
  5. Spacebottle
    Replies:
    1
    Views:
    870
    joris
    May 24, 2011
Loading...

Share This Page