Document upload (doc, docx, odt, etc.) and display in Rails (as text)? - file-upload

Does anybody know how to upload a document to later show in a Rails application (as text)? Is Paperclip the right gem to do this? If it is how? (I have uploaded images before with Paperclip).

I like Paperclip. It seems well documented, and has worked well for everything I have needed. (I don't personally know any of them, but the clever folks at Thoughbot have created some pretty useful stuff, for which I feel indebted to them).
Obviously, you need to add Paperclip to your Gemfile, and (if you are using bundler) do your bundle install
Add to your model
has_attached_file :aFile
Add to you controller something to catch whatever you name it in your view (probably in your create and update methods)
#profile.aFile = params[:profile][:aFile]
Probably should check for its existence, if it is a required param
if params[:profile][:aFile].blank?
redirect_to #profile
else
render :action => 'do_something_interesting_with_file'
end
And that's about it. Don't forget your config entries. For example, if you are using some kind of post-processing on the file
Paperclip.options[:command_path] = "/opt/local/bin/"
I found this to be extraordinarily helpful
RailsCast by Ryan Bates

Related

creating mobile version of rails app

I'm trying to work out how to redirect mobile users to other views in my rails app, but I'm missing something as it's not loading the mobile view from my device
in application_controller.rb, I added:
def check_for_mobile
session[:mobile_override] = params[:mobile] if params[:mobile]
prepare_for_mobile if mobile_device?
end
def prepare_for_mobile
prepend_view_path Rails.root + 'app' + 'views_mobile'
end
def mobile_device?
if session[:mobile_override]
session[:mobile_override] == "1"
else
# Season this regexp to taste. I prefer to treat iPad as non-mobile.
(request.user_agent =~ /(iPhone|iPod|Android|webOS|Mobile)/) && (request.user_agent !~ /iPad/)
end
end
helper_method :mobile_device?
then I have a file app/views/views_mobile/guidelines/index.html.erb
When I go to the index page on my iPhone it doesn't load the mobile index view - I'm not sure which part I'm missing...
Redirection for this kind of issue is a bad idea. Try responsive design. I would start with twitter bootstrap. This will give you a scaffold system to start with that will adjust to different screen sizes.
Also this is not uniquely a ruby on rails issue. This is a UI design issue.
Here's some good ideas.
Dont duplicate your views. it will just be harder to maintain down the road.
Use css media queries in your css to adjust your styles.
Try not to make two sites but one site that can bend and flex as it needs to.
All that being said I did not answer your specific question but instead tried to show you the way your going about it is wrong so I will understand if you dont mark this as correct.
Responsive resources.
http://twitter.github.io/bootstrap/
http://neat.bourbon.io/
http://www.abookapart.com/products/responsive-web-design
http://www.w3.org/TR/css3-mediaqueries/
Unless you left it out of your code snippet, I don't see where you're calling you're calling your check_for_mobile method. You probably need to add a before_filter to application.rb like:
before_filter :check_for_mobile
I also suggest you check out Ryan Bates Railscast on how to incorporate mobile-specific views into your Rails app. His strategy is similar to what you're trying to achieve, but instead of using a subfolder for your views he solves the same problem very elegantly by creating a 'mobile' MIME type. Check it out here:
http://railscasts.com/episodes/199-mobile-devices?view=asciicast

Attach document to a contact form in rails 3.2

I have created a contact form which will email me once sent. Now I need to be able to have the option of attaching a document that will be emailed to me also. i dont need to save it in a db as it will be with the email and downloaded from there.
Im new to rails so would like to see what other people have done in this situation, ive done some reading and see that i will need the gem paperclip in most instances? Also i have looked at jquery/paperclip in github but this seems a lot of work for what i hope is a small piece of work? I could be wrong here mind, so apologies if i am
Any help greatly appreciated
What you would have to do is use carrierwave gem or paperclip gem to upload the file to a specified folder in your public directory. I would do this using ajax that will allow you to upload the file instantly once you select the file to be sent. This would allow you to upload the attachment and have the file path known and ready so when you click submit, the following code will execute allowing you to send the file. You can also add a line of code to delete the file after it sent successfully if you don't want to accumulate files on your server. More documentation can be found here..
http://guides.rubyonrails.org/action_mailer_basics.html#sending-emails-with-attachments
class ApplicationMailer < ActionMailer::Base
def welcome(recipient)
attachments['free_book.pdf'] = File.read('path/to/file.pdf')
mail(:to => recipient, :subject => "New account information")
end
end

What do I need to change im my App to migrate Restful_Authentication to Devise?

I found a tutorial here https://github.com/plataformatec/devise/wiki/How-To:-Migrate-from-restful_authentication-to-Devise-, however it seems like it is missing pieces of what to do, for example the restful_authentication plugin is still there...how do I remove it? Then how does Devise know what to get for my App...I've tried it but it just keeps breaking my App.
Erase everything in the user.rb controller (or your controller)
Make Sure all of Restful_Authenication is gone (there is quite a bit of includes that may be presnet)
Make sure devise_for :users is present in the routes.rb file
Look for method errors and replace them back in the User.rb controller.
Think that fixed it.

Devise authenticates before user/<id> but I want to turn it off

I've got Devise working on my Ruby on Rails application but viewing the user requires authentication and I don't want that. I've tried setting authenticate_user like so:
class UsersController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
..
end
But it still redirects to the sign_in page. Can anyone point me in the right direction?
Cheers,
Rim
PS: Please excuse my n00b-ness
Doh!
I was getting confused and had the wrong path. I been scratching my head for ages on that.
I fixed it...
I always use to copy controller files from devise gem folder to my applications controller folder
The devise controller files can be found here /usr/lib/ruby/gems/1.8/gems/devise-1.1.2/app/controllers/ (may be at different location in your case)
Copy devise folder in there and paste it to app/controllers/ and then customize registrations_controller as per your need
But I believe there must be some good solution on this. By the time you can use this.. :)

Rails3 UJS response not evaluated

It gives me the creeps,i'm done, i need some help here, i reverted multiple times back but i can't find the error.
Simple controller (customers),a simple form for adding a customer via :remote => true and the controller does respond_to do |format| { format.js } . Works fine, renders my create.js.rjs template.
I work for a few hours without making any javascript changes or changes to my controllers or authorization etc.. and when i try it again it's not working anymore.
What i mean with not working: Controller gets called, record saved, all partials rendered. But no javascript evaluated, not even a simple alert(1) at the beginning of the file.
I tried with different prototype.js versions and different rails.js versions, but nothing helped. I hope someone has a clue about this or already experienced this.
It's not that i don't want to post code. But it won't help. Its basic code that works and, after some changes where i don't know what i really changed (some locales here, some css there, html templates from a completely different controller a bit..)..
Currently developing with: ruby 1.9.2, rails 3.0.3, prototype 1.7 RC3, rails.js from github.
SOLVED
How stupid, I missed the part where the template naming changed. My application templace was named "application.rhtml". It worked until now. As it stopped to work, I changed it to "application.html.erb" and now it's working.