I’m following this rails cast to create a nested form:
http://railscasts.com/episodes/196-nested-model-form-part-1
I have a ‘word’ model which has an image and when updating the word I want to update the image as well. I’m using rails 3.2.3.
My issue is that the image form doesn’t show on the page.
word.rb:
class Word < ActiveRecord::Base
attr_accessible :name
validates_presence_of :name
belongs_to :category
attr_accessible :category_id
has_one :image
accepts_nested_attributes_for :image, :allow_destroy => true
attr_accessible :image_attributes
end
words/_form.html.erb:
<%= form_for @word, :html => {:multipart => true} do |f| %>
<% if @word.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@word.errors.count, "error") %> prohibited this word from being saved:</h2>
<ul>
<% @word.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :category %>
<br/>
<%= f.collection_select(:category_id, Category.all, :id, :name) %>
<br/>
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<br/>
<%= image_tag @word.image.file_url(:thumb).to_s unless @word.image.nil? %>
<%= f.fields_for :image do |builder| %>
<br/>
<%= builder.label :file, "Image" %> <br/>
<%= builder.file_field :file %>
<%= builder.hidden_field :file_cache %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Any help is greatly appreciated, cheers!
Issue solved by deleting old ‘word’ entries and adding
to the ‘new’ action in words_controller.