Add font awesome icons to a form label - ruby

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 -%>

Related

validations failing as blank when fields not blank

I am runnning ruby 1.9.3 and rails 4.1.4
trying the following validations
in the models\profile.rb
attr_accessor :password
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :password, :confirmation => true #password_confirmation attr
validates_length_of :password, :in => 6..20, :on => :create
before_save :encrypt_password
in view profiles\new.html.erb
from the form
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.label :interests %><br />
<%= f.text_area :interests %>
</p>
<p>
<%= f.label :zip %><br />
<%= f.text_field :zip %>
</p>
<p>
<%= f.label :country %><br />
<%= f.country_select :country, ["United Kingdom"] %> </p>
using strong parameters controllers\profile_controller,rb
class ProfilesController < ApplicationController
def new
#profile = Profile.new
end
def create
#profile = Profile.new(params[profile_params])
if #profile.save
flash[:notice] = "You signed up successfully"
flash[:color]= "valid"
else
flash[:notice] = "Form is invalid"
flash[:color]= "invalid"
end
render "new"
end
private
## Strong Parameters
def profile_params
params.require(:profile).permit(:name, :email, :password, :interests,:zip)
end
end
validations always fail saying fields are blank. By causing an exception the report shows model profile with its fields populated. It is as if the data is simply not accessible so I suspect I'm misuing strong parameters sonehow.
Any opinions welcomed.
The profile_params function is going to return a hash for you with the data directly.
What that basically does is it filters the params hash for the 'profile' key's value for the keys named as the parameters of permit(...)
So the way to fix is to write
#profile = Profile.new(profile_params)
instead of
#profile = Profile.new(params[profile_params])
With strong parameters you almost never interact with the params array directly from controller actions (such as create). The private strong parameter methods will handle that for you and by that, only safe, filtered data will be given to your controller actions.

CarrierWave Uploading Using SimpleForm; Why do "remote_image_url" and "image" save to same database column?

I have a simple_form with an image uploader, which is connected to CarrierWave:
<%= simple_form_for #house do |f| %>
<%= f.input :price %>
<%= f.input :town %>
<%= f.input :description %>
<%= f.input :bedrooms %>
<%= f.input :bathrooms %>
<%= f.input :url, label: "URL" %>
<%= f.input :rating %>
<%= f.input :remote_image_url %>
<%= f.file_field :image %>
<%= f.button :submit, "Add House" %>
<% end %>
For some reason the two fields
<%= f.input :remote_image_url %>
<%= f.file_field :image %>
are storing to the exact same column in my database, which is named "image". I don't understand why.
By carrierwave, you can either upload file/picture from your machine directly by <%= f.file_field :image %>, or from the link on remote server using helper method 'remote_image_url' . This naming convention remote_yourimagefield_url is important, as carrierwave will come to know by this naming structure to grab files from the remote server. As :remote_image_url points to the same column as :image, therefore they are stored in the same column

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.. ;)

Rails 3.1 nested form issue

Ive got the following scenario:
cmsasset model that is container for image using paperclip to upload and store, location model that can have attached cmsassets in a habtm join table approach
location accepts nested attributes for cmsassets here's the exact model code:
accepts_nested_attributes_for :cmsassets, :allow_destroy => true
now when i try to use the following in my form code the nested form is not displayed:
<%= form_for #location do |f| %>
<%= render 'shared/error_messages', :target => #location %>
<p>Name<br />
<%= f.text_field :name %></p>
<p>Description<br />
<%= f.text_area :description %></p>
<p>Location Tags (Separated by a Comma)<br />
<%= f.text_field :tag_names %></p>
<%= render :partial => 'shared/contact_info_form', :locals => {:f => f} %>
<p>Splash Image:<br />
<%= f.fields_for :cmsassets do |cmsasset_form| %>
<%= cmsasset_form.number_field :client_id, :type => 'hidden', :value => session[:current_client] %>
<p>Image Name:<br />
<%= cmsasset_form.text_field :name %></p>
<p>Description:<br />
<%= cmsasset_form.text_field :description %></p>
<p><%= cmsasset_form.file_field :attachment %><br />
<%= cmsasset_form.text_field :cms_asset_type, :type => 'hidden', :value => 'Splash' %></p>
<% end %></p>
<p>Display Address:<br />
<%= f.text_field :display_addr %></p>
<p>Latitude<br />
<%= f.number_field :latitude %></p>
<p>Longitude<br />
<%= f.number_field :longitude %></p>
<p>Trigger Radius<br />
<%= f.number_field :trigger_radius%></p>
<p>Published >> <%= f.check_box :published %></p>
<p>Expiration Date<br />
<%= f.text_field :expiration_date %></p>
<br />
<%= f.submit "Save" %>
However, when I make cmsassets singular in the nested form call it renders but fails obviously because of the habtm data structure...
whats crazy is that this approach works using the nested_form gem, with the sole exception of paperclip causing a failure in this case because we use dynamic styles at the model level..
if anyone can shed some light on either of these issues i'd be very interested to hear some ideas!
It's not valid HTML to write nested forms.
Try to place the upload form outside the main form.

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