Passing Object in params, and parse.

Hi,

Im passing an object as follows.

Controller:

def new   @c = Check.first end

View:

<form action="create" method="post"> <%= hidden_field_tag "checks", @c %> <%= submit_tag "submit" %> </form>

The params i get in create method is, {"checks"=>"#<Check:0xa2c3134>", "commit"=>"submit"}

Any idea how to parse "#<Check:0xa2c3134>" and take all the data?

Thanks, Srikanth J

Hi Srikanth,

Are you looking to parse individual attributes of Check to your create method or do you want to do the manipulations in create?

In your create method, you can just do something like:

def create

@check = params[:checks]

and then you can manipulate it here

@check.inspect

end

Hope this helps.

Cheers,

David Chua

http://flavors.me/davidchua | http://dchua.com | @davidchua

what you see in the params is not the Check object but it's string representation. There is no sane way to get the actual object out of this. I guess what you want is to pass the id of the check object and not the object itself.

<%= hidden_field_tag "checks", @c.id %>

and in the controller

def create   @check = Check.find(params[:checks]) end

-- pascal

Hi David,

def create   @check = params[:checks]   # and then you can manipulate it here   # @check.inspect end

@check.inspect gives me,.. "#<Check:0xa2c3134>"

any idea how can i make it {"name" => "XXX", "number" => "YYY"}

Thanks, Srikanth

Hi Pascal,

what you see in the params is not the Check object but it's string representation. There is no sane way to get the actual object out of this. I guess what you want is to pass the id of the check object and not the object itself.

for example of object i made it @c = Check.first.

Actually its

@c = Check.new @c.name = "Srikanth" @c.number = 1234

And i need this object in create.

Thanks, Srikanth

Oh I’m sorry, I didn’t quite understand your question earlier, its clearer now.

What you can consider doing is attaching the Check object’s id into the hidden_field, and then pass the id into your create method.

Then all you need to do is to use ActiveRecord to retrieve that Check object with your id.

David

What you can consider doing is attaching the Check object's id into the hidden_field, and then pass the id into your create method.

Actually there was no id for that object.

I did like this. converted to json and parsed it.

def new   @c = Check.new   @c.name = "Srikanth"   @c.number = 1234   @a = @c.to_json end

And in View: <%= hidden_field_tag "checks", @a %>

def create p = JSON.parse(params[:checks]) puts p.inspect end

got p as, {"check"=>{"name"=>"Srikanth", "number"=>1234}}

Thanks all for reply.

You can also try using

<%= hidden_field_tag ‘checks[name]’, @c.name %>

<%= hidden_field_tag ‘checks[number]’, @c.number %>

So you don’t have to parse it in the create action.