Hello,
I have a params hash which is called
params[:person]
my people database has fields
first_name
last_name
etc..
When the web form is submitted for creation of a person, the
params[:person] contains the value of all the person attributes.
for e.g
params[:person]{"first_name"=>"John","last_name"=>"Anderson"}
I WANT TO MODIFY IT AS
params[:person]{"first_name"=>"%John%","last_name"=>"%Anderson%"}
NOW THE PROBLEM IS THAT I NEED TO ADD A % SIGN AT THE BEGINNING AND END
OF EACH VALUE IN HASH. SO I HAVE USED
params[:person].each_value{|value| value="%"+value+"%"}
but the above code does not work...The params hash remains UNCHANGED.
Can anyone please tell me what do i need to do...and where am i going
wrong.
Thank you
Regards,
Ank
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Dec 28, 2007, at 11:11 AM, Ank Ag wrote:> > Hello, > > I have a params hash which is called > params[:person] > > my people database has fields > first_name > last_name > etc.. > > When the web form is submitted for creation of a person, the > params[:person] contains the value of all the person attributes. > for e.g > params[:person]{"first_name"=>"John","last_name"=>"Anderson"} > > I WANT TO MODIFY IT AS > params[:person]{"first_name"=>"%John%","last_name"=>"%Anderson%"} > > NOW THE PROBLEM IS THAT I NEED TO ADD A % SIGN AT THE BEGINNING AND > END > OF EACH VALUE IN HASH. SO I HAVE USED > > params[:person].each_value{|value| value="%"+value+"%"} > > but the above code does not work...The params hash remains UNCHANGED. > Can anyone please tell me what do i need to do...and where am i going > wrong. >Take a look at http://www.ruby-doc.org/core/classes/Array.html collect and/or collect! I think that''s what you want. Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 28, 11:11 am, Ank Ag <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > I have a params hash which is called > params[:person] > > my people database has fields > first_name > last_name > etc.. > > When the web form is submitted for creation of a person, the > params[:person] contains the value of all the person attributes. > for e.g > params[:person]{"first_name"=>"John","last_name"=>"Anderson"} > > I WANT TO MODIFY IT AS > params[:person]{"first_name"=>"%John%","last_name"=>"%Anderson%"} > > NOW THE PROBLEM IS THAT I NEED TO ADD A % SIGN AT THE BEGINNING AND END > OF EACH VALUE IN HASH. SO I HAVE USED > > params[:person].each_value{|value| value="%"+value+"%"} > > but the above code does not work...The params hash remains UNCHANGED. > Can anyone please tell me what do i need to do...and where am i going > wrong. > > Thank you > Regards, > Ank > -- > Posted viahttp://www.ruby-forum.com/.The params hash remains unchanged because the ''value'' variable is local to that loop, so it doesn''t change anything outside the loop (such as the params). It changes the ''value'' variable, but not the params variable. In order to change the params, you need to directly reference them in the loop, something like params[:person].each{|k,v| params[:person][k] = ''%'' + v + ''%''} --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Anthony wrote:>> Posted viahttp://www.ruby-forum.com/. > The params hash remains unchanged because the ''value'' variable is > local to that loop, so it doesn''t change anything outside the loop > (such as the params). It changes the ''value'' variable, but not the > params variable. In order to change the params, you need to directly > reference them in the loop, something like > > params[:person].each{|k,v| params[:person][k] = ''%'' + v + ''%''}Thanks a lot Anthony......Its working now. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---