I'm using accepts_nested_attributes_for in one of my Rails app. i have three models :album has_many :photo and each :photo has_and_belongs_to_many :tag. User can add more photo in album using paperclip and jquery.multifile.js. every thing working fine but the main problem is the hash is not properly create.
album => {:name => "", :body => "", :photos_attributes => {"tags_attributes" => {:name => "abc"}, "photo" => {"file" => "tempfile"}}}
but i need.
album => {:name => "", :body => "", :photos_attributes => "photo" => {"file" => "tempfile", "tags_attributes" => {:name => "abc"}}}}
My view is: please consider the gallery => album and attachment => photo
<%= form_for(#gallery, :html => {:multipart => true}) do |f| %>
<div id = "gallery_name_formField">
<%= f.label "Gallery Name:" %><br />
<%= f.text_field :name, :name => "gallery[name]" %><br />
</div>
<div id = "gallery_body_formField">
<%= f.label "Gallery Description:" %><br />
<%= f.text_area :body, :name => "gallery[body]" %><br />
</div>
<%= f.fields_for :attachments do |af| %>
<div id = "gallery_file_formField">
<%= af.label "Select The Attachment:" %><br />
<%= af.file_field(:attachment, :name => "gallery[attachments_attributes][][attachment]")%>
</div>
<div id="tags">
<%= af.fields_for :tags do |tf| %>
<%= tf.label :name, "Tag:" %><br />
<%= tf.text_field :name, :id => "mySingleField_0", :name => "gallery[attachments_attributes][][tags_attributes][][name]" %>
<% end %>
</div>
<% end %>
<div class="attachment_submit_formField">
<%= f.submit(:id => "create_gallery") %>
</div>
<% end %>
Have you tried to use nested form gem? Give it a try
Its very straightforward way to have multiple nested forms.
It works for me.
Related
I have a form_for select where the options are being defined from within the model. I am trying to get it to display a placeholder option but cannot figure out how to.
The Model:
class Factoid < ActiveRecord::Base
attr_accessible :description, :name, :title
validates_presence_of :description, :name, :title
validates_uniqueness_of :title
NAMES = "Angela", "Geordie", "Jared", "Jennifer", "Kevin", "Matthew", "Oscar", "Owen", "Regina", "Todd", "Vaibhavi", "Zack"
UNRANSACKABLE_ATTRIBUTES = ["id", "updated_at"]
def self.ransackable_attributes auth_object = nil
(column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys
end
end
The Form:
<%= form_for #factoid, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :title, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :title, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :description, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :description, :class => 'text_area' %>
</div>
</div>
<div class="control-group">
<%= f.label :name, :class => 'control-label' %>
<div class="controls">
<%= f.select :name, :collection => Factoid::NAMES %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
factoids_path, :class => 'btn' %>
</div>
<% end %>
The second issue is that the dropdown menu is displaying the word "collection" at the top of it (See screenshot below). How do I get read of that. Ideally I want to have a dropdown menu with a placeholder of "Names" that is also displayed at the top when the dropdown menu is opened.
For your text field try something like:
<%= f.text_field :title, :class => 'text_field', value: 'my_default_value' %>
for your select try:
<%= f.select :name, Factoid::NAMES %>
See the docs for select and the rails guide for typical usage (I think the method I've shown you will not work upon submitting the form, see the guides linked for explanation, I'm not sure though).
I'm using Simple Form. I have a form for creating new items, and a form for editing existing ones. I also have two file fields for every item. Thing that bugs me is that file fields are displayed fine when creating new item, but then they are not generated at all when editing an existing item.
I had this perfectly working in Rails 3.0, now doesn't work on Rails 3.2.1.
The form:
<%= simple_form_for #item, :html => { :multipart => true } do |f| %>
<%= f.input :title, :input_html => { :maxlength => 35 } %>
<%= f.input :description, :input_html => { :maxlength => 450 } %>
<%= f.input :secure_details, :placeholder => "Serial numbers and other stuff that will remain private", :input_html => { :maxlength => 450 } %>
<%= f.association :bookmark, :collection => current_user.bookmarks(:order => :position), :include_blank => false %>
<%= f.input :warranty_until, :as => :string, :input_html => { :id =>'datepicker2' } %>
<div class="image_attachment">
<div class="attachment_text">
Update item photo<br />
<small>(will replace old one)</small>
</div>
<div class="attachment_button">
<% f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
</div>
</div>
<div class="image_attachment">
<div class="attachment_text">
Update receipt<br />
<small>(will replace old one)</small>
</div>
<div class="attachment_button">
<% f.fields_for :receipts do |receipt| %>
<%= receipt.file_field :photo %>
<% end %>
</div>
</div>
<%= f.input :public, :label => "My friends can see this item", :input_html => { :class => "right" } %>
<%= f.input :giveaway, :label => "Mark as giveaway", :input_html => { :class => "right" } %>
<div class="margin_r margin_t">
<%= f.button :submit, :class => 'small_button white right' %>
</div>
<% end %>
Basically this part of code doesn't work:
<div class="attachment_button">
<% f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
</div>
The generated HTML is just empty div.
The very same code works when creating a new item, but doesn't work when editing existing one.
Both Assets and Receipts are using Paperclip for storing images. Here is a code for Asset class:
class Asset < ActiveRecord::Base
belongs_to :item
has_attached_file :photo,
:styles => {
:thumb => "80x80#",
:small => "150x150>" }
validates_attachment_size :photo, :less_than => 550.kilobytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
end
Maybe you forgot add this line of code in your Item model:
accepts_nested_attributes_for :receipts, :allow_destroy => true
accepts_nested_attributes_for :assets, :allow_destroy => true
And add '=':
<%= f.fields_for :assets do |asset| %>
<%= asset.file_field :photo %>
<% end %>
<%= f.fields_for :receipts do |receipt| %>
<%= receipt.file_field :photo %>
<% end %>
Currently I have a model 'Locations' that has an image upload field added to the form. When I go, to lets say" update one the existing locations or even add a new one, it will upload the image correctly and display it, but will not save any of the input fields.
If I remove the upload photo field on the form and do not require it, then everything updates and saves correctly. So having an issue when a image is present, it saving the image but not the rest of the fields.
Any suggestions on why this might be happening?
Locations.rb
class Location < ActiveRecord::Base
belongs_to :region
has_many :spots
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
has_attached_file :photo,
:styles => { :thumb => "150x150#", :medium => "200x200#"},
:path => ":attachment/:id/:style.:extension",
:s3_domain_url => "adsimgstore.s3.amazonaws.com",
:storage => :s3,
:s3_credentials => Rails.root.join("config/s3.yml"),
:bucket => 'adsimgstore',
:s3_permissions => :public_read,
:convert_options => { :all => "-auto-orient" }
attr_accessible :locations, :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
end
Form
<%= form_for (#location), :html => { :multipart => true } do |f| %>
<% if #location.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#location.errors.count, "error") %> prohibited this location from being saved:</h2>
<ul>
<% #location.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :network_id %><br />
<%= f.text_field :network_id %>
</div>
<div class="field">
<%= f.label :region_id %><br />
<%= f.text_field :region_id %>
</div>
<div class="field">
<%= f.label :spot_duration %><br />
<%= f.text_field :spot_duration %>
</div>
<div class="field">
<%= f.label :frequency %><br />
<%= f.text_field :frequency %>
</div>
<div class="field">
<%= f.label :screen_count %><br />
<%= f.text_field :screen_count %>
</div>
<div class="field">
<%= f.label :ad_size %><br />
<%= f.text_field :ad_size %>
</div>
<div class="field">
<%= f.label :ad_type %><br />
<%= f.text_field :ad_type %>
</div>
<div class="field">
<%= f.label :impressions %><br />
<%= f.text_field :impressions %>
</div>
<div class="field">
<%= f.label :rate_card %><br />
<%= f.text_field :rate_card %>
</div>
<div class="field">
<%= f.file_field :photo %>
</div>
<div class="field">
<td><%= image_tag #location.photo.url(:thumb) %></td>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Did you try to comment the line
attr_accessible :locations, :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at
I've done the migration I have
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/missing/:style.png"
in my model, and im using
<%= form_for #user, :as => :user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<div class="actions">
<%= form.submit %>
</div>
To save the avatar uploaded and
<%= image_tag #user.avatar.url %>
<%= image_tag #user.avatar.url(:medium) %>
<%= image_tag #user.avatar.url(:thumb) %>
To display them all, but when I submit the new avatar it is not getting saved, I'm using
#user = User.create(params[:user])
in the create so it should be saving, what could be the problem?
<%= form_for #user, :validate => true, :html => { :multipart => true } do |f|%>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username, :disabled => 'disabled' %><br />
<%= f.label :full_name %><br />
<%= f.text_field :full_name%><br />
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<%= f.file_field :avatar %>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
ruby-1.9.2-p290 :002 > user = User.first
ruby-1.9.2-p290 :004 > user.avatar
=> /images/missing/original.png
Nothing is getting changed
avatar_file_name: nil, avatar_content_type: nil, avatar_file_size: nil, avatar_updated_at: nil>
You need to add :avatar to the attr_accessible list.
I assume that everything else for user is being saved properly?
The only thing that comes to my mind is, maybe you're using attr_accessible to protect from mass assignment and you forgot to add :avatar_file_name, :avatar_content_type, :avatar_file_size... to the whitelist?
I have a form (snippet)
<% form_for(#transfer, :html => {:multipart => true}) do |f| %>
<p>
<%= f.label :source %><br />
<%= f.text_field :source %>
</p>
<p>
<%= f.label :destination %><br />
<%= f.text_field :destination %>
</p>
<% fields_for :upload do |u| %>
<p>
<%= u.label :upload %><br />
<%= u.text_field :upload %>
</p>
<% end %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', transfers_path %>
So now in my transfers controller I can do:
#transfer = Transfer.new(params[:transfer])
#upload = Upload.find_or_create_by_md5(params[:upload])
I am able to post to a single form with XML by simply changing the params to XML like
<transfer>
<source>foo</source>
<destination>bar</destination>
</transfer>
or
<upload>
<upload>baz</upload>
</upload>
But I cannot figure out how to combine them under the same XML root
Assuming you're using Rails 2.3.x, you might want to look into adding accepts_nested_attributes_for to your Transfer model. See what-s-new-in-edge-rails-nested-attributes.
Well I haven't been able to figure out how to do this for XML, so for now I've had to settle for doing it with REST. I came across the RestClient library and looking through the source, figured out you could do nested params like this:
RestClient.post( url,
{
:transfer => {
:path => '/foo/bar',
:owner => 'that_guy',
:group => 'those_guys'
},
:upload => {
:file => File.new(path)
}
})
Send a note to the author about documenting the functionality here