Api请求不在开发模式下工作

时间:2015-07-02 18:34:18

标签: ruby-on-rails curl

我有rails应用程序,我有事件控制器

def create          
            @event = Event.create(event_params) 
            @event.location_id = Location.find_by_address(params[:address]).id
            @event.owner = current_user

            if params[:clot_id].present?
                @event.clot_id = params[:clot_id]
            end         
            render json: @event, status: 201, location: @event              
        end


def event_params
            params.require(:event).permit(:venue, :clot_id, :contact, :cost, :description, :name, :tag_list,:location_id,   
                    compound_started_at_attributes: [:date, :time],
                    compound_ended_at_attributes: [:date, :time], location_attributes: [:id, :address])
        end 

当我尝试在localhost上发出curl请求时,它的工作正常

curl -i -H 'Authorization: Token token'="NXNW8kxzZ2z8czHF6" -X POST -d 'event[venue]=indore&event[contact]=9977966105&event[cost]=52&event[name]=forencis&event[compound_started_at_attributes][date]= 2015-08-20&event[compound_started_at_attributes][time]= 09.05&event[compound_ended_at_attributes][date]= 2015-08-25&event[compound_ended_at_attributes][time]= 10.05&event[location_id]=10&event[clot_id]=26' http://localhost:3000/api/events

卷曲到服务器

curl -i -H 'Authorization: Token token'="oKSMrppRXEiRSH61okn5" -X POST -d 'event[venue]=indore&event[contact]=9977966105&event[cost]=52&event[name]=forencis&event[compound_started_at_attributes][date]= 2015-08-20&event[compound_started_at_attributes][time]= 09.05&event[compound_ended_at_attributes][date]= 2015-08-25&event[compound_ended_at_attributes][time]= 10.05&event[location][address]=indore&event[clot_id]=1' 104.236.233.57/api/events

但是当我试图向生产服务器发出相同的curl请求时,它会在production.log中给出错误

F, [2015-07-02T14:20:47.041280 #29752] FATAL -- :
NoMethodError (undefined method `id' for nil:NilClass):
  app/controllers/api/events_controller.rb:34:in `create

即行中的错误

  

@ event.location_id = Location.find_by_address(params [:address])。id

我在生产数据库中检查了数据库中是否存在location_id = 10,并且在发送api请求之前我还检查了数据库中的其他值。

请帮我解决错误。

1 个答案:

答案 0 :(得分:1)

您正在使用:address参数,在查询字符串中,地址位于位置参数:event[location][address]=indore

不应该是event[address]=indore吗?

您的代码假定始终'位置'将被发现。您不应该假设并向Event类添加一些验证(或对您有意义的其他解决方法):

@event.location_id = Location.find_by_address(params[:address]).try(:id)

Event班:

class Event
  validates_presence_of :location_id
end