Omniauth can't find route - ruby-on-rails-3

I'm trying to implement Omniauth with my Rails 3 app. I followed the tutorial from the Railscast episod 205, but can't get it to work. When I call the '/auth/twitter' (it doesn't work with any provider) Rails complains it can't find the appropriate route (it tries to load my default route).
I added omniauth.rb under config/initializers/, put gem 'omniauth' in my Gemfile, and ran bundle install.
I'm not sure how to debug this problem nor what information to provide to help understand the problem.

After some research I found a solution. I added the following to my routes.rb file:
get 'auth/:provider' => 'authentications#passthru'
and to my authentications controller:
def passthru
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Not sure why I had to do that, but it's working...

Here were my steps to get it working:
1) Add omniauth to your Gemfile
2) Add omniauth strategy to your Gemfile (omniauth-linkedin for example)
3) Run bundle install
4) Add the omniauth initializer in config/initializers/omniauth.rb
5) Add the match auth/:provider/callback => users#omniauth route to routes.rb (point it to the controller/action where you'll handle the authentication response)
6) Build the action to handle the authentication response in the controller you referenced in step #4
7) Restart your web server
The default /auth/:provider route wasn't recognized for me until I restarted Apache.

You have to add 'omniauth-twitter' to your gemfile, and
:strategy_class => OmniAuth::Strategies::Twitter
to your omniauth.rb, at the end of the twitter provider.
Then it will recognize the /auth/twitter path.

Related

How to set sinatra-authentication to use erb instead of haml?

I'm trying to set up the sinatra-authentication gem in a simple sinatra app, and running into an issue where sinatra can't find the correct views. I understand that sinatra-authentication uses haml by default, but I'm using erb in this app.
This in mind, I found in the sinatra-authenticaiton docs that there is a setting which allows you to change the template engine, by adding the following to your app file:
configure do
set :template_engine, :erb # for example
end
I've added this to my app.rb file, and sinatra is still looking for the signup.haml when I try to hit the /signup route in my app.
A couple of notes:
I've included the gem in my Gemfile, and successfuly run a bundle install on my app.
source 'https://rubygems.org'
gem 'sinatra'
gem 'data_mapper'
gem 'pg'
gem 'dm-postgres-adapter'
gem 'sinatra-authentication'
I saw something in the documentation that suggested that I may need to specify the location of my view files, so I added the following to my configuration block.
set :sinatra_authentication_view_path, Pathname(__FILE__).dirname.expand_path + "views/"
**I think I've required the gem accurately in my app file by adding
require "sinatra-authentication"
use Rack::Session::Cookie, :secret => 'mys3cr3tk3y'
This gist is a current representation of my app.rb file in the root of my sinatra app. https://gist.github.com/rriggin/5378641#file-gistfile1-txt
Here is a screenshot of the error sinatra throws: http://cl.ly/image/0y041t0K3u3O
When I run the app locally, a 'dm-users' table is created in my local db as expected.
Is there another configuration setting that I'm missing in order to get sinatra-authentication to properly look for the erb templates rather than haml files. Any help would be greatly appreciated.
Thanks
The specs don't test that the template_engine setting works, and looking at the way the setting is called, I believe it's not correct, i.e.
send settings.template_engine, get_view_as_string("index.#{settings.template_engine}"), :layout => use_layout?
might better work as:
send app.settings.template_engine, get_view_as_string("index.#{app.settings.template_engine}"), :layout => use_layout?
that's what I reckon. If you fork the project, change the line and add it to your Gemfile and it works then consider writing a quick spec for it and you'll have improved the mainline of that project as well as fixed your problem.

Rails Active Admin resource problem

I recently have watched railscast 284 about active admin and wanted to implement it into my web app, however I am running into an issue when I add a resource. I get the following message every time I try to navigate to the created tab:
NameError in Admin::LoadsController#index
undefined local variable or method `per' for []:ActiveRecord::Relation
Rails.root: /Users/thomascioppettini/rails_projects/want-freight
Application Trace | Framework Trace | Full Trace
Request
Parameters:
{"order"=>"id_desc"}
Show session dump
Show env dump
Response
Headers:
None
The only thing I can think of that may affect the application is adding a recaptcha to devise, which active admin depends on.
For me, it looks like this is a pagination problem. What gem are you using? You should give as more details about your settup. Can you show us your resource file from admin directory? What version of rails and what ActiveAdmin are you using ?
If you are using the will_paginate gem, set the version to 3.0.pre2. I was using ~>3.0.pre2, which auto-updated to 3.0.2 when I ran a bundle update Reverting fixed the issue. If you're using Bundler, the line is this:
gem "will_paginate", "3.0.pre2"
I agree with Dawaid. It is a pagiantion error. Add "Kaminari" gem to you Gemfile. According to active admin docs, it is using kaminari for pagination.. will_paginate will also work for you as swilliams described...
As I understand active_admin doesn't support will_paginate anymore. But if you don't want to rewrite your pagination to Kaminari you can fix this problem with putting some code to initializers
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
alias_method :per, :per_page
alias_method :num_pages, :total_pages
end
end
end
end
module ActiveRecord
class Relation
alias_method :total_count, :count
end
end

omniauth w/ Twitter not working on rails 3.1.0.rc5

I have a Rails app running on 3.1.0.rc5, and I've setup omniauth for twitter exactly the same as seen here (https://github.com/sferik/sign-in-with-twitter) but it's still not working.
The problem is that after Twitter is redirecting back to my callback URL I have request.env["omniauth.auth"] is nil, rack.auth is nil too. I even went so far as to print out the entire request.env hash and didn't see anything like access_token or access_secret.
The only thing that doesn't look like my request token and secret is stored in action_dispatch.secret_token.
Any ideas on how to make this work?
Ok, so I got this working now. My Gemfile was:
gem "oa-oauth", :require => "omniauth/oauth"
gem "twitter"
I changed it to:
gem 'omniauth', :git => 'git://github.com/intridea/omniauth.git'
gem 'twitter'
gem 'json_pure'
gem 'json'
And everything started working. The request.env["omniauth.auth"] key was present etc. I had to add the json gem lines because of a uninitialized constant JSON::ParserError error I was getting. I think that is a bug in the release candidate.

map.root does not work in ruby on rails

I have deleted public/index.html.
Here is my config/routes.rb
Those all I have in that file
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.root :controller => "home"
I've also tried using map.root :controller => "home#index"
when I ran rake to check
$ rake routes
(in /var/www/atlantix)
/:controller/:action/:id
/:controller/:action/:id.:format
root / {:controller=>"home", :action=>"index"}
I also have:
app/views/home/index.html.erb
app/controllers/home_controller.rb
My issue:
I am a complete newbie of ROR.
I am using Apache and Ruby 1.8.
When I navigate to the http://localhost/myapp/,
I do not see the new home page I created.
Am I missnig something critical in my setup and configuration.
Please advise & help
Mmmm you seem to mix Rails 2 syntax. In Rails 3 you should write:
root :to => 'home#index'
Note: you should also delete the index.html from the public folder.
More information concerning routing can be found here.

OmniAuth Invalid Response - Updating to 1.9.2 not working

Hi
I know this question have been asked before but the answers there isn't working for me.
I still get the, when redirecting back to my site.
/auth/failure?message=invalid_response
I have ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.6.0] installed, using rails 3.0.7 and the required gems installed. I read on another thread that you should have pure_json added to the gemfile to make it work. But that didn't help me either.
I'm clueless... Thanks in advance
authenticationscontroller
def index
#authentications = current_user.authentications if current_user
end
def create
#render :text => request.env["omniauth.auth"].to_yaml
auth = request.env["omniauth.auth"]
current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
flash[:notice] = "Authentication successful."
redirect_to authentications_url
end
I was able to get this to work by specifying the following in my Gemfile
gem 'omniauth', '0.2.0'
Version 0.2.6 wouldn't work for me. I also updated by .rvmrc to rvm use 1.9.2#rails3. Oh, also make sure you're logged in - do note that in your code above you are assuming that current_user exists. See Ryan's Railscast part two for allowing user creation via Omniauth.
I've got a demo working here, but do note I'm doing authentication from scratch rather than using Devise.