mynameisrufus
mynameisrufus

Reputation: 81

Rails form helper fails

Ok this is my first app in rails so hopefully this is a simple problem.

Here is my object:

- !ruby/object:ProductImage
attributes:
image_id:
product_id:
created_at:
updated_at:
attributes_cache: {}

This works:

<%= image_form.text_field :product_id %>

But I get undefined method `image_id' for:

<%= image_form.text_field :image_id %>

I just don't get it...

Cheers for any help on this.

This is the actual partial:

<div class="image">
  <% new_or_existing = product_image.new_record? ? 'new' : 'existing' %>
  <% prefix = "product[#{new_or_existing}_product_image_attributes][]" %>
  <% fields_for prefix, product_image do |i| -%>
    <div class="input select">
      <%= i.text_field :image_id %>
      <%= link_to_function "remove", "$(this).up('.image').remove()" %>
    </div>
  <% end -%>
</div>

P.S a text field is being used just as an example

Upvotes: 0

Views: 154

Answers (2)

David Medinets
David Medinets

Reputation: 5618

ID fields should not be text_fields, they should be checkboxes or drop-down select boxes. Why would your use know what the Rails ID is?

Upvotes: 0

Jaime Bellmyer
Jaime Bellmyer

Reputation: 23307

Can you post the entire form? I have a hunch that maybe you did:

<% form_for :image do |image_form| %>

instead of:

<% form_for :product_image do |product_image_form| %>

Of course, I'd rethink displaying a form where people are manually entering id values in order to join an image to a product, but I understand you're in learning mode right now.

Upvotes: 1

Related Questions