Update params only on 1 field

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.

Kevin R. 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

Shai R. wrote:

Kevin R. 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.

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 ->

hth && good luck

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.:

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

Also you can have two form elements, one hidden field like
ans second is regular input

Then you can just Thing.update_attributes(params[:thing])

On Sep 16, 5:50 pm, Kevin R. [email protected]

William P. 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.:

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

Oh, 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.

Kevin R. wrote:

William P. 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.:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs