Hay all, im passing data from a form to a controller. Now, i''m passing 1 string of data. How can i get this data, and, in the controller update only 1 field? Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
In the controller, the string you are passing in will be available in the params hash, the key being the name of the field in the form. i.e.: <input type="text" name="my_string"/> This would be accessible in your controller as params[:my_string] When you say you want to update only one field, do you mean only one field in the database record, or one field on the form in the page via AJAX? if you only want to update one field in the database: @foo = Foo.find(params[:id]) @foo.update_attributes(:my_string => params[:my_string]) or if my_string is the only param in the params hash (other than id) you can just say @foo.update_attributes(params) If you only want to update one field on the page via ajax you can use: render :update do |page| page[:id_of_field_to_update].replace_html "text to be inserted into element" end Kevin Ruscoe wrote:> Hay all, im passing data from a form to a controller. Now, i''m passing 1 > string of data. > > How can i get this data, and, in the controller update only 1 field? > > Thanks. >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Kevin Ruscoe wrote:> Hay all, im passing data from a form to a controller. Now, i''m passing 1 > string of data. > > How can i get this data, and, in the controller update only 1 field? > > Thanks.you mean something like view / form ===========params[:obj][:meth] # returns ''param-method'' params[:id] # returns obj.id controller / action ===================@obj = Obj.find(params[:id]) @obj.meth = params[:obj][:meth] @obj.save! (instead of using update_attributes) is this of any help? hii -- 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 -~----------~----~----~----~------~----~------~--~---
Shai Rosenfeld wrote:> Kevin Ruscoe wrote: >> Hay all, im passing data from a form to a controller. Now, i''m passing 1 >> string of data. >> >> How can i get this data, and, in the controller update only 1 field? >> >> Thanks. > > you mean something like > > view / form > ===========> params[:obj][:meth] # returns ''param-method'' > params[:id] # returns obj.id > > controller / action > ===================> @obj = Obj.find(params[:id]) > @obj.meth = params[:obj][:meth] > @obj.save! > (instead of using update_attributes) > > > is this of any help? > hiiSo, View - So i need to pass the ''ID'' and a object ''thing'' with the name of the field being ''replace''. controller @thing = MODEL.find(params[:id]) @thing.replace = params[:thing][:replace] @think save! That will overwrite the given params with the ID of the ID sent? Makes sense! Thanks. Although could you explain the params[@thing][:replace]? Thanks. -- 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 -~----------~----~----~----~------~----~------~--~---
Also you can have two form elements, one hidden field like <input type="hidden" name="thing[id]" value="id_of_thing"/> ans second is regular input <input type="text" name="thing[replace]"/> Then you can just Thing.update_attributes(params[:thing]) ... On Sep 16, 5:50 pm, Kevin Ruscoe <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Shai Rosenfeld wrote: > > Kevin Ruscoe wrote: > >> Hay all, im passing data from a form to a controller. Now, i''m passing 1 > >> string of data. > > >> How can i get this data, and, in the controller update only 1 field? > > >> Thanks. > > > you mean something like > > > view / form > > ===========> > params[:obj][:meth] # returns ''param-method'' > > params[:id] # returns obj.id > > > controller / action > > ===================> > @obj = Obj.find(params[:id]) > > @obj.meth = params[:obj][:meth] > > @obj.save! > > (instead of using update_attributes) > > > is this of any help? > > hii > > So, > > View - > > So i need to pass the ''ID'' and a object ''thing'' with the name of the > field being ''replace''. > > controller > > @thing = MODEL.find(params[:id]) > @thing.replace = params[:thing][:replace] > @think save! > > That will overwrite the given params with the ID of the ID sent? > > Makes sense! > > Thanks. > > Although could you explain the params[@thing][:replace]? > > Thanks. > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
> Although could you explain the params[@thing][:replace]?...? you''re not passing an object to the controller, you''re passing the requests parameters (params). this is in the form of a hash, that''s why it looks like params[:some][:thing] this means that the param hash looks something like {:some => {:thing => ''this is the parameter'' } } so if you do params[:some][:thing] # you''ll get "this is the parameter" i may have confused you by writing params[:obj] - i was trying to state that it is a good practice to have the parameter that is passed to the controller in the same name as the object''s name ( params[:thing] for the @thing obj), but it is definitely not a must. what u did above is fine. the explanation is as follows: if you have a ActiveRecord object (a row from sql) and you want to update it''s attributes you first assign the attribute you want to change, ie: @thing.replace = ''this is the replacement'' this changes the object''s attribute. now, if you want to save the new modified object to the database, all u have to do is (surprisingly enough) do: @thing.save! and the next time you look up that object from the sql, you''ll get the new modified one. there could also be different approaches to saving one attribute into the database (such as update_attribute and so forth) for more info -> http://api.rubyonrails.org/classes/ActiveRecord/Base.html hth && good luck -- 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 -~----------~----~----~----~------~----~------~--~---
William Pratt wrote:> In the controller, the string you are passing in will be available in > the params hash, the key being the name of the field in the form. i.e.: > > <input type="text" name="my_string"/> > > This would be accessible in your controller as params[:my_string] > > When you say you want to update only one field, do you mean only one > field in the database record, or one field on the form in the page via > AJAX? > > if you only want to update one field in the database: > > @foo = Foo.find(params[:id]) > @foo.update_attributes(:my_string => params[:my_string]) > > or if my_string is the only param in the params hash (other than id) you > can just say > > @foo.update_attributes(params) > > If you only want to update one field on the page via ajax you can use: > > render :update do |page| > page[:id_of_field_to_update].replace_html "text to be inserted into > element" > endOh, good point. I might add some RJS to tell the user that the data has been sent. I got it working after checking through the API. i''m using... def rate @artist = Artist.find(params[:id]) Artist.update(@artist.id, {:rate => params[:rating]}) redirect_to :action => ''list'' end Pretty identical to your method. -- 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 -~----------~----~----~----~------~----~------~--~---
Kevin Ruscoe wrote:> William Pratt wrote: >> In the controller, the string you are passing in will be available in >> the params hash, the key being the name of the field in the form. i.e.: >> >> <input type="text" name="my_string"/> >> >> This would be accessible in your controller as params[:my_string] >>Hi, I want to do the same thing here. But instead of update, I want to save. Is it correct to use the above postings'' codings or to do save I need a whole new coding? Thanks -- 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 -~----------~----~----~----~------~----~------~--~---