Created
October 15, 2013 06:11
-
-
Save chintanparikh/6987243 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Brother Chintan, we taught you better than this :P