undefined local variable or method `params' for controller











up vote
0
down vote

favorite












I am trying to implement contact manager with rails. I am using RSpec for test-driven development.



"When I am looking at a single person’s page, I click an add link that takes me to the page where I enter the phone number. I click save, then I see the person and their updated information"



I am trying to validate this action.



In my controller class



describe "POST #create" do
context "with valid params" do
let(:alice) { Person.create(first_name: 'Alice', last_name: 'Smith') }
let(:valid_attributes) { {number: '555-1234', person_id: alice.id} }

it "creates a new PhoneNumber" do
expect {
post :create, params: {phone_number: valid_attributes}, session: valid_session
}.to change(PhoneNumber, :count).by(1)
end

it "redirects to the phone number's person" do
post :create, params: {:phone_number => valid_attributes}, session: valid_session
@phone_number = PhoneNumber.new(person_id: params[:person_id])
expect(response).to redirect_to(@phone_number.person)
end
end


I want to redirect to phone number's person but while getting phone number
It gives me an error undefined local variable or method params' for #<RSpec::ExampleGroups::PhoneNumbersController::POSTCreate::WithValidParams:0x00007fc89e276898>



I also have view test as follows



 it 'adds a new phone number' do
page.click_link('Add phone number')
page.fill_in('Number', with: '555-8888')
page.click_button('Create Phone number')
expect(current_path).to eq(person_path(person))
expect(page).to have_content('555-8888')
end


Right now this test is also failing because of this error :



expected: "/people/1"
got: "/phone_numbers"


How can I resolve these problems and redirect user as desired? Thanks in advance










share|improve this question






















  • The error is about what it says, you have neither local variable nor method in rspec where you execute @phone_number = PhoneNumber.new(person_id: params[:person_id]) statement.
    – Marek Lipka
    Nov 19 at 12:02















up vote
0
down vote

favorite












I am trying to implement contact manager with rails. I am using RSpec for test-driven development.



"When I am looking at a single person’s page, I click an add link that takes me to the page where I enter the phone number. I click save, then I see the person and their updated information"



I am trying to validate this action.



In my controller class



describe "POST #create" do
context "with valid params" do
let(:alice) { Person.create(first_name: 'Alice', last_name: 'Smith') }
let(:valid_attributes) { {number: '555-1234', person_id: alice.id} }

it "creates a new PhoneNumber" do
expect {
post :create, params: {phone_number: valid_attributes}, session: valid_session
}.to change(PhoneNumber, :count).by(1)
end

it "redirects to the phone number's person" do
post :create, params: {:phone_number => valid_attributes}, session: valid_session
@phone_number = PhoneNumber.new(person_id: params[:person_id])
expect(response).to redirect_to(@phone_number.person)
end
end


I want to redirect to phone number's person but while getting phone number
It gives me an error undefined local variable or method params' for #<RSpec::ExampleGroups::PhoneNumbersController::POSTCreate::WithValidParams:0x00007fc89e276898>



I also have view test as follows



 it 'adds a new phone number' do
page.click_link('Add phone number')
page.fill_in('Number', with: '555-8888')
page.click_button('Create Phone number')
expect(current_path).to eq(person_path(person))
expect(page).to have_content('555-8888')
end


Right now this test is also failing because of this error :



expected: "/people/1"
got: "/phone_numbers"


How can I resolve these problems and redirect user as desired? Thanks in advance










share|improve this question






















  • The error is about what it says, you have neither local variable nor method in rspec where you execute @phone_number = PhoneNumber.new(person_id: params[:person_id]) statement.
    – Marek Lipka
    Nov 19 at 12:02













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to implement contact manager with rails. I am using RSpec for test-driven development.



"When I am looking at a single person’s page, I click an add link that takes me to the page where I enter the phone number. I click save, then I see the person and their updated information"



I am trying to validate this action.



In my controller class



describe "POST #create" do
context "with valid params" do
let(:alice) { Person.create(first_name: 'Alice', last_name: 'Smith') }
let(:valid_attributes) { {number: '555-1234', person_id: alice.id} }

it "creates a new PhoneNumber" do
expect {
post :create, params: {phone_number: valid_attributes}, session: valid_session
}.to change(PhoneNumber, :count).by(1)
end

it "redirects to the phone number's person" do
post :create, params: {:phone_number => valid_attributes}, session: valid_session
@phone_number = PhoneNumber.new(person_id: params[:person_id])
expect(response).to redirect_to(@phone_number.person)
end
end


I want to redirect to phone number's person but while getting phone number
It gives me an error undefined local variable or method params' for #<RSpec::ExampleGroups::PhoneNumbersController::POSTCreate::WithValidParams:0x00007fc89e276898>



I also have view test as follows



 it 'adds a new phone number' do
page.click_link('Add phone number')
page.fill_in('Number', with: '555-8888')
page.click_button('Create Phone number')
expect(current_path).to eq(person_path(person))
expect(page).to have_content('555-8888')
end


Right now this test is also failing because of this error :



expected: "/people/1"
got: "/phone_numbers"


How can I resolve these problems and redirect user as desired? Thanks in advance










share|improve this question













I am trying to implement contact manager with rails. I am using RSpec for test-driven development.



"When I am looking at a single person’s page, I click an add link that takes me to the page where I enter the phone number. I click save, then I see the person and their updated information"



I am trying to validate this action.



In my controller class



describe "POST #create" do
context "with valid params" do
let(:alice) { Person.create(first_name: 'Alice', last_name: 'Smith') }
let(:valid_attributes) { {number: '555-1234', person_id: alice.id} }

it "creates a new PhoneNumber" do
expect {
post :create, params: {phone_number: valid_attributes}, session: valid_session
}.to change(PhoneNumber, :count).by(1)
end

it "redirects to the phone number's person" do
post :create, params: {:phone_number => valid_attributes}, session: valid_session
@phone_number = PhoneNumber.new(person_id: params[:person_id])
expect(response).to redirect_to(@phone_number.person)
end
end


I want to redirect to phone number's person but while getting phone number
It gives me an error undefined local variable or method params' for #<RSpec::ExampleGroups::PhoneNumbersController::POSTCreate::WithValidParams:0x00007fc89e276898>



I also have view test as follows



 it 'adds a new phone number' do
page.click_link('Add phone number')
page.fill_in('Number', with: '555-8888')
page.click_button('Create Phone number')
expect(current_path).to eq(person_path(person))
expect(page).to have_content('555-8888')
end


Right now this test is also failing because of this error :



expected: "/people/1"
got: "/phone_numbers"


How can I resolve these problems and redirect user as desired? Thanks in advance







ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 12:00









wolf

518




518












  • The error is about what it says, you have neither local variable nor method in rspec where you execute @phone_number = PhoneNumber.new(person_id: params[:person_id]) statement.
    – Marek Lipka
    Nov 19 at 12:02


















  • The error is about what it says, you have neither local variable nor method in rspec where you execute @phone_number = PhoneNumber.new(person_id: params[:person_id]) statement.
    – Marek Lipka
    Nov 19 at 12:02
















The error is about what it says, you have neither local variable nor method in rspec where you execute @phone_number = PhoneNumber.new(person_id: params[:person_id]) statement.
– Marek Lipka
Nov 19 at 12:02




The error is about what it says, you have neither local variable nor method in rspec where you execute @phone_number = PhoneNumber.new(person_id: params[:person_id]) statement.
– Marek Lipka
Nov 19 at 12:02












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










There's no such thing as params in your test.



Instead of...



@phone_number = PhoneNumber.new(person_id: params[:person_id])


...you could do...



@phone_number = PhoneNumber.new(person_id: valid_attributes[:person_id])


But since you're just testing that you redirected to the person, you could remove the above line completely and do



expect(response).to redirect_to(alice)





share|improve this answer





















  • Hey, thanks for your answer. I tried the code as you pointed out however, I got an error as follows: Expected "http://test.host/people/1" to be ==="http://test.host/phone_numbers/1".
    – wolf
    Nov 19 at 12:29










  • So that means your controller is not redirecting to the person, it's redirecting to the phone number record. Is that ok? If so, you need to change the text to expect(response).to redirect_to(assigns(:phone_number)) which is the way you'd access the controllers's instance variable... assigns(:phone_number) in a test is the same variable as @phone_number in the controller.
    – SteveTurczyn
    Nov 19 at 13:02










  • If it's NOT ok (i.e. the controller should redirect to the person record) then congrats, your test found a problem in your code!
    – SteveTurczyn
    Nov 19 at 13:03










  • Thanks, I got the idea!
    – wolf
    Nov 19 at 13:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374204%2fundefined-local-variable-or-method-params-for-controller%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










There's no such thing as params in your test.



Instead of...



@phone_number = PhoneNumber.new(person_id: params[:person_id])


...you could do...



@phone_number = PhoneNumber.new(person_id: valid_attributes[:person_id])


But since you're just testing that you redirected to the person, you could remove the above line completely and do



expect(response).to redirect_to(alice)





share|improve this answer





















  • Hey, thanks for your answer. I tried the code as you pointed out however, I got an error as follows: Expected "http://test.host/people/1" to be ==="http://test.host/phone_numbers/1".
    – wolf
    Nov 19 at 12:29










  • So that means your controller is not redirecting to the person, it's redirecting to the phone number record. Is that ok? If so, you need to change the text to expect(response).to redirect_to(assigns(:phone_number)) which is the way you'd access the controllers's instance variable... assigns(:phone_number) in a test is the same variable as @phone_number in the controller.
    – SteveTurczyn
    Nov 19 at 13:02










  • If it's NOT ok (i.e. the controller should redirect to the person record) then congrats, your test found a problem in your code!
    – SteveTurczyn
    Nov 19 at 13:03










  • Thanks, I got the idea!
    – wolf
    Nov 19 at 13:18















up vote
3
down vote



accepted










There's no such thing as params in your test.



Instead of...



@phone_number = PhoneNumber.new(person_id: params[:person_id])


...you could do...



@phone_number = PhoneNumber.new(person_id: valid_attributes[:person_id])


But since you're just testing that you redirected to the person, you could remove the above line completely and do



expect(response).to redirect_to(alice)





share|improve this answer





















  • Hey, thanks for your answer. I tried the code as you pointed out however, I got an error as follows: Expected "http://test.host/people/1" to be ==="http://test.host/phone_numbers/1".
    – wolf
    Nov 19 at 12:29










  • So that means your controller is not redirecting to the person, it's redirecting to the phone number record. Is that ok? If so, you need to change the text to expect(response).to redirect_to(assigns(:phone_number)) which is the way you'd access the controllers's instance variable... assigns(:phone_number) in a test is the same variable as @phone_number in the controller.
    – SteveTurczyn
    Nov 19 at 13:02










  • If it's NOT ok (i.e. the controller should redirect to the person record) then congrats, your test found a problem in your code!
    – SteveTurczyn
    Nov 19 at 13:03










  • Thanks, I got the idea!
    – wolf
    Nov 19 at 13:18













up vote
3
down vote



accepted







up vote
3
down vote



accepted






There's no such thing as params in your test.



Instead of...



@phone_number = PhoneNumber.new(person_id: params[:person_id])


...you could do...



@phone_number = PhoneNumber.new(person_id: valid_attributes[:person_id])


But since you're just testing that you redirected to the person, you could remove the above line completely and do



expect(response).to redirect_to(alice)





share|improve this answer












There's no such thing as params in your test.



Instead of...



@phone_number = PhoneNumber.new(person_id: params[:person_id])


...you could do...



@phone_number = PhoneNumber.new(person_id: valid_attributes[:person_id])


But since you're just testing that you redirected to the person, you could remove the above line completely and do



expect(response).to redirect_to(alice)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 12:21









SteveTurczyn

25.7k42738




25.7k42738












  • Hey, thanks for your answer. I tried the code as you pointed out however, I got an error as follows: Expected "http://test.host/people/1" to be ==="http://test.host/phone_numbers/1".
    – wolf
    Nov 19 at 12:29










  • So that means your controller is not redirecting to the person, it's redirecting to the phone number record. Is that ok? If so, you need to change the text to expect(response).to redirect_to(assigns(:phone_number)) which is the way you'd access the controllers's instance variable... assigns(:phone_number) in a test is the same variable as @phone_number in the controller.
    – SteveTurczyn
    Nov 19 at 13:02










  • If it's NOT ok (i.e. the controller should redirect to the person record) then congrats, your test found a problem in your code!
    – SteveTurczyn
    Nov 19 at 13:03










  • Thanks, I got the idea!
    – wolf
    Nov 19 at 13:18


















  • Hey, thanks for your answer. I tried the code as you pointed out however, I got an error as follows: Expected "http://test.host/people/1" to be ==="http://test.host/phone_numbers/1".
    – wolf
    Nov 19 at 12:29










  • So that means your controller is not redirecting to the person, it's redirecting to the phone number record. Is that ok? If so, you need to change the text to expect(response).to redirect_to(assigns(:phone_number)) which is the way you'd access the controllers's instance variable... assigns(:phone_number) in a test is the same variable as @phone_number in the controller.
    – SteveTurczyn
    Nov 19 at 13:02










  • If it's NOT ok (i.e. the controller should redirect to the person record) then congrats, your test found a problem in your code!
    – SteveTurczyn
    Nov 19 at 13:03










  • Thanks, I got the idea!
    – wolf
    Nov 19 at 13:18
















Hey, thanks for your answer. I tried the code as you pointed out however, I got an error as follows: Expected "http://test.host/people/1" to be ==="http://test.host/phone_numbers/1".
– wolf
Nov 19 at 12:29




Hey, thanks for your answer. I tried the code as you pointed out however, I got an error as follows: Expected "http://test.host/people/1" to be ==="http://test.host/phone_numbers/1".
– wolf
Nov 19 at 12:29












So that means your controller is not redirecting to the person, it's redirecting to the phone number record. Is that ok? If so, you need to change the text to expect(response).to redirect_to(assigns(:phone_number)) which is the way you'd access the controllers's instance variable... assigns(:phone_number) in a test is the same variable as @phone_number in the controller.
– SteveTurczyn
Nov 19 at 13:02




So that means your controller is not redirecting to the person, it's redirecting to the phone number record. Is that ok? If so, you need to change the text to expect(response).to redirect_to(assigns(:phone_number)) which is the way you'd access the controllers's instance variable... assigns(:phone_number) in a test is the same variable as @phone_number in the controller.
– SteveTurczyn
Nov 19 at 13:02












If it's NOT ok (i.e. the controller should redirect to the person record) then congrats, your test found a problem in your code!
– SteveTurczyn
Nov 19 at 13:03




If it's NOT ok (i.e. the controller should redirect to the person record) then congrats, your test found a problem in your code!
– SteveTurczyn
Nov 19 at 13:03












Thanks, I got the idea!
– wolf
Nov 19 at 13:18




Thanks, I got the idea!
– wolf
Nov 19 at 13:18


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374204%2fundefined-local-variable-or-method-params-for-controller%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]