Skip to content

Instantly share code, notes, and snippets.

@chintanparikh
Created October 15, 2013 06:11
Show Gist options
  • Save chintanparikh/6987243 to your computer and use it in GitHub Desktop.
Save chintanparikh/6987243 to your computer and use it in GitHub Desktop.
def create
errors = []
@user = User.find(params[:user_id])
if @user.nil?
errors << "User not found"
end
@stock = Stock.find_by_ticker(params[:ticker])
if @stock.nil?
@stock = Stock.new(ticker: params[:ticker])
unless @stock.save
errors.concat(@stock.errors.full_messages)
end
end
unless errors.nil? and @stock.persisted?
errors << "Error saving stock"
end
unless errors.nil?
@user.stocks << @stock
json = {
status: 200,
symbol: @stock.ticker,
path: Rails.application.routes.url_helpers.stock_path(@stock),
errors: "nil"
}
else
json = {
status: 500,
symbol: "nil",
path: "nil",
errors: errors
}
end
render json: json.to_json
end
@Ruxton
Copy link

Ruxton commented Oct 15, 2013

Brother Chintan, we taught you better than this :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment