rails: text not rendering - ruby-on-rails

class MyTestController < ApplicationController
def index
render_text "hello world"
end
end
This is what I am getting when I go to http://127.0.0.1:3000/My_Test/ :
NoMethodError in My testController#index
undefined method `render_text' for #
RAILS_ROOT: C:/rails/rails_apps/cookbook
Application Trace | Framework Trace | Full Trace
app/controllers/my_test_controller.rb:5:in `index'
Request
Parameters:
None
Show session dump
Response Headers:
{"cookie"=>[], "Cache-Control"=>"no-cache"}
please keep in mind i am following this tutorial:
http://oreilly.com/pub/a/ruby/archive/rails.html?page=2

That tutorial is very old and out of date. The render_text method no longer exists. Try the following instead:
def index
render :text => "hello world"
end

Related

Keep Encountering 500 Error When Using Stripe Webhooks with Ruby on Rails 7

I'm developing an application where users can pay a one time fee to submit a screenplay. The current flow looks like this:
User fills out form
Upon submitting form, the user is redirected to Stripe Checkout
Stripe webhook listens for session.checkout.completed, once that happens the is_paid field is updated to TRUE.
Every thing except the webhooks are working fine. I can submit the form, be redirect to stripe, and make a payment. The issue that I'm encountering is that whenever I use Stripe CLI (stripe listen --forward-to localhost:3000/webhooks) and go through the checkout process on my local environment, I receive the following errors in the Stripe CLI tab:
payment_intent.created
[500] POST http://localhost:3000/webhooks
customer.created
[500] POST http://localhost:3000/webhooks
payment_intent.succeeded
[500] POST http://localhost:3000/webhooks
charge.succeeded
[500] POST http://localhost:3000/webhooks
checkout.session.completed
[500] POST http://localhost:3000/webhooks
And the following errors in my Rails Server tab:
Completed 500 Internal Server Error in 0ms (ActiveRecord: 0.0ms | Allocations: 454)
ArgumentError (wrong number of arguments (given 0, expected 1+)):
app/controllers/webhooks_controller.rb:8:in `create'
The above says there's an error on line eight which would be endpoint_secret = Rails.application.credentials.dig[:stripe, :webhook_secret] but I don't see what's wrong with this. I've checked my credentials and they're definitely correct (I've copy and pasted them numerous times, made sure they were test keys, compared them letter by letter etc).
When I use stripe trigger checkout.session.completed I get the same errors.
If anyone has an idea of where I'm going wrong it would be apppreciated!
Here's what my screenplay_controller looks like:
def create
#screenplay = current_user.screenplays.new(screenplay_params)
if #screenplay.save
session = Stripe::Checkout::Session.create({
line_items: [{
price: "removed for privacy",
quantity: 1,
}],
mode: "payment",
metadata: { screenplay_id: #screenplay.id },
customer_email: current_user.email,
success_url: root_url,
cancel_url: root_url,
})
redirect_to session.url, allow_other_host: true
else
render :new, status: :unprocessable_entity
end
end
This is my webhooks_controller:
class WebhooksController < ApplicationController
skip_before_action :verify_authenticity_token
def create
event = nil
sig_header = request.env["HTTP_STRIPE_SIGNATURE"]
payload = request.body.read
endpoint_secret = Rails.application.credentials.dig[:stripe, :webhook_secret]
begin
event = Stripe::Webhook.construct_event(
sig_header, payload, endpoint_secret
)
rescue JSON::ParserError => e
# Invalid payload
head 400
return
rescue Stripe::SignatureVerificationError => e
# Invalid signature
head 400
return
end
case event.type
when "checkout.session.completed"
session = event.data.object
screenplay = Screenplay.find_by(id: session.metadata.screenplay_id)
screenplay.update(is_paid: true)
end
end
end
This is what my routes look like:
Rails.application.routes.draw do
root "static_pages#home"
devise_for :users
resources :screenplays
resources :webhooks, only: [:create]
end
And this is what my Stripe initialiser looks like:
Stripe.api_key = Rails.application.credentials.dig(:stripe, :secret_key)
You need to replace square brackets with normal brackets
a = { b: :c }
a.dig(:b)
# Returns :c
a.dig[:b]
# ArgumentError: wrong number of arguments (given 0, expected 1+)

Server replies with default debug response instead of raised exception response

Within an engine w/ an API, when querying the API an exception is thrown, but the server response is not using the response specified, and instead rendering a default debug response (in production).
I can confirm that the exception is caught by the controller:
Started GET "/api_v2/admin/submissions?system_id=123&spt_id=123" for
127.0.0.1 at 2019-03-15 10:04:37 -0400
Processing by ApiV2::Admin::SubmissionsController#show as JSON
Parameters: {"system_id"=>"123", "spt_id"=>"123"}
[3, 12] in /....../emp_api_v2/app/controllers/emp_api_v2/application_controller.rb
3:
4: before_action :doorkeeper_authorize!
5:
6: rescue_from ::ActiveRecord::RecordNotFound do |e|
7: byebug
=> 8: response(:standard_error, 500, "Hello World")
9: end
10:
11: def doorkeeper_unauthorized_render_options(error: nil)
12: { json: { message: "Not authorized" }, status: 403 }
(byebug) cont
Completed 500 Internal Server Error in 5220ms (ActiveRecord: 0.0ms)
ActiveRecord::RecordNotFound - Couldn't find Emp::System with 'id'=123:
The server is expected to respond with 500 Server Error not the debug error stacktrace.
Why are there two responses, even though the controller catches the exception and runs a response method.
NOTE: This happens in dev and prod ! Server responds with 500 first (my catch response) but then with a stacktrace and 404 (As this is the source of the error and correct exception). It breaks my tests as the past response was 500. I was not able to revert my server to the old behavior by: reinstalling ruby, reinstalling rails + all gems + rolling back all changes throughout the repo. This behavior seems to be externally set by a ENV variable or something else.
I'd be grateful for any insight.
Edit: The (api) app controller looks like this:
module ApiV2
class ApplicationController < ActionController::API
before_action :doorkeeper_authorize!
rescue_from ::ActiveRecord::RecordNotFound do |e|
response(:standard_error, 500, "Hello World")
end
def doorkeeper_unauthorized_render_options(error: nil)
{ json: { message: "Not authorized" }, status: 403 }
end
end
end
Edit 2: I was able to get the correct response by wrapping the call in a rescue block. That code will result in a lot of begin/rescue blocks though as each of them has a specific error message.
begin
system = Emp::System.find(sys_id)
rescue
render json: { status: 500, content: "Specific Error msg" } and return
end
Originally i had a method as follows:
def handle_exception(message, &block)
begin
block.call
rescue Exception => e
render json: { message: message }, status: 500 and return
end
end
This will result in double render error as it's not returning from the top-level method but from the block.

Getting rails error when using Spaceship::Tunes

In a rails app I am running:
54 def itunes_all_apps
55 begin
56 Spaceship::Tunes.login(params[:itunes_username], params[:itunes_password])
57 apps = Spaceship::Tunes::Application.all
58 render json: apps.to_json, status: 200
59 rescue => e
60 render json: {error: e}.to_json, status: 500
61 end
62 end
It returns a status 500 error with no other information every time.
However, if I change this around slightly, for example getting teams (note, from Spaceship, not Spaceship::Tunes) this works fine:
def itunes_all_apps
begin
spaceship = Spaceship.login(params[:itunes_username], params[:itunes_password])
teams = spaceship.teams
render json: teams.to_json, status: 200
rescue => e
render json: {error: e}.to_json, status: 500
end
end
I'm not using any fast file or or config or anything. Just passing in a username and password via an api call and trying to get a response back. I'm new to rails so it may be my implementation of the Spaceship examples provided.
Using spaceship 0.36.1 gem (the latest)
I've pored through the docs to no avail. Grasping for any leads on what I'm doing wrong.
http://www.rubydoc.info/gems/spaceship/Spaceship/Tunes
https://github.com/fastlane/fastlane/blob/master/spaceship/docs/iTunesConnect.md
Someone suggested I run these two commands in irb, which I did, and they worked perfect!
Spaceship::Tunes.login('myAppleId', 'myPassword')
Spaceship::Tunes::Application.all
So it's not an iTunes account problem or credentials problem (because it works in irb), routes problem (because I ran both rails methods above with same route), or params problem (because I ran both rails methods above with same param names).
I really appreciate any suggestions. Thanks.
Edit:
Commenting out begin, rescue, and rending the error, the stack trace is as follows:
2016-10-24T17:47:34.974650+00:00 app[web.1]: Started POST "/api/v1/users/13/itunes_all_apps" for 162.237.102.13 at 2016-10-24 17:47:34 +0000
2016-10-24T17:47:34.977478+00:00 app[web.1]: Processing by Api::V1::UsersController#itunes_all_apps as JSON
2016-10-24T17:47:34.977521+00:00 app[web.1]: Parameters: {"itunes_username"=>"myCorrectUsername", "itunes_password"=>"[FILTERED]", "team_id"=>"myCorrectTeamId", "id"=>"13", "user"=>{}}
2016-10-24T17:47:35.629629+00:00 heroku[router]: at=info method=POST path="/api/v1/users/13/itunes_all_apps" host=myHerokuApp.herokuapp.com request_id=002d906d-354e-4633-8b54-71aa5181e3a7 fwd="161.237.102.13" dyno=web.1 connect=2ms service=657ms status=500 bytes=259
2016-10-24T17:47:35.619597+00:00 app[web.1]: Completed 500 Internal Server Error in 642ms (ActiveRecord: 0.0ms)
2016-10-24T17:47:35.620430+00:00 app[web.1]:
2016-10-24T17:47:35.620432+00:00 app[web.1]: IOError (not opened for reading):
2016-10-24T17:47:35.620434+00:00 app[web.1]:
2016-10-24T17:47:35.620433+00:00 app[web.1]: app/controllers/api/v1/users_controller.rb:58:in `itunes_all_apps'
It seems that Spaceship::Fastlane::Application does not implement as_json method and the default as_json touches some IO object, which cannot be represented as json.
My suggestion would be to create JSON serializer. You could use active_model-serializer, but if you do not want to create a dependency just for one object, then you can create your own serializer.
class SpaceshipApplicationSerializer
attr_reader :spaceship_applications
def initialize(spaceship_applications)
#spaceship_applications = spaceship_applications
end
def as_json(options = {})
spaceship_applications.each_with_object([]) do |spaceship_application, memo|
memo << object_as_json(spaceship_application)
end
end
def object_as_json(object)
attributes.each_with_object({}) do |attribute, memo|
memo[attribute] = object.send(attribute)
end
end
def attributes
[
:apple_id,
:name,
:vendor_id,
:bundle_id,
:last_modified,
:issues_count,
:app_icon_preview_url
]
end
end
# In your controller
def itunes_all_apps
begin
Spaceship::Tunes.login(params[:itunes_username], params[:itunes_password])
apps = Spaceship::Tunes::Application.all
render json: SpaceshipApplicationSerializer.new(apps).to_json, status: 200
rescue => e
render json: {error: e}.to_json, status: 500
end
end
EDIT:
Yes, the classes return an array, but the actual objects in array don't play nicely with json. It's hard to say if the problem is with the library - on one hand Spaceship::Tunes::Application not returning a proper json representation is a missing feature, but if the to_json raises an exception (a method the class responds to) - then I would say that is a bug.
Creating your own serializer to build json representation the way you want it - is a common pattern.

wrong status line: "TTP/1.1 302 Found"

I'm doing an HTTP request from a Rails action like so:
class TeamController < ApplicationController
def test
_uri = 'http://v.youku.com/player/getPlayList/VideoIDS/XNjQyNjg3ODg0_ev_5'
_html_response = nil
open(_uri) do |http|
_html_response = http.read
end
render text: _html_response
end
end
But I get the error:
wrong status line: "TTP/1.1 302 Found"
But I used the same code in a simple Ruby file and I got the response without any errors. What am I doing wrong?
thanks,I find the reason,that's because my ubuntu vm is forbided to visit this site.sheet!

rescue Nokogiri error

I've a simple script that looks at Twitter username and gets me the location. But some of the username doesn't exist and I get error:
/usr/lib/ruby/1.8/open-uri.rb:277:in `open_http': 404 Not Found (OpenURI::HTTPError)
I've tried to rescue it, but I can't to make it work. Can anyone help? Thanks
a = []
my_file = File.new("location.txt", 'a+')
File.open('address.txt', 'r') do |f|
while line = f.gets
url = "http://twitter.com/#{line}"
doc = Nokogiri::HTML(open(url, 'User-Agent' => 'ruby'))
doc.css("#side #profile").each do |loc|
my_file.puts "http://twitter.com/#{line} #{loc.at_css(".adr").text}"
puts line
end
end
end
I also need help rescuing another error:
twitter.rb:14: undefined method `text' for nil:NilClass (NoMethodError)
Thanks.
Double quotes inside the other double quotes! Use single quotes for the call to at_css():
my_file.puts "http://twitter.com/#{line} #{loc.at_css('.adr').text}"
Turns out a simple rescue StandardError did the trick.

Resources