Rails 3.1 nested form issue - ruby-on-rails-3.1

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.

Related

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:

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

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 do I add form errors for a model that belongs to another model, rails 3.1

I'm following along with the Rails Guides - Getting Started tutorial. It makes a basic Post model, and a Comment model that belongs to Post.
I have added a simple validation to the Comment model, and it works, but I can't figure out how to get form errors to display if I fill it out wrong.
Here is my comment.rb model
class Comment < ActiveRecord::Base
validates :body, presence: true
belongs_to :post
end
Here is the original form for adding a comment, it's in posts/show.html.erb
<h2>Add a comment:</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<div class="field">
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
And the original create action in comments_controller.rb
class CommentsController < ApplicationController
def create
#post = Post.find(params[:post_id])
#comment = #post.comments.create(params[:comment])
redirect_to post_path(#post)
end
end
I've tried quite a few things, but it all feels like fumbling around in the dark. Can someone point me in the right direction please?
Take a look at the dynamic_form gem - this used to be part of Rail itself but was extracted a while back. With it, you can display errors inline like this:
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
<%= f.error_message_on :commenter %>

Resources