Route not found when using Devise's devise_scope - ruby

I am using devise for authentication in my app. My routes file look like this:
devise_scope :user do
get "/password_reset" => "devise/passwords#new", :as => "new_user_password"
post "/password_reset/complete" => "devise/passwords#create", :as => "user_password"
get "/password_renew" => "devise/passwords#edit", :as => "edit_user_password"
put "/password_renew/complete" => "devise/passwords#update", :as => "password"
end
The full routes.rb file: http://hastebin.com/vofaluhera.coffee
running rake routes show that these route work well:
new_user_password GET /password_reset(.:format) devise/passwords#new
user_password POST /password_reset/complete(.:format) devise/passwords#create
edit_user_password GET /password_renew(.:format) devise/passwords#edit
password PUT /password_renew/complete(.:format) devise/passwords#update
However when I visit route /password_reset it rases this error:
Could not find devise mapping for path "/password_reset"
Also when I switch to devise_for it work fine but with a deprecation warning and it adds more unwanted routes. So basically, I want to stick with devise_scope
Please help me fix this errors.
Edit:
It turns out that I need to add before using devise_scope
devise_for :users, :skip => [:passwords]

Related

Using model attribute as :filename when using paperclip gem

I'm getting an error when attempting to change the :filename of my paperclip attachment to equal an attribute on the class I'm attaching the paperclip file to.
When I use "#{self.company_name}" it errors out. Apparently in this scope, "self" is not Company. When I wrote this line I assumed that self is the instance of Company that I'm uploading this attachment to. Any idea how I can fix this? The Paperclip docs say to use ":filename" but I'd like to use the value of Company.company_name instead.
class Company < ActiveRecord::Base
include AliasAttrs
has_attached_file :company_logo, {
:storage => :ftp,
:path => "/logos/#{self.company_name}",
:url => FTP_CONFIG[:access_host]+"logos/:filename",
:ftp_servers => [
{
:host => FTP_CONFIG[:host],
:user => FTP_CONFIG[:user],
:password => FTP_CONFIG[:pass],
:port => 21 # optional, 21 by default
}
]
}
end
Update
I tried using the advice found in this post: https://robots.thoughtbot.com/paperclip-tips-and-updates
But now I am getting the following error when starting my server:
undefined method `interpolations' for Paperclip::Attachment:Class (NoMethodError)
It looks like the syntax for interpolations has changed. Updated it and it worked. Add the following to your model or create a paperclip.rb file in config/initializers
Paperclip.interpolates :company_name do |attachment, style|
attachment.instance.company_name
end

Getting information from Facebook OmniAuth Authentication Hash

I am a beginner with Ruby and Rails but I managed to use OmniAuth for authentication via Facebook. Everything works fine, I am able to create users and they are able to login with Facebook.
The problem is, I would like to take some of the user data (such as email, profile photo, etc.) and save it.
Going through the README (https://github.com/mkdynamic/omniauth-facebook), I managed to find:
Here's an example Authentication Hash available in request.env['omniauth.auth']:
{
:provider => 'facebook',
:uid => '1234567',
:info => {
:nickname => 'jbloggs',
:email => 'joe#bloggs.com',
:name => 'Joe Bloggs',
:first_name => 'Joe',
:last_name => 'Bloggs',
:image => 'http://graph.facebook.com/1234567/picture?type=square',
:urls => { :Facebook => 'http://www.facebook.com/jbloggs' },
:location => 'Palo Alto, California',
:verified => true
}
}
I tried to do more searching on the Authentication Hash and got this which lists some of the information that can be fetched: https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema
The thing is, I asked for certain permissions. The question is, how do I know what sort of information Facebook is sending? Unfortunately, saying the info is in request.env['omniauth.auth'] does me not much good. How do I fetch the information from here?
I am a real beginner going through the Rails tutorial (http://ruby.railstutorial.org/) but trying to create my own app by trial and error.
request.env['omniauth.auth'] will give you a hash. Check the Ruby docs for what you can do with a hash.
http://ruby-doc.org/core-1.9.3/Hash.html
For any elements that are not always going to be there you can just check for blank? (a Rails convenience method), e.g.
omniauth = request.env['omniauth.auth']
unless omniauth['info']['email'].blank?
send_spam omniauth['info']['email'] # ;)
end
You would probably find the following screencast useful if you haven't seen it already:
http://railscasts.com/episodes/235-omniauth-part-1

error to customize error 404 rails 3.1 with hight_voltage gem

Hi guys I remove /page from high_voltage gem with this answer.
Remove page/ of High Voltage for statics page rails
I have in my routes for high_voltage this:
match '/:id' => 'high_voltage/pages#show', :as => :static, :via => :get
For maintenance page 404 in rails 3.1 I follow this fix http://techoctave.com/c7/posts/36-rails-3-0-rescue-from-routing-error-solution with errors_controller.rb with the next code:
def routing
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Then I add to routes.rb the next code for maintenance page 404 in rails
match '*a', :to => 'errors#routing'
The problem is that if I put in browser www.mydomain.com/sdfs dont working 404 system error and show No such page: sdfs
but however if I put www.mydomain.com/a_controller/action/sdfs yes working fine the fix for error 404 page.
I think that problem is my routes.rb
I solved this problem by extending the HighVoltage::PagesController and modifying the error catching:
class PagesController < HighVoltage::PagesController
rescue_from ActionView::MissingTemplate do |exception|
render_not_found
end
end
In my case though, my 404 function resides in my application controller so that it can easily be called from any location. If you make the same change, you will also need to update your route:
match '/:id' => 'pages#show', :as => :static, :via => :get
Thank you kevinthopson for mi this dont working fine :(.
I have in my routes.rb:
match '/:id' => 'pages#show', :as => :static, :via => :get
I have added this in pages_controller.rb
class PagesController < HighVoltage::PagesController
rescue_from ActionView::MissingTemplate do |exception|
render_not_found
end
end
I have added this code to aplication_controller.rb:
def render_not_found
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
Working fine if you put now:
localhost:3000/dfadsfadsf
The problem now is that if you put for example that routes in navigation bar:
localhost:3000/users_or_static_page/asdfadfadfa
Dont working for me :(.

Rails paperclip plugin

I am a newbie. I am trying to upload an image through paperclip. The url and path code is working but the style option is not working. This is my code:
class User < ActiveRecord::Base
has_attached_file :image, :styles => { :small => "150x150>" },
:url => "/assets/users/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/users/:id/:style/:basename.:extension"
end
When I use the style it doesn't work. If I remove the style option it does work. Please help me out!
Not sure if this will work but try :style_:basename.:extension

Rails 3 - Restricting formats for action in resource routes

I have a resource defined in my routes.
resources :categories
And I have the following in my Category controller:
def show
#category = Category.find(params[:id])
respond_to do |format|
format.json { render :json => #category }
format.xml { render :xml => #category }
end
end
The controller action works fine for json and xml. However I do NOT want the controller to respond to html format requests. How can I only allow json and xml? This should only happen in the show action.
What is the best way to achieve this?
Also is there any good tips for DRYing up the respond_to block?
Thanks for your help.
I found that this seemed to work (thanks to #Pan for pointing me in the right direction):
resources :categories, :except => [:show]
resources :categories, :only => [:show], :defaults => { :format => 'json' }
The above seems to force the router into serving a format-less request, to the show action, as json by default.
You must wrap those routes in a scope if you want to restrict them to a specific format (e.g. html or json). Constraints unfortunately don't work as expected in this case.
This is an example of such a block...
scope :format => true, :constraints => { :format => 'json' } do
get '/bar' => "bar#index_with_json"
end
More information can be found here: https://github.com/rails/rails/issues/5548
This answer is copied from my previous answer here..
Rails Routes - Limiting the available formats for a resource
You could do the following in your routes.rb file to make sure that only the show action is constrained to json or xml:
resources :categories, :except => [:show]
resources :categories, :only => [:show], :constraints => {:format => /(json|xml)/}
If this doesn't work you could try explicitly matching the action:
resources :categories, :except => [:show]
match 'categories/:id.:format' => 'categories#show', :constraints => {:format => /(json|xml)/}
constraints was not working for POST requests and then I tried defaults it works for all.
namespace :api, :defaults => { :format => 'json' } do
namespace :v1 do
resources :users do
collection do
get 'profile'
end
end
post 'signup' => 'users#create'
post 'login' => 'user_sessions#create'
end
end
I was using Rails 4.2.7

Resources