Rspec | Missing id for create controller

62 views
Skip to first unread message

Deepak Sharma

unread,
Jun 25, 2016, 5:09:08 AM6/25/16
to rubyonra...@googlegroups.com
I am getting Id missing nil error in my create controller spec. Yet I have passed ID stuff in it still facing same everything. Here my create spec and controller file

Create Spec

  describe 'POST :create' do
    context 'with valid data' do
      let(:valid_data) { FactoryGirl.attributes_for(:student) }
      it 'redirect to show page' do
        post :create, student: valid_data
        expect(response).to redirect_to(student_path(assigns[:student]))
      end
    end
  end

Student Controller 


  def create
    @student = current_user.students.build(student_params)

    respond_to do |format|
      if @student.save
        format.html { redirect_to @student }
        format.json { render :show, status: :created, location: @student }
      else
        format.html { render :new }
        format.json { render json: @student.errors, status: :unprocessable_entity }
      end
    end
  end

private

  def student_params
    params.require(:student).permit(:Student_Prefix, :First_Name, :Middle_Name, :Last_Name, :Father_Prefix, :Father_Name, :Father_Middle_Name, :Father_Last_Name, :Mother_Prefix, :Mother_Name, :Mother_Middle_Name, :Mother_Last_Name,  :user_id)
  end

Error

1) StudentsController POST :create with valid data redirect to show page
   Failure/Error: expect(response).to redirect_to(student_path(assigns[:student]))
   
   ActionController::UrlGenerationError:
     No route matches {:action=>"show", :controller=>"students", :id=>nil} missing required keys: [:id]
   # ./spec/controllers/students_controller_spec.rb:38:in `block (4 levels) in <top (required)>'
   # -e:1:in `<main>'


Where I am going wrong!

--
Cheers!

Deepak Kumar Sharma
Guru Nanak Dev Engineering College
India!

Blog: http://deekysharma.wordpress.com

tamouse pontiki

unread,
Jun 25, 2016, 9:32:10 AM6/25/16
to rubyonra...@googlegroups.com
On Sat, Jun 25, 2016 at 4:08 AM, Deepak Sharma <deeky....@gmail.com> wrote:
I am getting Id missing nil error in my create controller spec. Yet I have passed ID stuff in it still facing same everything. Here my create spec and controller file

Create Spec

  describe 'POST :create' do
    context 'with valid data' do
      let(:valid_data) { FactoryGirl.attributes_for(:student) }
      it 'redirect to show page' do
        post :create, student: valid_data

Looks like right here:
 
        expect(response).to redirect_to(student_path(assigns[:student]))

Should be `assigns(:student)`.

 

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CALDHwN5oJqMmp7d_%3DjGwt%3DNXa%2BuLqL5xfYZh0kknrD5_X94xkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Deepak Sharma

unread,
Jun 26, 2016, 2:22:10 PM6/26/16
to rubyonra...@googlegroups.com
On Sat, Jun 25, 2016 at 7:01 PM, tamouse pontiki
<tamous...@gmail.com> wrote:
>
> Looks like right here:
>
>>
>> expect(response).to redirect_to(student_path(assigns[:student]))
>
>
> Should be `assigns(:student)`.

I tried above option but getting same error.

Deepak Sharma

unread,
Jun 28, 2016, 2:21:34 PM6/28/16
to rubyonra...@googlegroups.com
On Sun, Jun 26, 2016 at 11:51 PM, Deepak Sharma <deeky....@gmail.com> wrote:
> I tried above option but getting same error.

Any help on this. I tried so many attempts to solve it but no luck.

Thank You!

Hassan Schroeder

unread,
Jun 29, 2016, 10:32:39 AM6/29/16
to rubyonrails-talk
On Tue, Jun 28, 2016 at 11:21 AM, Deepak Sharma <deeky....@gmail.com> wrote:
>
> Any help on this. I tried so many attempts to solve it but no luck.

Providing a link to a reproducible test case would be a good start...

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote
Reply all
Reply to author
Forward
0 new messages