How to add additional file fields to ruby on rails project? - ruby

Yesterday I have made paperclip multiple upload gallery. Today I want to customize this all and I need to make a button with on click add one more file upload field. Example you can see in this video: http://www.emersonlackey.com/article/rails-paperclip-multiple-file-uploads 28:27.
I have searched in google, but couldn't find anything.
#post form:
<% form_for #post, :html => {:multipart => true} do |t| %>
<p>
<%= t.label :title, 'Virsraksts:' %></br>
<%= t.text_field :title %></br>
</p>
<p>
<%= t.label :content, 'Teksts:' %>
<%= t.text_area :content, :class => "mceEditor"%>
</p>
<p>Pievienot jaunas bildes:</p>
<%= f.link_to_add "Add a task", :assets %>
<%= f.fields_for :assets do |asset_fields| %>
<% if asset_fields.object.new_record? %>
<%= asset_fields.file_field :asset %>
<%= asset_fields.link_to_remove "Noņemt" %>
<% end %>
<% end %>
<p>
<%= f.fields_for :assets do |asset_fields| %>
<% unless asset_fields.object.new_record? %>
<p>
<%= link_to image_tag(asset_fields.object.asset.url(:thumb)), asset_fields.object.asset.url(:original) %>
<%= asset_fields.check_box :_destroy %>
</p>
<% end %>
<% end %>
</p>
<%= t.submit %>
#post model:
class Post < ActiveRecord::Base
attr_accessible :title, :content, :assets_attributes
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
end
#asset model:
class Asset < ActiveRecord::Base
belongs_to :post
has_attached_file :asset, :styles => { :large => "640x480", :medium => "300x300>", :thumb => "100x100>" },
:url => "/assets/albums/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/albums/:id/:style/:basename.:extension"
accepts_nested_attributes_for :post, :allow_destroy => true
end

There's a great cast from R.Bates about nested form here. i've modified it so that it's now possible to add, remove photos, works with paperclip, Feel free to clone this rails3.2 app https://github.com/Saidbek/multiple-image-uploader

You are going to need the javascript to make the functions to work.
If you are a railscasts premium user you can access the link above, as Said recommended, but if you not, you should take a look at this link.

Related

Formtastic Creating a form without users

I'm creating a web application with formtastic. I installed the gem and wrote my code like this:
<%= semantic_form_for #index do |form| %>
<%= form.inputs do %>
<%= form.input :name %>
<%= form.input :born_on, :start_year => 1997 %>
<%= form.input :description, :as => :text %>
<%= form.input :female, :as => :radio, :label => "Gender", :collection => [["Male", false], ["Female", true]] %>
<% end %>
<%= form.actions do %>
<%= form.action :submit, :as => :button %>
<% end %>
<% end %>
I want the form to appear on the index page which is why I have #index. For some reason I can't do #index. How would I reference the top line so that it renders a form on the index page? Currently my index page has nothing in it, but it is defined in the controller
form_for helper expects some object that responds to fields You refer inside Your form. Here is an example of using semantic_form_for with plain Ruby object: http://affy.blogspot.com/2010/02/using-formtasic-without-activerecord.html.
Also the object You specify for form doesn't effect which page is being rendered. Are You sure You are not mixing up something? Maybe if You share a bit more of Your controller code we might help You better.

Multiple File uploads with paperclip without using Nested attributes

Whenever I have used paperclip to upload multiple images I have created a separate model called images for example and used the accepts_nested_attributes_for in the associated model..
But what if i want to add multiple images without using this. So for example if my model looks like this
class Portfolio < ActiveRecord::Base
belongs_to :sector
attr_accessible :overview, :title, :sector_id, :photo
has_attached_file :photo, :styles => { :portfolio => "680x680#"}
end
And my form looks like this
<%= form_for #portfolio do |f| %>
<%= f.label :title, "Title", :class => 'title_label' %>
<%= f.text_field :title %>
<%= f.label :sector_id, "Choose Sector", :class => 'title_label' %><br>
<%= f.collection_select(:sector_id, Sector.all, :id, :name, :prompt => "Please Select a Sector") %><br>
<%= f.label :overview, "Overview", :class => 'title_label' %>
<%= f.text_area :overview %><br>
<%= f.file_field :photo %><br><br>
<%= f.submit 'Submit', :class => 'btn' %>
<% end %>
How would i go about uploading multiple images in this scenario.. Normally within nested form for example i would use the link_to_add helper provided.
I don't have this available though with this scenario so what can i do?
is it best practive to always keep the attachments/photos separate and use nested_attributes?
Any help appreciated
To add multiple photos you need to mention, :html => { :multipart => true} in form tag as,
<%= form_for(#portfolio, :html => { :multipart => true}) do |f| %>
and in file_tag as,
<%= f.file_field :photo, as: :file, multiple: true, name: 'photo[photo]' %>
name photo[photo] so that each photo will get same name and can easily accessed in controller.
Just dtas it..
NOTE: i consider you are using jquery with paperclip to upload photos. If not try out this n let me knw if it works.. ;)

Add font awesome icons to a form label

Is it possible to use font awesome icons within a label when creating a form for example.
if my form looks like this
<%= f.label :email %>
<%= f.email_field :email, :autofocus => true %>
How would i add the i class to the label?
I have tried this
<%= f.label :email, :class => 'icon-user' %>
<%= f.email_field :email, :autofocus => true %>
but this doesn't work?
Try something like this (assuming email is a field of class User):
<%= f.label :email do %>
<i class="icon-user"></i>
<%= User.human_attribute_name :email %>
<%- end -%>

how to do a many to many model for this project in rails

I'm having trouble creating a many to many model for my project.
Basically i have a Matches & Teams model.
Teams are created prior to the Matches.
Once the match is created then i would like to add teams to it.
Match can have many teams, Teams can have many matches.
I'm currently adding teams via nested_form and adding multiple teams at once.
When submitting the form, i get an error expecting the team to be in a relationship already with the match.
I can do this with a many to one relationship but it fails with many-to-many, was wondering if there was any way to do it without doing a custom route.
Below is the form, controllers are as per default values.
Form:
<%= nested_form_for(#match) do |f| %>
<% if #match.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#match.errors.count, "error") %> prohibited this match from being saved:</h2>
<ul>
<% #match.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.date_select :date %>
</div>
<%= f.fields_for :teams, :html => { :class => 'form-vertical' } do |builder| %>
<%= builder.label "Team Name:" %>
<%= builder.autocomplete_field :name, autocomplete_team_name_teams_path, :update_elements => {:id => "##{form_tag_id(builder.object_name, :id)}" },:class => "input-small",:placeholder => "Search" %>
<%= builder.hidden_field :id %>
<% end %>
<%= f.link_to_add raw('<i class="icon-plus-sign"></i>'), :teams, :class => 'btn btn-small btn-primary' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Using a join model, the has_many :through macro and the accepts_nested_attributes_for macro you can do something like the following.
class Match
has_many :competitions
has_many :teams, :through => :competitions
accepts_nested_attributes_for :teams
end
class Competition
belongs_to :match
belongs_to :team
end
class Team
has_many :competitions
has_many :matches, :through => :competitions
end
Just make sure your form is set up to send the following data structure as params when the request reaches the create or update controller.
params => {
:match => {
# ...
:teams_attributes => [
{ :name => 'Foo', :color => 'blue' },
{ :name => 'Bar', :color => 'green' },
# ...
]
}
}

Rails Save File Checkbox

How can I make it so that my info only gets stored if the checkbox is checked. Here is what I have so far:
<% #extra.each do |extra| %>
<%= f.fields_for :purchaseds do |builder| %>
<div class="label-field">
<%= builder.label :name, extra.name %>
<p><%= extra.description %></p>
</div>
<div class="text-field">
$<%= extra.price %>
<%= builder.check_box :purchased %>
</div>
#I WOULD LIKE THIS TO ONLY GET SAVED IF THE CHECK BOX FOR PURCHASED IS CHECKED
<%= builder.hidden_field :name, :value => extra.name %>
<%= builder.hidden_field :description, :value => extra.description %>
<%= builder.hidden_field :price, :value => extra.price %>
<% end %>
<% end %>
My client asked to be able to add extra services himself, and then users could be able to choose if they want to purchase them as accessories to their order. So what I did was I made a table called Extra (for extra services) and another table called Purchased. Purchased belongs to Order and is a nested attribute.
In your purchaseds model add validations:
validates_presence_of :name, :description, :price, :if => :purchased
Update
Add :reject_if option to your purchaseds parent model in
accepts_nested_attributes_for :purchaseds, :reject_if => {|attrs| !attrs[:purchased]}

Resources