NoMethodError in Controller#new - ruby-on-rails

Hi everyone Im working in my app and I run into this error
NoMethodError in GasStationsController#new
undefined method `gas_stations' for #<Class:0x12cf77510>
Rails.root: /Users/Users/Documents/myapps/app1
Application Trace | Framework Trace | Full Trace
app/controllers/gas_stations_controller.rb:4:in `new'
I dont understand if my routes are ok. I put my routes.rb and my GasStationsController
Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :users do
resources :gas_stations
end
....
user_gas_stations GET /users/:user_id/gas_stations(.:format) gas_stations#index
POST /users/:user_id/gas_stations(.:format) gas_stations#create
new_user_gas_station GET /users/:user_id/gas_stations/new(.:format) gas_stations#new
edit_user_gas_station GET /users/:user_id/gas_stations/:id/edit(.:format) gas_stations#edit
user_gas_station GET /users/:user_id/gas_stations/:id(.:format) gas_stations#show
PUT /users/:user_id/gas_stations/:id(.:format) gas_stations#update
DELETE /users/:user_id/gas_stations/:id(.:format) gas_stations#destroy
as you see the method is there "gas_stations"
I only have this on my controller
class GasStationsController < ApplicationController
def new
#user = User.find(params[:user_id])
#gas_station = #user.gas_stations.build
end
end
User model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
has_many :cars
validates_presence_of :email
end

As the error says, Rails has no idea what gas_stations are as it pertains to a user. You need to set that association:
class User ...
...
has_many :gas_stations
...

Related

Devise Current User Can't Get Nested Data to Show in View

I'm trying to make an example app to learn about namespace/scopes/modules
Normally I would user current_user helper but I have Client::Addresses nested in behind and would like to grab say the user's city and just display it on their edit page (devise registration/edit screen)
<%= current_user.?? %>
Using the line below. I also added inverse_of as my understanding it'll reverse the relationship as well but no avail.
<%= #user.addresses.cacity %>
I think this is pretty close #user.id works but adding the rest error reads. Looks like I also dealt with strong params just not sure. I'm doing this to practice namespacing:scopes/modules:
undefined method `cacity' for #<ActiveRecord::Associations::CollectionProxy []>
It would be great to do something like.
<%= current_user.addresses.cacity %>
Here's some additional information with what I got so far, let me know if additional info is needed.
routes.rb
Rails.application.routes.draw do
namespace :client do
resources :subscriptions
end
# Security Devise Setup
devise_for :admins
devise_for :users
# Main Pages
root 'website/page#index'
# Client Sections
resources :users do
scope module: "client" do
root :to => 'dashboard#index'
resources :addresses
end
end
namespace :admin do
root :to => 'panel#index'
end
end
user.rb
class User < ActiveRecord::Base
include Gravtastic
gravtastic
# Devise Settings
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
# Model Relationships
has_many :addresses, :inverse_of => :user, class_name: 'Client::Address'
end
client/address.rb
class Client::Address < ActiveRecord::Base
belongs_to :user, :inverse_of => :addresses
end
#user.addresses
is a collection, and you send a (instance) method cacity to a collection, which, as errors states, do not respond to it.
#user.addresses.first.cacity would work.
You could limit the relation to has_one:
has_one :address #...
Which will allow you to use the following:
#user.address.cacity

Undefined local variable or method 'rolify'

In my Rails project, I am trying to set up user authentication and user roles using the following Gems:
Devise
Cancan
Rolify
I had Devise up and working perfectly, but then when I went to set up CanCan and Rolify, I get this error in the browser:
NameError in Home#index
Showing C:/Sites/JustManage/app/views/devise/menu/_registration_items.html.erb where line #1 raised:
undefined local variable or method `rolify' for #<Class:0x3f48660>
Extracted source (around line #1):
1: <% if user_signed_in? %>
2:
3: <li>
4: <%= link_to('Edit registration', edit_user_registration_path) %>
Trace of template inclusion: app/views/layouts/application.html.erb
Rails.root: C:/Sites/JustManage
user.rb
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :subdomain, :first_name, :last_name
# Make sure certain attributes are unique
validates_uniqueness_of :email, :case_sensitive => false
end
role.rb
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
scopify
end
rolify.rb
Rolify.configure do |config|
config.use_dynamic_shortcuts
end
Let me know if you need me to post contents from any of my other files!
I had the same issue. Restarting my server fixed it!
'When in doubt, ctrl-c out!'
Is it safe to assume that you installed and subsequently required the rolify gem properly?...
Please verify that rolify is listed when you run gem list and that you have added require 'rolify' at the top of your user.rb file.

rails_admin Routing Error

I installed for my app RailsAdmin, but I can't go to the admin panel. After authorization I have routing error:
Routing Error
No route matches [GET] "/admin"
Try running rake routes for more information on available routes.
/config/routes.rb
1.mount RailsAdmin::Engine => '/adminpanel', :as => 'rails_admin'
/config/initializers/rails_admin.rb
1.RailsAdmin.config do |config|
2. config.authorize_with :cancan
3.end
/app/models/ability.rb
1.class Ability
2. include CanCan::Ability
3. def initialize(user)
4. if user.class.to_s == "Admin"
5. can :access, :rails_admin
6. can :manage, :all
7. elsif user.class.to_s == "User"
8. can :manage, :read
9. end
10. end
11.end
Also I have Admin model
/app/models/admin.rb
class Admin < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
But if I add in my routes.rb this:
mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
I have cyclic forwarding.
Please help me with this problem.
Try putting these two lines at the bottom part of your routes:
mount RailsAdmin::Engine => '/adminpanel', :as => 'rails_admin'
root :to => redirect('/adminpanel')

Rails undefined method `find_or_create_from_auth_hash'

My sessions_controller is as follows:
class SessionsController < ApplicationController
require 'omniauth-facebook'
require 'omniauth'
def create
#user = User.find_or_create_from_auth_hash(auth_hash)
self.current_user = #user
redirect_to '/'
end
protected
def auth_hash
request.env['omniauth.auth']
end
end
So... it's THERE isn't it?
Here's my users.rb file:
class User < ActiveRecord::Base
acts_as_voter
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
has_many :posts
has_many :tasks
end
And my routes file:
LiquidAdmin::Application.routes.draw do
devise_for :users
get '/auth/:provider/callback', to: 'sessions#create'
resource :sessions, :only => :create
get "home/bandwall"
get "home/index"
root :to => "home#index"
So where's the problem? "auth_hash" is clearly defined... The SessionsController is loading... so why is it complaining about no method for find_or_create_from_auth_hash?
In Rails 3.2, the method you'll want to use is called first_or_create:
User.where(auth_hash: auth_hash).first_or_create
According to the Rails 3.2 release notes:
Add first_or_create, first_or_create!, first_or_initialize methods to
Active Record. This is a better approach over the old
find_or_create_by dynamic methods because it's clearer which arguments
are used to find the record and which are used to create it.
Given this, the following is made possible through the query:
User.where(auth_hash: auth_hash).first_or_create(foo: 'bar') # Assuming no other User entries exist
#=> #<User id: 1, auth_hash: "your_auth_hash", foo: "bar">
User.where(auth_hash: auth_hash).first_or_create(foo: 'baz')
#=> #<User id: 1, auth_hash: "your_auth_hash", foo: "bar">
Given that this question is based on OmniAuth, I think a better answer would be:
#user = User.where(auth_hash).first_or_create
or if in Rails version greater than 4.1.14
#user = User.where(uid: auth_hash[:uid], provider: auth_hash[:provider]).first_or_create
auth_hash is not a field in the User model, but rather a Hash of fields that could be on the User model.

Devise Signout Routing Error

I'm using Devise 1.5.1 in a Rails 3.0.3 app. It works well, with one exception: The signout link gives me this error:
Routing Error
uninitialized constant UsersController
The link that leads to this is:
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
I haven't created an app/controllers/user_controller.rb file, but my understanding that this wasn't necessary when using Devise, correct?
In case it's relevant, my routes.rb file looks like:
Su::Application.routes.draw do
get "group/create"
devise_for :users
resources :users
resources :payers
resources :payments
resources :categories
resources :groups
match "adduser", :to => "groups#adduser"
root :to => "pages#home"
end
...and app/models/user.rb looks like:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :email, :password, :password_confirmation, :remember_me, :group_id
end
I have googled and searched on SO extensively, but to no avail. How should I troubleshoot something like this?
In your routes file, you have
devise_for :users
which serves for the routes for Devise, but
resources :users
is a generic CRUD route, which makes Rails to think that in your app, you have Users Controller, and that you are doing something with the Users model in your model.
The error tells that you don't have a Users Controller and that's true, but it's looking for it because of the route.
So, either delete the line or add a Users Controller if you want to do something with the Users model.
If anything is not clear, post it as a comment.

Resources