How do I display an avatar in Rails - ruby-on-rails-3

I am creating a blog where the author has there own page of titles they have written and would like to display an avatar and email address. How would i go about doing this?
Thank you

One solution is to use Paperclip. It's very simple to use and you can display the same picture in multiple sizes. For instance, similarly to SO you can show smaller avatars when on a question page, and bigger avatars when you're on a user's info page.
Example
class User < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :large_avatar => "300x300>",
:small_avatar => "100x100>" }
end
You would only need to add that to your model, and then a few extra columns for the migration. My point is that there's not a lot of overhead in using the solution. You will have to include a few other parameters for the forms you use to submit a file url, etc., but it's really simple.

http://gravatar.com has grown in popularity recently, it is used on stackoverflow and github.
You use an md5 hash of the email address to show the avatar.
Usage:
require 'digest/md5'
"http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest("Email Address".downcase)}?s=128"
Result:
http://www.gravatar.com/avatar/012f4052c6fb1a600a3e4f39e1f2439a?s=128

If you have their email address information, I'd recommend using Gravatar.
In essence, you create an md5 hash of their e-mail address (after converting it to all lower-case characters) and use that to build an image URL.
They have plugins for many blog engines, and it's very straight forward to implement without a plugin. See their Developer Resources page.

Related

How can I exclude certain parameters from Rails-generated links by default?

We use Rails' caches_action method along with Redis to implement caching of common pages.
It's a very dynamic site, so pages with differing URL params must remain distinct in the cache (e.g. ?page=1 vs ?page=2). But as long as the URLs match, the content can be cached and served to multiple visitors.
Since a very high portion of our traffic comes from Adwords ads, many of our users were arriving at our pages with a gclid param in the URL, which rendered the caching useless. I worked around this by excluding the gclid from the cache path used to identify unique URLs:
caches_action :search,
:expires_in => 10.minutes,
:cache_path => Proc.new { |c| c.params.except('gclid') }
This worked great for a while, until I realized that our Analytics data was off because the HTML we were caching included internal links with the gclid param from previous visits.
So the solution to this is pretty straightforward- every time I generate a link I need to manually exclude gclid, like:
link_to 'some page', params.except('gclid')
My question is: is there any way to automatically exclude gclid from all generated links? It would be really handy to not have to do this manually throughout the site.
Here's what I came up with. Turns out that link_to ends up calling url_for anyway, so that seems the best place to hook it in.
module ActionDispatch
module Routing
module UrlFor
alias :original_url_for :url_for
def url_for(options = {})
options = options.except(:gclid) if options.kind_of? Hash
original_url_for options
end
end
end
end
This answer by #jdc helper me get there, as did #MurifoX's comment.

Form processing with coffeescript

This seems like one of the most common coffeescript thigns to do and yet after extensive searching on stackoverflow and on google, I can't find any example of it.
For various reasons, I'm trying to have a standard rails form submit be handled by coffeescript. Imagine a form (generated by rails) for a User class with a name (string), age (integer), and birthday (date)--so the form has the field objects named.
$(document).ready ->
$('form').submit ->
name = form.name.value
age = form.age.value
birthday = form.birthday.value
User.create(:name => name, :age => age, :birthday => birthday)
One other catch, I want it to render a different page instead of /user/ and I'm unclear how to do that.
I've tried out the code but can't seem to capture the values. I figured there would be some example online but after many different search attempts I couldn't find anything.
Thanks for any help.
You might want to look into a client side framework like Backbone.js
This will let you modify javascript representations of your model and sync it with your rails app through a restful api.

How do I utilize user input without putting info into a Model?

This is an incredibly newbish question, but I can't seem to find the answer.
I'm building an app that utilizes external APIs heavily, and I'm fairly new to Rails, so it's still a little rough to get around. I can't, for the life of me, figure out how to accept user input and execute a function in my app without writing to a model.
For example, I just want to let a user type in a Twitter username and have it display on the page. I know how to make a form to cache the search in a model, but I can't figure out how to just... make a function happen on a page. I've been breaking my brains on this for several days now.
Please help? :/
You don't need a model to use Rails, but if you don't need ActiveRecord at all, you might benefit from a lighter framework like Sinatra. That doesn't answer your question, but it's worth thinking about if you really have no database requirement for your application.
It sounds like you're just trying to access non-resourceful user input, which is accessible in the controller via the params hash. So, assuming you have set up a valid route for the form action, you use your controller to extract GET or POST parameters.
For example:
# You define a non-resourceful route in routes.rb for your form action.
get 'twitternames/show'
# Form action directs user to GET the following route after filling in the form.
http://example.com/twitternames/show?user=foo
# The controller action extracts the data.
def show
#user = params[:user]
# render the view unless you tell rails to do something else
end
# show.html.erb
<%= #user %>
Creating the right route is the key. Once you've defined a route that can break a URL into the proper segments, the rest will fall into place.

Rails 3 CMS + blog wanted to fit existing site. Unobtrusive and Lightweight

I'd like to add a CMS and blog to a web app. One that won't get in the way. There's not a lot of content, but enough that we want non-devs to be able to revise pages and probably add and remove them too.
We have a substantial app that can't be touched by the CMS, not a site that we're migrating.
How have you dealt with this situation?
Would you advise:
Running two apps (a content app and the 'app' app)
Plugging in a light weight CMS
Rolling our own using gems/plugins for WYSIWYG
Details
We'll be adding a bug ticketing and support system later too. Probably built into the app.
We'd like the users of the app to be able to comment on pages and blog posts, file tickets, etc. all from their main account, so it seems to make sense to build it into our app, rather than as an extra app. Love to hear war stories on this.
Should be:
Unobtrusive (Shouldn't interfere with the existing app)
Must not mess with Devise, DeclarativeAuthorization, or Omniauth. We've got extensive user accounts, permissions, authentication mechanisms and groups setup. These must stay.
Lightweight (prefer something dev friendly than feature loaded)
Desired Features:
Basic WYSIWYG for content editors
Lets us handle accounts (with Devise)
and maybe even permissions (with DeclarativeAuthorization)
I've read this similar question, but the author seems willing to have something a bit more intrusive.
Simple Rails 3 CMS Gem/Plugin?
Options Found
Refinery seems to have a lot of features, but at a cursory look it needs a lot of control over what's going on: http://refinerycms.com/guides/attaching-refinery-cms-to-an-existing-rails-application It says it's modular, but it seems like there's a big chunk of non optional stuff in there.
Radiant seems a bit monolithic as well
http://groups.google.com/group/radiantcms/browse_thread/thread/b691cf9ab644a8b2
ComfortableMexicanSofa seems a bit closer to what I want: https://github.com/twg/comfortable-mexican-sofa
Adva-Cms has the right philosophy but appears to be dead. Adva-Cms2 isn't ready
http://adva-cms.org/
Governor seems good, but maybe a bit too young and lean
https://github.com/carpeliam/governor
Conclusion
So far rolling our own, or using ComfortableMexicanSofa seems like the bet, but I'd like your thoughts before I spend a few days messing around with it.
I am now rolling my own blog app and I am kind of newbie to Rails 3. Even like that, in 1 week i have a blog with tags, comments, authentication with omniauth, etc.. my advise is: roll your own. I was having the same doubt and looking for pre-made solutions and I decided to start it from zero and just look for plugins for anything i need.
It goes pretty fast if you know already some rails programming and you use the right plugins. This is what i used:
Omniauth to let users be able to autenticate with facebook, twitter etc.. and leave you comments.
rails_admin: it allows you to manage your blog by going to yourapp.com/admin. It uses devise to create an Admin user (you can specify a diferent model name than user to not to mix it with the users from omniauth or from your other app) and if you have the right models and associations between them you can from there create your posts, assign them tags or categories and also delete comments etc.. its all done in an easy way. For the Text Area that you use to introduce the content of your posts you can associate it with the ckeditor just by adding to the rails_admin initializer something like:
config.model Post do
edit do
field :body, :text do
ckeditor true
end
end
end
And with the ckeditor you can introduce pictures, attach videos, format text, and so on.
Use kaminari for pagination, or you can use will_paginate if you are more used to that.
Using the blueprint framework for styling with css you will save time and have a more standar styling.
Use few jquery lines to insert/delete comments graciously.
And that's all I can remember now. And if it shouldn't interfere with the main app, i would just assign a subdomain for it. So if you go to blog.myapp.com you access to the blog and if you go to myapp.com you access to the app. And you want users from the app to interact with the blog so you should use just one app and have this 2 subdomains pointing at differents parts of the same a app.. take a look at: rails 3 - one app, multiple domains, how implement a different 'root' route for one of the domains?
That's all i can think now! let me know if i can help you in anything else.
rails_admin: it allows you to manage your blog by going to yourapp.com/admin. It uses devise to create an Admin user (you can specify a diferent model name than user to not to mix it with the users from omniauth or from your other app) and if you have the right models and associations between them you can from there create your posts, assign them tags or categories and also delete comments etc.. its all done in an easy way. For the Text Area that you use to introduce the content of your posts you can associate it with the ckeditor just by adding to the rails_admin initializer something like:
config.model Post do
edit do
field :body, :text do
ckeditor true
end
end
end

rails3, params[:id] encryption

I trying to prevent url hacking, I passing an id to the url that the forms need, it works fine but if the user changes that value on the url it will send values to the wrong table.
<%= link_to '+ New Event',
{:controller =>'events', :action =>
'new', :company_id => company.id} %>
On the php world I used to encrypt that id ...how can I do this on rails3 or is there a better way ??
needless to say I sort of new to rails and I know a little bit of php
any help or suggestions will be greatly appreciated.
Even though this is an older question, it's a very worthwhile question. It is absolutely worthwhile to conceal the ID in the URL for, among other things, prevention of information disclosure.
For example, an application has a robust security model allowing users to only view resources to which they have rights. However, why should a user be able to look at the value of the ID in the URL and use it to deduce how many resources there are or, as the original questioner suggests, start trying to poke around with forced browsing.
The solution to this in rails turns out to be pretty simple. What I find works best is overriding to_param in the models, usually via a module in the lib directory and a before_filter in the application controller that decrypts the IDs.
For a walkthrough, have a look at this: http://www.youtube.com/watch?v=UW_s9ejrCsI
Rather than trying to encrypt or hide your company.id value, ask yourself what exactly it is that you want to prevent users from doing.
If you just want to prevent users from creating events associated with non-existant companies (by setting the id to a really high value for instance), then a simple
validates_presence_of :company
On the Event model would be fine.
If you only want users to be able to create events associated with companies that they work for, or have access for in some way, then you should create custom validations to verify that.
F