Cameron Bass Cameron Bass - 9 months ago 25
Ruby Question

undefined method `permit' for "1":String

I getting this error when running RSpec and I have no idea what it wants from me? I don't know why it's saying

permit
is undefined? and what is this
1:string
thing happening? If anyone has ideas I'd love to know. Here is my error and code for clarity.

CONTROLLER:

class SubscribersController < ApplicationController
def index
@subscriber = Subscriber.all
end

def new
@subscriber = Subscriber.new
end

def create
@subscriber = Subscriber.create(subscriber_params)
if @subscriber.save
flash[:notice] = "Subscriber Has Been Successfully Created"
redirect_to new_subscriber_path(:subscriber)
else
render "new"
end
end

def search
@subscriber = Subscriber.new
end

def visit
@subscriber = Subscriber.find_by_phone_number(params[:phone_number])
if @subscriber
@subscriber.visit += 1
@subscriber.save
flash[:notice] = "thanks"
redirect_to subscribers_search_path(:subscriber)
else
render "search"
end
end


private

def subscriber_params
params.require(:subscriber).permit(:first_name, :last_name, :email, :phone_number)
end
end


enter image description here

TEST:

require "rails_helper"

describe SubscribersController do
include Devise::TestHelpers

let(:user) { FactoryGirl.create(:user)}


it "creates a new subscriber" do
sign_in(user)
subscriber = FactoryGirl.create(:subscriber)
expect { post :create, :subscriber => subscriber }.to change(Subscriber, :count).by(1)
end
end


SUBSCIRBER OBJECT

enter image description here
Let me know if you need to see anything else.

Answer

I believe the problem here is that you're supplying a ruby object rather than a hash of values when you're submitting your request in the spec. Try using the following instead:

subscriber = FactoryGirl.attributes_for(:subscriber)
expect { post :create, :subscriber => subscriber }.to ...