On 7 June 2011 17:12, Mauro <[email protected]> wrote:
> On 7 June 2011 15:07, Colin Law <[email protected]> wrote:
>> On 7 June 2011 15:53, Mauro <[email protected]> wrote:
>>> On 7 June 2011 14:48, Colin Law <[email protected]> wrote:
>>>> On 7 June 2011 15:45, Mauro <[email protected]> wrote:
>>>>> I'm using devise and in application_controller I have before_filter
>>>>> :authenticate!
>>>>> In sector_controller I have:
>>>>>
>>>>> skip_filter :authenticate_user!, :only => [:index, 
>>>>> :search_categories_by_sector]
>>>>>
>>>>> When I run rspec it says: undefined method authenticate!, even for
>>>>> index action.
>>>>> But I' set skip_filter for index action.
>>>>> Why rspec needs authenticate for index?
>>>>
>>>> Because you have specified skip for :authenticate_user! not :authenticate! 
>>>> ?
>>>
>>> I've make a mistake writing, in application_controller I have:
>>> before_filter :authenticate_user!
>>
>> In that case, as you are getting an error 'undefined method
>> authenticate!' that is nothing to do with the filter.
>>
>> It is no good re-typing what you think you have when asking questions,
>> it is vital that we know *exactly* what you have.
>> I suggest that you copy and paste the relevant code out of
>> application_controller.rb and sector_controller.rb and also copy and
>> paste the test that is failing and the error.
>
> That' right.
> application_controller.rb
>
> class ApplicationController < ActionController::Base
>  before_filter :authenticate_user!
>  rescue_from DeviseLdapAuthenticatable::LdapException do |exception|
>    render :text => exception, :status => 500
>  end
>
> suppliers_controller.rb
>
> class SuppliersController < InheritedResources::Base
>  skip_filter :authenticate_user!, :only => [:index,
> :search_categories_by_sector]
>  before_filter :load_sectors_and_categories, :except =>
> :search_categories_by_sector
>
>  respond_to :js
>
>  def index
>    set_title("Albo Fornitori - Ricerca", "Albo Fornitori")
>    @search = Supplier.search(params[:search])
>    @suppliers = @search.page(params[:page]).per(Settings.suppliers_per_page)
>  end
>
>  private
>  def search_categories_by_sector
>    unless params[:sector_id].blank?
>      @categories = Category.find_all_by_sector_id(params[:sector_id])
>      render :layout => false
>    else
>      @categories = Category.all
>      render :layout => false
>    end
>  end
>
>  def set_title(title1, title2)
>    @titleBox1 = title1
>    @titleBox2 = title2
>  end
>
>  def load_sectors_and_categories
>    @sectors = Sector.all
>    @categories = Category.all
>  end
> end
>
> suppliers_controller_spec.rb
>
> describe SuppliersController do
>
>  # This should return the minimal set of attributes required to create a valid
>  # Supplier. As you add validations to Supplier, be sure to
>  # update the return value of this method accordingly.
>  before :each do
>    @category = Category.make!
>  end
>
>  def valid_attributes
>    { :company_name => 'Supplier-1', :vat_number => '12345678901',
> :address => 'addres-1', :city => 'city-1',
>     :prov => 'prov-1', :zip_code => '12345', :inps => 'inps-1',
> :inail => 'inail-1', :email => '[email protected]',
>     :categories => [@category] }
>  end
>
>  def mock_supplier(stubs={})
>    @mock_supplier ||= mock_model(Supplier, stubs).as_null_object
>  end
>
>  describe "GET index" do
>    it "assigns all suppliers as @suppliers" do
>      sectors = Sector.all
>      categories = Category.all
>      supplier = Supplier.create! valid_attributes
>      get :index
>      assigns(:suppliers).should eq([supplier])
>      assigns(:sectors).should eq(sectors)
>      assigns(:categories).should eq(categories)
>    end
>  end
>
> the error is:
>
> 1) SuppliersController GET index assigns all suppliers as @suppliers
>     Failure/Error: get :index
>     NoMethodError:
>       undefined method `authenticate!' for nil:NilClass

Don't you get a trace showing where the error occurred?

I haven't used Devise though so someone else may be better able to
help now that we have determined exactly what is happening.  Could it
be due to the fact that you are using InheritedResources?

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to