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

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

Related

pass a string to a hidden field in rails simple form

I am trying to build a simple message relay system from scratch. My form looks like this:
<%= simple_form_for #message do |f| %>
<%= f.error_notification %>
<%= f.input :content, label: "Your message" %>
<%= f.input :target_user_id, label: "Who are you sending your message to?" %>
<%= hidden_field_tag 'user_id', current_user.id %>
<%= hidden_field_tag 'sender_email', current_user.email %>
<%= f.button :submit %>
<% end %>
I am trying to pass "current_user.email" to the message model, where "Message.sender_email" is a string, but I am getting "nil" when I look at the created Message in the console. All the other fields do get passed though.
Just add this line to your controller:
#message.sender_email = #user.email
in the message create method.

l18n::InvalidLocaleData Rails

Error occurs when you click on a url http://localhost:3000/articles/new/.
Showing c:/Sites/blog/app/views/articles/new.html.erb where line #8 raised:
can not load translations from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/locale/en.yml: expects it to return a hash, but does not
File blog.html.erp
<h1>New article</h1>
<%= form_for :article do |f| %>
<p>
<%= f.label :title%><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text%><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Controller articles action new.
class ArticlesController < ApplicationController
def new
end
end
File config/routes.rb
Rails.application.routes.draw do
resource :articles
get 'welcome/index'
Rails 4.1.8
Ruby 2.1.5
Winddows 7
Delete all of the files C:\RailsInstaller\Ruby2.1.0\lib\ruby\gems\2.1.0\gems\activesupport-4.1.8\lib\active_support\locale\en.yml put - en:

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

passing value from select_tag

I want to pass selected :ammount value to my controller as :quantity. What i am doing wrong?
<%= label(:ammount, "Ammount:") %>
<%= select_tag(:ammount, options_for_select([1,2,3,4,5,6,7,8,9,10])) %>
<%= button_to 'Add to cart', line_items_path(:product_id => product.id, :quantity => :ammount) %>
You should use form here to pass the data to controller`s action:
<%= form_tag line_items_path(:product_id => product.id) do %>
<%= label(:ammount, "Ammount:") %>
<%= select_tag(:ammount, options_for_select([1,2,3,4,5,6,7,8,9,10])) %>
<%= submit_tag 'Add to cart' %>
<% end %>
All that you did in your sources is just passing symbol :ammount to a controller`s action as a quantity param.

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.

Resources