795: POST + Ruby Rails 中的意外标记
795: unexpected token at in POST + Ruby on Rails
我正在尝试让邮递员 POST 打电话来喊我的 class 这些是我的模型和控制器
#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,但我无法上传图像。
我本来打算打电话给 header content-type = aplication/json
,只是不得不接受它,并添加到我的路线 defaults: {format: 'json'}
我正在尝试让邮递员 POST 打电话来喊我的 class 这些是我的模型和控制器
#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,但我无法上传图像。
我本来打算打电话给 header content-type = aplication/json
,只是不得不接受它,并添加到我的路线 defaults: {format: 'json'}