Paperclip with Sinatra, AdapterRegistry-No handler errors - sinatra

I have used the Paperclip gem with Rails before without any problem. Now I am trying to use it in the same way for uploading an image in a Sinatra Application with Active Record. But I get the following error when I press Submit:
Paperclip::AdapterRegistry::NoHandlerError at /posts
No handler found for {:filename=>"194618-800.jpg", :type=>"image/jpeg", :name=>"post[image]", :tempfile=>#<File:/var/folders/5z/bl3r01mx0sbbljpfnryxjhqr0000gn/T/RackMultipart20131116-12697-jbv4i3>, :head=>"Content-Disposition: form-data; name=\"post[image]\"; filename=\"194618-800.jpg\"\r\nContent-Type: image/jpeg\r\n"}
This is my model:
class Post < ActiveRecord::Base
include Paperclip::Glue
belongs_to :user
has_many :tags
has_attached_file :image, :styles => {:thumb => "150x150" }
end
And this is part of my form, where the image is selected:
<%= label :post, :image %>
<input type="file" name="post[image]"></input>
I havent forgot to put enctype="multipart/form-data"
I have also created and run the migration, using this line:
add_attachment :posts, :image
I have been searching, but could not find a solution, so would appreciate if somebody gives me a hint. Thank you

Related

How to get the name of the current user in Refinery?

In RefineryCMS, for some functionality I have to store the present logged in user's first name and last name.
How to get logged in user's first name and last name?
While going through this implementation, I found that refinery user table does not have column to store first name, last name or any basic details about user.
So, first step is to create a migration file having first_name, last_name etc as per requirement.
$ rails generate migration AddFirstNameLastNameToRefineryUsers
first_name:string last_name:string
Then run
$ rake db:migrate
Now, create a decorators for user model.
Path :: /app/decorators/models/refinery/user_decorator.rb
Create a decorator : Refinery Documentation Link for Creating Decorator
Add this line inside that model
attr_accessible :first_name, :last_name
validates :first_name,:last_name, :presence => true
Now add this line at view area
/app/views/users/new.html.erb
<div class='field'>
<%= f.label :first_name %>
<%= f.text_field :first_name, :class => 'larger widest' %>
</div>
<div class='field'>
<%= f.label :last_name %>
<%= f.text_field :last_name, :class => 'larger widest' %>
</div>
Now, Rails will save the values, when user makes first time login.

Display records in refinery page

I need to get the record from database and i want to display that record value on home page. My table name is refierny_about. I have a model. It was created while I generating the engine.
Thanks for your advise.
My guess is "About" is your custom extension do this.
The syntax here is: The Refinery Namespace::Your Extension Namespace::Extension Model Name
so you can use the controller is Refinery::Abouts::About.all
app\decorators\controllers\refinery\pages_controller_decorator.rb here past this code
Refinery::PagesController.class_eval do
def home
#posts = Refinery::Abouts::About.all
end
end
and its your html code.
<% #posts.each do |a| %>
<p><%= link_to about.title, refinery.abouts_about_path(a) %></p>
<% end %>

Rails paperclip, Edit form file_field not assigned

I used paperclip to attach an avatar onto my user, in my Model:
has_attached_file :avatar,
:styles => {square_tiny: '50x50#', square_small: '100x100#', square: '200x200#'}
I have a form
<%= form_for(#user_profile,
:url => { :controller => :user_profiles, :action => :update_general_info, :id => #user_profile.id },
:html => { :multipart => true,
:class=> "form-horizontal" }) do |f| %>
<div class="control-group">
<%= f.label :avatar, :class => "control-label" %>
<div class="controls">
<%= f.file_field :avatar %>
</div>
</div>
....
<% end %>
The upload works perfect, but I come back and EDIT my user, the file field says 'no file chosen'. And since I am validating presence of that avatar, every time a user edit his details, he has to upload his avatar again...
How do I work around that?
I thought the :multipart => true would help but it didn't.
There is absolutely no good way for a page to set a value to a file field, and that's for security reasons.
If the browser allowed a page or a JS script to set a value to a file field that would allow a malicious page to preset the file field value with some system or passwords file. And that would be a massive security hole.
What I do in that case is I display the already saved file as a link that the user can click to download. You can then provide little AJAX links to delete (the file is deleted with an AJAX call and the link replaced with a new file input) and replace (the link is replaced with a file input).
Your last option would be to use AJAX to upload the file. If you use AJAX for a file upload you'll POST to a hidden frame so the file input will keep its selected value.
Either way keep in mind that any change to file field value has to be user initiated.

How exactly does Rails prefills forms?

I have a (simple) question for my own curiosity:
I'd like to find out how Rails prefill forms with posted values like... you know, when there's a validation error on some models' attributes then you do something like "render :edit" and the form is magically prefilled.
What exactly are the mechanisms that Rails use to do such a thing? I didn't manage to find any documentation on this subject and I'd like to understand the magic.
So if someone can give me some explanations on this subject, I'll be glad to read that!
Thanks!
[Edit] And a subsidiary question: when a model inherits from another (STI) do we have to do something in particular to prefill forms?
You are mostly using the form_for helper in this style:
<%= form_for #person do |f| %>
<!-- Some more stuff here -->
<%= f.text_field :first_name %><br />
<!-- Some more stuff here -->
<% end %>
What this essentiall does is, it generates a text field that is filled with the value of #person.first_name.to_s. When an error happens, #person.first_name is filled with the errornous value. If you create a person (#person = Person.new), then #person.first_name.to_s is "".
So rails just fills the text field with the value, the attribute has.
f by the way is a rails FormBuilder. It's methods are documented here, if you want to take a closer look at the source.

select and onChange in a Ruby on Rails form

I browsed all SO questions and answers about this topic but I'm still unable to make my scenario work.
I want to trigger a click button action when a dropdown menu option is selected ; seems simple and should be very common with AJAX.
Here are the relevant excerpts of my code:
<%= form_for(#test, :html => {:id => "form_id", :name => "MyForm", :remote => "true"}) do |form| %>
<%= form.label "Menu1" %>
<%= form.select (:Menu1, [["Option1","value1"],["Option2","value2"]], :html_options=>{:onChange=>"javascript: this.form.apply_button_name.click();"}) %>
<!-- more select menus and text fields here -->
<div class="actions">
<%= form.submit "Apply", :name => "apply_button_name", :remote => "true" %>
</div>
<% end %>
I used ":remote => "true" both for the form and the button because that's the only way to get AJAX working. I also tried with and without explicit "html_options" and "javascript:", after I browsed some SO answers that suggested that but that did not help. I also tried onSelect, and onClick instead of onChange, but still no luck.
The generated HTML is the following:
Menu1
<select id="test_Menu1" name="test[Menu1]"><option value="value1">Option1</option>
<option value="value2" selected="selected">Option2</option></select>
As you can see, there's no onChange event handler in the HTML code ; WHY? Anyone is seeing what am I doing wrong?
Thanks for any help.
Modify your call to form.select, like this:
<%= form.select :Menu1, [["Option1","value1"],["Option2","value2"]], {},
:onChange=>"javascript: this.form.apply_button_name.click();" %>
If you examine the documentation for:
API Dock Ruby on Rails select
You will see that the select form helper takes the form:
select(object, method, choices, options = {}, html_options = {})
If you don't pass anything for the option hash (in your case this will be an empty hash), the form thinks that your html_options hash are your options hash, and gets confused.
A way to check this is to add something like {:onchange=> "alert('Hello');"} and either see if the event successfully triggers, or alternatively, in your actual web page, right click on the select element and inspect it. If no onchange option is present in the html, that means that your rails form helper is indeed confusing the html_options with the other options. So, what you should have:
<%= form.select (:Menu1, [["Option1","value1"],["Option2","value2"]], {}, {:onChange=>"handler();"} %>
MAKE SURE TO INCLUDE THE EMPTY HASH FOR THE OPTIONS BEFORE THE HTML OPTIONS AND YOU SHOULD BE FINE. I don't think you even need to have the html_options and javascript stuff you have.
Lastly, if onChange doesn't work, try to use onchange with no capital C.