I have a controller create method like this.
def create
if user.save!
user.add_role = params[:user][:Role].parameterize('_').to_sym
if current_user.has_role?(:producer_or_beat_maker_or_composer)
#if(Student.find_by_email("#{current_user.email}")!= nil)
redirect_to(:controller => 'users',:action => new_registration_path(resource_name))
elsif current_user.role=="Producer"
redirect_to(:controller => 'users',:action => 'index')
end
end
end
when i click on user creation,i got an error like " undefined method `parameterize' for nil:NilClass". please help through this..
This one is quite simple params[:user][:Role] is nil. Perhaps you meant params[:user][:role].
Related
If the dispute save the change the order boolean false to true
but after create the rails log show
undefined method `update_attributes' for false:FalseClass
Someone know why?
order
has_one :dispute
dispute
belongs_to :order
def create
if current_user == #order.buyer
dispute = #order.dispute.nil? ? Dispute.new : #order.dispute
if dispute.save
#order = params[:dispute_status] == "1"
#order.update_attributes(:dispute_status => true)
redirect_to order_dispute_path(#order, #dispute)
flash[:success] = 'yess'
else
flash[:error] = 'Erro'
redirect_to :back
end
end
end
And if i remove the #order.update_attributes(:dispute_status => true)
the logs shows :
ActionController::RoutingError (No route matches {:action=>"create",
:controller=>"disputes", :order_id=>false, :format=>nil}
Well you have #order = params[:dispute_status] == 1. This returns a boolean. In your case, probably false. So now #order is a boolean false and you are trying to call update_attributes on it.
FalseClass.instance_methods.include?(:update_attributes) # false
ActiveRecord::Base.instance_methods.include?(:update_attributes) # true
your #order isn't a ActiveRecord::Base instance! clear?
I am using Devise in my Rails application. For some users I am getting this kind of error:
undefined method 'to' for nil:NilClass from this method
def http_auth_header?
Devise.mappings[scope].to.http_authenticatable && !request.xhr?
end
that is from Devise itself.
How can I resolve this?
I spent time to fix the same issue.
I did some custom stuff into the SessionController :
resource = warden.authenticate!(:scope => "user", :recall => "#{controller_path}#failure")
And in fact, scope MUST BE a symbol :
resource = warden.authenticate!(:scope => :user, :recall => "#{controller_path}#failure")
So I think you just provide a scope as a String instead of a symbol
Hope it helps ;)
I want to create a custom method, which I have defined in my questions_controller.rb file, as so:
def self.ping
#question = Question.first
#question.update(:amplify => #question.amplify + 1)
end
Now the problem is how do I call this method? Do I need to define a route first? How can I reference this from the console?
Thanks in advance.
I'd suggest to move it to your model:
question.rb
def self.ping
question = Question.first
question.update(:amplify => question.amplify + 1)
end
and define custom route, routes.rb
post '/ping' => 'questions#ping', as: 'ping'
questions_controller.rb
def ping
Question.ping
end
then you can reference it from console:
Question.ping
Please read this http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/
class Foo
def self.bar
puts 'class method'
end
def baz
puts 'instance method'
end
end
Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class
Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’
Instead of
question.update(:amplify => question.amplify + 1)
You can use
question.increment!(:amplify)
I'm getting this error
NameError in Browse#index
undefined local variable or method `browse_path'
Here's the code in view where the error is raised
<li><%= link_to "Browse", browse_path %></li>
Here's the controller
class BrowseController < ApplicationController
def index
#tags = tag.find(:all, :order => 'created_at DESC')
end
end
and finally my routes
get'/browse' => 'browse#index', :as => :index
anyone know what is causing this?
Try :as => :browse for the named route.
I am testing my project, it is about application_helper.rb that contain this method :
def delete_link table=""
if current_user.role.name=="Admin" || current_user.role.name=="Pemilik"
link_to 'Hapus', table, :confirm => 'Anda yakin?', :method => :delete
end
end
I want to testing that method, so I make this testing on my customers_controller_test.rb like this
require 'test_helper'
class CustomersControllerTest < ActionController::TestCase
include Devise::TestHelpers
include ApplicationHelper
setup do
#permission = Permission.create(:name=>"Customer")
#role = Role.create(:name=>"Admin")
#user = User.create(:name=>"Admin", :role_id => 1, :email => "email#email.email", :password => "123456")
#role_permission = RolePermission.create(:role_id=>#role.id, :permission_id=>#permission.id, :access=>2)
#customer = FactoryGirl.create(:customer)
sign_in #user
end
test "should link to delete" do
puts assert sign_in #user
current_user = #user
puts current_user.name
puts current_user.role.name
assert delete_link(customer_path(#customer.id))
puts "2"
end
end
But I got Undefined local variable or method 'current_user'. Any idea? Thx for ur advice :)
The current_user method of devise is defined on the application controller and as such is not available in helpers. Supply the user via a parameter of th helper method and you are set.