Richlewis Richlewis - 4 years ago 100
Ruby Question

find(params[:id]) and instance variable returns <Object>

Hopefully an answer to this question will clear up my understanding a little of what is going on here

Within my Images Controller I have a


class ImagesController < ApplicationController
def show
@image = Image.find(params[:id])

When I am on a page
is 6


resources :images, only: [:show]

So on this page I have a form_for to write to a different model, within a hidden field (for now) I have

<%= form_for(@cart_item, url: cart_item_path, :remote => true do |f| %>
<%= f.hidden_field :image_id, value: @image %>
<% end %>

Now I would expect the value to equal 6, but when i inspect it the value is
, thus when submitting the form saves the image_id as 0.

And i also realise that if i put i would get the id, but that's not the way I thought it worked

As stupid as it may sound what am i missing?

Any help appreciated


Answer Source

You should be doing it like

<%= f.hidden_field :image_id, value: %>

@image holds the record, not id

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download