undefined method `permit' for "#<flight:0xdb2b88c>"String</flight:0xdb2b88c>



  • Why is this mistake coming?

    Failure/Error:
           params.require(:flight).permit(:airline, :flight_number, :number_with_date,
             :flight_type, :code_shared, :departure_time, :arrival_time, :origin_apt,
             :destination_apt, :origin_id, :destination_id, :aircraft, :capacity_weight,
             :capacity_volume, :available, :update_status)
    
     NoMethodError:
       undefined method `permit' for "#&lt;Flight:0xdb2b88c&gt;":String
     # ./app/controllers/api/v1/flights_controller.rb:50:in `flight_params'
     # ./app/controllers/api/v1/flights_controller.rb:30:in `create'
     in &lt;module:Runner&gt;'
     # ./spec/api/v1/flights_spec.rb:137:in `block (5 levels) in &lt;top (required)&gt;'
    

    Spectare itself

    describe 'POST /create' do
    let(:access_token) { create(:access_token) }
    let(:user) { User.find(access_token.resource_owner_id) }
    let(:flight) { create(:flight) }

    context 'authorized' do
      context 'with valid attributes' do
        before do
          user.update!(role: 'robot')
          post '/api/v1/flights', format: :json, access_token: access_token.token, flight: flight
        end
    
        it 'responses with 201' do
          expect(response.status).to eq 201
        end
    
        it 'creates new flight' do
          expect { response }.to change(Flight, :count).by 1
        end
      end
    end
    

    end

    Controller

    class Api::V1::FlightsController < Api::V1::BaseController
    skip_before_action :doorkeeper_authorize!, only: [:index, :search, :show]
    before_action :set_flight, only: [:show, :update, :destroy]
    authorize_resource

    def create
    @flight = Flight.create(flight_params)
    respond_with :api, :v1, @flight
    end

    private

    def set_flight
    @flight = Flight.find(params[:id])
    end

    def flight_params
    params.require(:flight).permit(:airline, :flight_number, :number_with_date,
    :flight_type, :code_shared, :departure_time, :arrival_time, :origin_apt,
    :destination_apt, :origin_id, :destination_id, :aircraft, :capacity_weight,
    :capacity_volume, :available, :update_status)
    end
    end



  • Good news, the controller's right. Technically. The wrong are just speculation.

    The thing is, you're trying to pass the model's object in parameters. Flight♪ Without knowing how to pass it on HTTP, the testophreimvork did .to_s (...right? You should check it out, and I finally got it. params[:flight] line "#<Flight:0хШотатам>"

    It's easy to fix, just think what you're doing. At the user level, ActiveRecord does not yet exist, sets of parameters should be made (whether form, JSON or something). For example, by attributes_for from FactoryGirl, which collects xashmap, and is already known as the HTTP.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2