One Month Rails | Image Upload with Paperclip - missing image - ruby-on-rails-3

I am following the One Month Rail course and I have this problem: I used the Paperclip gem to take care of the image uploading. I followed the instructions; however, when I upload my image, it does not display correctly, but instead display a missing image
Screenshot of Chrome Console: https://dl.dropboxusercontent.com/u/2570626/Screen%20Shot%202014-02-06%20at%209.51.13%20AM.png
My Github folder is: https://github.com/phanatuan/pinteresting
Really appreciate your help,
Tuan

in your pins/_form.html.erb partial you mistyped your file_field for :image
Replace:
<div class="field form-group">
<%= f.label :image %>
<%= f.file_field :image, class: 'form-control' %>
</div>
Everything else is ok.

Related

configure an existing controller to display devise notifications: undefined local variable or method `resource'

I have created a Rails 3 app with devise and i also have a controller called 'account' . When an user signs in with devise's sign in view , he's redirected to a view of account controller. So the notifications od devise "sign in successful" etc are not displayed. When i tried adding
<%= devise_error_messages! %>
to this view It gave an error
NameError in Account#welcome
undefined local variable or method `resource' for #<#:0x4e9fe70>
Can anyone pls tell me how to make this account controller display devise's notifications??
I tried reading the docs in github but didnt help..
I guess we have do modify routes file as its showing error in resources..
Update1:
Layout file:
<div id="maincontent">
<div class="entry">
<% flash.each do |name, msg| %>
<%= content_tag :section, msg, :id => "flash_#{name}", :class => "flash" %>
<% end %>
<!--<p class="commentbar"> Signup</p>-->
<%= yield %>
</div>
</div>
Once you installed Devise, there will be a block of guide. One paragraph is about the messages:
Ensure you have flash messages in app/views/layouts/application.html.erb.
For example:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
So, to display success messages/notice, use
<%= notice %>
<%= alert %>
These two are also Rails default helper to show flash messages. So you are fine with both Devise and other normal flash messages with this setting.

Changing Error Message in Rails Model

I have a Rails (3.1) app with a model validation that, when triggered, puts the model and field name before the message, e.g.:
Profile image profile image content type Only jpeg, gif and png files are allowed for profile pictures
Is there a way to avoid that, so it reads:
Only jpeg, gif and png files are allowed for profile pictures
model.rb validation:
validates_attachment_content_type :profile_image,
:content_type => ['image/jpeg', 'image/png', 'image/gif'],
:message => "Only jpeg, gif and png files are allowed for profile pictures"
The error appears as a part of this code in my layout:
<% if object.errors.any? %>
<div class="alert alert-message error" data-alert="alert">
<a class="close" data-dismiss="alert">×</a>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
My hunch is that msg is not actually the message but the entire error hash, and so calling <%= msg %> actually converts the whole hash to a string, including the keys. You could confirm this with <%= msg.class %>.
Assuming the view code you posted is a partial it would be helpful to see the view that includes the partial. If it's not a partial it would be useful to see the surrounding code.

tinymce issue with rails 3

I have legacy rails 3 app, where I need to modify a page to use tinymce to edit a text_area.
There are existing pages in this app that already use tinymce.
For reasons I cannot go into here, I cannot use any of the tinymce plugins that are available.
Now my problem is as follows.
I have a model called Sections, that has two attributes, section_name and html.
I want to be able to edit the html using tinymce.
My view has a form which is as follows
<%= form_for #section , :url => update_section_path , :method => :put do |f| %>
<%= f.hidden_field :id %>
<%= f.label :section_name , "Section Name" %>
<%= f.text_field :section_name %> <br />
<%= f.label :html, "Html" %>
<%= f.text_area :html %>
<%= f.submit "Update" %>
<% end %>
The form appears as expected.
The TinyMCE editor also appears on the page with the original html.
The problem is that when I click on the Update button, the put query sent to my server, does not contain the new modified content of the Html text_area. It sends back the original text that was in that text_area.
Could anyone help me understand why.
Thanks in advance
=Puneet
I found an issue in my html which was causing this. Once I fixed that issue it worked fine

Ruby on Rails simple_form_for all.each do

I am using Rails 3.0, Ruby 1.9.2 and the Plataformatec simple_form gem. This code works with a form_for but not simple_form_for:
<%= simple_form_for(#provider) do |f| %>
<% Car.all.each do |c| %>
<div>
<%= check_box_tag :car_ids, c.id, #store.cars.include?(c), :name => 'store[car_ids][]' %>
$<%= c.cost %> | <%= c.description %>
</div>
<% end %>
<div class="actions">
<%= f.submit "New" %>
</div>
<% end %>
How do I get it to work with simple_form_for?
Thanks in advance!
You can't use simple_form right the same way as form_for.
For example ther is no any check_box_tag method in simple_form gem. There is ONLY inuput fields that you can specify with :as option. So your check_box_tag will be converted to
f.input car_ids, ..., :as => :check_box
Checkout Usage, Rdoc and other useful stuff https://github.com/plataformatec/simple_form
The problem was in the controller code.
In the "new" controller action I can't simply perform:
#provider = Provider.new(params[:provider])
as one would normally.
Instead I have to process each parameter separately:
#provider.location = params[:provider][:location]
etc...
For the Car check boxes, I add each car_id from the car_ids parameter to the "has_many" cars model association one at a time:
car_ids = params[:provider][:car_ids]
car_ids.each do |cid|
#provider.cars << Car.find(cid)
end
Then I can call:
#provider.save!
And it saves correctly (my initial problem was that it wasn't saving the selected Cars).
For some reason, I was able to figure this out only after posting the question here. Funny how that works.
Thanks all for your replies!

what are the paperclip rails3 file_field {options}

I installed paperclip and use it to handle photo attachment.
I have a simple form where i can upload photo's works perfect by the way.. But i want to adjust the "browse" button, for example change it's name... Or even better hide the text field, so that there's only an upload button with the text, "upload photo..."
This the form code:
<div class="photoupload"
<%= f.label :description %>
<%= f.text_field :description %>
<%= f.file_field :photo %>
</div>
Regards!
Seems like you can't really change the button, but you can make it invisible, set its z-index (so it is underneath everything else) and use css to position some styled block on top of it.
http://www.quirksmode.org/dom/inputfile.html