Christian Fazzini
2010-Aug-26 10:29 UTC
How to display photos on edit and file_field on new
My form partial is being used to create new, and also for editing.
<% form_for @deal, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :city_id %>
<%= f.collection_select :city_id, City.find(:all), :id, :name,
{ :prompt => ''Select a city'' }, {:class =>
''foobar''} %>
</p>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.label :value %><br />
<%= f.text_field :value %>
</p>
<p>
<%= f.label :discount %><br />
<%= f.text_field :discount %>
</p>
<p>
<%= f.label :savings %><br />
<%= f.text_field :savings %>
</p>
<p>
<%= f.label :goal %><br />
<%= f.text_field :goal %>
</p>
<p>
<%= f.label :countdown %><br />
<%= f.text_field :countdown %>
</p>
<p>
<%= f.label :purchased %><br />
<%= f.text_field :purchased %>
</p>
<% f.fields_for :photos do |builder| %>
<p class="fields">
<%= builder.label :photo, "Photos" %><br />
<% if !@deal.new_record? %>
<%= image_tag :photo.url(:small) %>
<% else %>
<%= builder.file_field :photo %>
<% end %>
<br />
<!--
<%= builder.check_box :_destroy %>
<%= f.label :destroy, "Remove photo" %>
-->
<%= builder.hidden_field :_destroy %>
<%= link_to_function "remove",
"remove_fields(this)" %>
</p>
<% end %>
<p><%= link_to_add_fields "Add photos", f, :photos
%></p>
<% f.fields_for :features do |builder| %>
<p class="fields">
<%= builder.label :description, "Features" %><br />
<%= builder.text_area :description, :rows => 3 %><br />
<!--
<%= builder.check_box :_destroy %>
<%= f.label :destroy, "Remove description" %>
-->
<%= builder.hidden_field :_destroy %>
<%= link_to_function "remove",
"remove_fields(this)" %>
</p>
<% end %>
<p><%= link_to_add_fields "Add feature", f, :features
%></p>
<p>
<%= f.submit ''Save'' %>
</p>
<% end %>
--------------
Im having trouble with the following lines:
<% if !@deal.new_record? %>
<%= image_tag :photo.url(:small) %>
<% else %>
<%= builder.file_field :photo %>
<% end %>
<br />
When I am creating a new deal. The file_field appears appropriately.
But if I am editing a deal with existing photos, I cant get <%image_tag
:photo.url(:small) %> to show...
All this is inside the <% f.fields_for :photos do |builder| %> loop.
Which is needed for <%= builder.hidden_field :_destroy %> and
<%link_to_function "remove", "remove_fields(this)" %>,
which are meant
to remove photos when the form is submitted.
How can I get all this to work?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Christian Fazzini
2010-Aug-26 10:52 UTC
Re: How to display photos on edit and file_field on new
Ok i used <%= image_tag builder.object.photo.url(:small) %> instead of <%= image_tag :photo.url(:small) %> Seems to work now On Aug 26, 6:29 pm, Christian Fazzini <christian.fazz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My form partial is being used to create new, and also for editing. > > <% form_for @deal, :html => { :multipart => true } do |f| %> > <%= f.error_messages %> > > <p> > <%= f.label :city_id %> > <%= f.collection_select :city_id, City.find(:all), :id, :name, > { :prompt => ''Select a city'' }, {:class => ''foobar''} %> > </p> > <p> > <%= f.label :title %><br /> > <%= f.text_field :title %> > </p> > <p> > <%= f.label :description %><br /> > <%= f.text_area :description %> > </p> > <p> > <%= f.label :price %><br /> > <%= f.text_field :price %> > </p> > <p> > <%= f.label :value %><br /> > <%= f.text_field :value %> > </p> > <p> > <%= f.label :discount %><br /> > <%= f.text_field :discount %> > </p> > <p> > <%= f.label :savings %><br /> > <%= f.text_field :savings %> > </p> > <p> > <%= f.label :goal %><br /> > <%= f.text_field :goal %> > </p> > <p> > <%= f.label :countdown %><br /> > <%= f.text_field :countdown %> > </p> > <p> > <%= f.label :purchased %><br /> > <%= f.text_field :purchased %> > </p> > > <% f.fields_for :photos do |builder| %> > <p class="fields"> > <%= builder.label :photo, "Photos" %><br /> > > <% if !...@deal.new_record? %> > > <%= image_tag :photo.url(:small) %> > <% else %> > <%= builder.file_field :photo %> > <% end %> > > <br /> > > <!-- > <%= builder.check_box :_destroy %> > <%= f.label :destroy, "Remove photo" %> > --> > > <%= builder.hidden_field :_destroy %> > <%= link_to_function "remove", "remove_fields(this)" %> > </p> > <% end %> > > <p><%= link_to_add_fields "Add photos", f, :photos %></p> > > <% f.fields_for :features do |builder| %> > <p class="fields"> > <%= builder.label :description, "Features" %><br /> > <%= builder.text_area :description, :rows => 3 %><br /> > > <!-- > <%= builder.check_box :_destroy %> > <%= f.label :destroy, "Remove description" %> > --> > > <%= builder.hidden_field :_destroy %> > <%= link_to_function "remove", "remove_fields(this)" %> > </p> > <% end %> > > <p><%= link_to_add_fields "Add feature", f, :features %></p> > > <p> > <%= f.submit ''Save'' %> > </p> > <% end %> > > -------------- > > Im having trouble with the following lines: > > <% if !...@deal.new_record? %> > > <%= image_tag :photo.url(:small) %> > <% else %> > <%= builder.file_field :photo %> > <% end %> > > <br /> > > When I am creating a new deal. The file_field appears appropriately. > But if I am editing a deal with existing photos, I cant get <%> image_tag :photo.url(:small) %> to show... > > All this is inside the <% f.fields_for :photos do |builder| %> loop. > Which is needed for <%= builder.hidden_field :_destroy %> and <%> link_to_function "remove", "remove_fields(this)" %>, which are meant > to remove photos when the form is submitted. > > How can I get all this to work?-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.