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
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
add a comment |
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
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
The error is about what it says, you have neither local variable nor method inrspec
where you execute@phone_number = PhoneNumber.new(person_id: params[:person_id])
statement.
– Marek Lipka
Nov 19 at 12:02
add a comment |
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
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
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
ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
asked Nov 19 at 12:00
wolf
538
538
The error is about what it says, you have neither local variable nor method inrspec
where you execute@phone_number = PhoneNumber.new(person_id: params[:person_id])
statement.
– Marek Lipka
Nov 19 at 12:02
add a comment |
The error is about what it says, you have neither local variable nor method inrspec
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
add a comment |
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)
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 toexpect(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
add a comment |
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)
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 toexpect(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
add a comment |
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)
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 toexpect(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
add a comment |
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)
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)
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 toexpect(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
add a comment |
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 toexpect(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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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