795:POST + Ruby on Rails中的意外令牌

时间:2016-02-02 12:48:17

标签: ruby-on-rails ruby json carrierwave

我正在试图把邮递员召集一个POST来叫我的班级这些是我的模特和控制器

#yell.rb
class Yell < ActiveRecord::Base
  include ActionView::Helpers::DateHelper

  belongs_to :user, inverse_of: :yells
  has_many :negotiations, inverse_of: :yell, :dependent => :delete_all
  has_one :trade_offer, inverse_of: :yell, :dependent => :destroy
  has_and_belongs_to_many :categories, inverse_of: :yell, :dependent => :delete_all

  def as_json(options={})
    super(:only => [:id, :yell_type, :status, :payment_type],
          :include => {
              :trade_offer => {:only => [:id, :title, :description, :price],
                               :include => [:photos => {:only => [:id],
                                                        :methods => :url}]
              },
              :categories => {:only => [:id, :name]},
              :user => {:only => [:id, :name, :avatar]}
          },
          :methods => [ :times_ago]
    )
  end

  def times_ago
    time_ago_in_words(self.created_at)
  end

  def url
    self.trade_offer.photos.url.url
  end

end

#yells_controller.rb
# POST /yells.json
def create
  if (YELLTYPES[0][YELLTYPE_OFFER].include? params[:yell_type]) || (params[:yell_type].equal? YELLTYPE_REQUEST_BUY)

    @yell = Yell.new(yell_params)
    @user = User.find_by_id(params[:user_id]) #just have it because it has not current_user
    @yell.user = @user

    if @yell.save
      #creating an offer on a yell
      @trade_offer = TradeOffer.new(trade_offer_params)
      @trade_offer.yell = @yell
      if @trade_offer.save
        @yell.trade_offer = @trade_offer
      else
        @yell.destroy
        render json: {status: 3, message:"offer not create", data: nil}, status: :unprocessable_entity
        return
      end

      #categories relating to yell if the category does not exist it will be created
      Array(params[:yell][:categories]).each do |rel|
        @category = Category.find_by_name(rel[:name])
        if @category
          @categories_yells = CategoriesYell.new(category_id: @category.id, yell_id: @yell.id)
          if @categories_yells.save
            @yell.categories.build(id: @category.id, name: rel[:name])#only creates the relationship
          else
            @yell.destroy
            render json: {status: 4, message:"relationship category not create", data: nil}, status: :unprocessable_entity
          end
        else
          @yell.categories.create(name: rel[:name]) #creates the relationship and category
        end
      end

      #creating photos related to an offer
      Array(params[:yell][:trade_offer][:photos]).each do |rel|
        @photo = Photo.new(url: rel, trade_offer_id: @trade_offer.id)
        if !@photo.save
          @yell.destroy
          render json: {status: 5, message:"photo not upload", data: nil}, status: :unprocessable_entity
          return
        end
      end

      render json: {status: 0, message:"succes", data: @yell}, status: :created
    else
      render json: {status: 6, message:"yell not create", data: nil}, status: :unprocessable_entity
    end
  else
    render json: {status: 7, message:"type not permitted", data: {types_permitted: YELLTYPES}}, status: :unprocessable_entity
  end

end

这个想法是一个系列和一系列图像的尖叫。

时,这个调用在身体一步到位/ json时工作得很好。但是有了这个,我无法上传图像。

the error that returning the postman is that this image

1 个答案:

答案 0 :(得分:1)

我打算拨打标题content-type = aplication/json,只需要接听它,然后添加到我的路线defaults: {format: 'json'}