简体   繁体   中英

How to get id from post route in Sinatra

I have a factory that is creating a student. All of the required fields are being filled in and created in the factory. When I post a new student, I am never getting that student, just the one created in the factory? How Can I get the id of the new student from the POST?

students.rb

FactoryGirl.define do
factory :student do
sequence(:id)
sequence(:first_name){|n| "Tom"}
sequence(:last_name) {|n| "Dick"}
sequence(:password) {|n| "test"}
sequence(:email){ |n| "person#{n}@example.com" }
school
end
end

student_spec.rb

require File.dirname(__FILE__) + '/spec_helper'

describe 'admin' do
 before :all do
@student=FactoryGirl.create(:student)
end
it 'should save student information' do
params={
    post:{
        first_name: 'Abraham',
        last_name: 'Lincoln',
        address: '1400 Pennsylvania Ave.',
        city: 'Washington',
        state: 'D.C.',
        zip: '11111',
        email: 'alincoln@whitehouse.gov',
        password: 'I cant tell a lie',
        major: 'Law',
        level: 'Graduate',
        employment: 'President',
        participation: 'Yes',
        participation_research: 'No',
        what_doing_now: 'Watching a Play',
        ethnicity: 'White',
        eth_other: 'none',
        education_level: 'Doctorate',
        hometown: 'Springfield, IL',
        twitter_handle: '@TheRealAbe',
        facebook_url: 'therealabe',
        prior_education: 'None',
        prior_AS: 'None',
        prior_BS: 'None',
        prior_MS: 'None',
        prior_Other: 'None',
        awards_honors: 'Ended Civil War',
        scholarships: 'Full',
        other_inbre_funding: 'yes',
        deleted: 'false'
    }
}
post '/admin/students/add/', params, {'rack.session'=>{student_id:@student.id, admin_authorized:true}}
updated_student = Student.get(@student.id)

expect(updated_student.first_name).to eql('Abraham')
#expect(updated_student.last_name).to eql('Dick')
#expect(updated_student.address).to eql('1400 Pennsylvania Ave.')
end

end

I can't know for certain without seeing the route, but it seems to me that you are calling POST twice, once in the route, and once within the params hash. However, without seeing your routes, I can't be sure if that's an issue.

Another thought comes from reading your spec. Are you attempting to create a new student(POST) or are you attempting to edit the student? If you are attempting to edit an existing student, this should probably be referring to a separate route, PUT or a PATCH by RESTful conventions. As was mentioned above, a POST route does not need an id, precisely because it is creating a new student, which is where it will create the id, and the id is set by the database, not manually.

a reference to the above: http://guides.rubyonrails.org/v2.3.11/routing.html

I hope this at least points in the right direction. :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM