简体   繁体   中英

REST API POST request built in Ruby on Rails showing some Null values

Getting some null values from user_id, created_at and updated_at tables after making a post request to a RESTFUL API built in Ruby. Most values in the white list params are sent but it "rolls back" due to some null values not being sent(my assumption).

( https://s7.postimg.org/9m5v1m2u3/Screen_Shot_2017-11-20_at_12.01.58_PM.png ) ![null values shown]

( https://s7.postimg.org/h5yy3ngl7/Rollback.png ) ![rollsback error shown in terminal]

Here is the API controller:

class Api::V1::GigsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]

skip_before_action :verify_authenticity_token
    def index
        @gig_items = Gig.all
        render json: @gig_items
    end


    def create
        @gig_item = Gig.create(gig_params)
        render json: @gig_item
    end



    def show
        @gig_item = Gig.find(params[:id])
        render json: @gig_item
    end

private

    def gig_params
         params.require(:gig).permit(:title, :category, :description, :price, :main_image, :thumb_image, :status, category_ids: [])
    end
end

Here is the post request call using an action creator and axios

const ROOT_URL= "http://localhost:5000/api/v1/gigs"

export function createGig(values){
const request = axios.post(`${ROOT_URL}`, values);

return {
    type: CREATE_GIG,
    payload: request
}

}

and using redux-form to handle the post request action to send and validate the parameters:

onSubmit(values){
    this.props.createGig(values);
}


render(){
    const { handleSubmit } = this.props;
    return(
        <form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
            <Field
                label="Title of Gig"
                name="title"
                component={this.renderField}
             />
             <Field
                label="Categories"
                name="categories"
                component={this.renderField}
             />
             <Field
                label="Description"
                name="description"
                component={this.renderField}
             />
              <Field
                label="Price"
                name="price"
                component={this.renderIntegerField}
             />
              <Field
                label="Status"
                name="status"
                component={this.renderIntegerField}
             />
             <Field
                label="Main Image"
                name="main_image"
                component={this.renderField}
             />
             <Field
                label="Thumb Image"
                name="thumb_image"
                component={this.renderField}
             /> 

             <button type="submit" className="btn btn-success">Submit</button>
             {" "}
             <Link to={'/gigs'}><button type="submit" className="btn btn-danger">Cancel</button></Link>

        </form>
        )
}
}
 export default reduxForm({
validate,
form: 'GigsNewForm'
})(

Any thoughts as to why when doing a POST request on my application it yields null values to the created_at, updated_at and user_id while the other ones seems to be fine.

When dealing with HTTP API's, you need to pay attention to HTTP status codes to know what type of response you're getting.

In this case, your record isn't saving (maybe due to validation errors?) which is why you're getting null values. You're probably getting a 422 status code with that response, which means something definitely went wrong.

I was able to figured it out by revising the API controller

Originally I had:

 def create
    @gig_item = Gig.create(gig_params)
    render json: @gig_item
 end

Then I realized I was using devise for user authentication therefore I added:

def create
        @gig_item = **current_user**.gigs.create(gig_params)
        render json: @gig_item
end

I was able to successfully passed the parameters that were yielding null.

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