Trying to get rails debugger work. I did following:
gem install debugger
Added in Gemfile
gem "debugger", "~> 1.2.0"
bundle install - no error
Now I put debugger in one of my controllers
def show
require 'debugger'; debugger
#user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #user }
end
end
When I point my browser, I get following error on browser
LoadError in UsersController#show
cannot load such file -- debugger
I am using ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux] and Rails 3.2.6 with Phusion passenger
Any idea what is wrong?
Remove require statement and start rails server with debugger option
rails server --debugger
Not exactly sure how you want to use the debugger gem, but it is great for creating stop-points in active code. In Rails 3, you only need in your routes.rb:
gem 'debugger'
It doesn't have any effect in production and to have it stop your code at any point, simply put 'debugger' and your server console will give you access to that point in your code. At that point, to check the status of variables, you need to access IRB, so just type in 'irb'. In your 'show' action, you can check the value of your params and whether #user would be populated.
def show
debugger
#user = User.find(params[:id])
...
Related
Tested throughly in dev and Ransack works as described. However in production I get the following error:
NoMethodError (undefined method `ransack' for #<Class:0x000055725c12ba78>)
Here's the controller method where I'm calling the ransack method
def index
respond_to do |format|
format.html do
#q = ProductionItem.ransack(params[:q])
#production_items = #q.result.includes([:product]).includes([:line_item]).includes([:order]).includes([:dealer]).page(params[:page]).per(20)
end
end
end
grep 'ransack' Gemfile produces: gem 'ransack'
And just to make sure I ran bundle install and then restarted the entire VPS to make sure I hadn't forgot anything.
I've never used Ransack before. Am I supposed to use it differently in production?
After updating to Rails 5.0, I'm getting the following error:
"AbstractController::DoubleRenderError in RegistrationsController#create
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "reditect_to(...) and return"."
This is my code, which worked before my update:
def create
# save record
if params[:stuff].nil?
respond_to do |format|
format.js
end
else
redirect_to root_path
end
end
I've tried a lot of different syntaxes, for example:
redirect_to(root_path) and return
redirect_to(root_path)
return
return and redirect_to(root_path)
return redirect_to(root_path)
But everything returns the same error. Anyone know the proper syntax?
You probably have render or redirect where you're showing the # save-record.
Try this:
Add gem byebug to your Gemfile if you don't already have it installed, run bundle to install it, and restart Rails
Add byebug to the start of your create method
Invoke create from the browser or command-line, and step through it with n and s to step into other functions. You'll probably notice render or redirect is being called twice
You can use performed? to test for, or debug, a double render/redirect.
I am trying to install wicked_pdf in order to generate pre-filled contracts between 2 users on my Rails application.
I feel like I have installed wicked_pdf properly, but I get an "ActionController::UnknownFormat" error.
What I did :
# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
With or without uncommenting the 'exe...' lines (one after the other), I still get the error :
# initializers/wicked_pdf.rb
WickedPdf.config = {
# Path to the wkhtmltopdf executable: This usually isn't needed if using
# one of the wkhtmltopdf-binary family of gems.
# exe_path: '/usr/local/bin/wkhtmltopdf',
# or
# exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
# Layout file to be used for all PDFs
# (but can be overridden in `render :pdf` calls)
# layout: 'pdf.html',
}
My controller:
#bookings_controller.rb
class BookingsController < ApplicationController
def show
#booking = Booking.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render pdf: "test_wicked_pdf"
end
end
authorize #booking # For Pundit
end
The render HTML is working when i go to localhost:3000/bookings/135 ...
# bookings/show.html.erb
<h1>PDF test</h1>
...but not the PDF when I comment out "# format.html" in my controller
# bookings/show.pdf.erb
<h1>PDF test</h1>
Thanks a lot in advance
I know it is a pretty old question but you can use the gem wkhtmltopdf-binary-edge instead of the regular wkhtmltopdf-binary gem. It worked for me.
I am currently learning Rails Guides. I went through the steps but still encountered a mistake.
My Ruby version is ruby 2.1.1p76 and the Rails version is 4.0.4.
As the guide directed, I created an Article Controller.
class ArticlesController < ApplicationController
def new
end
def create
render plain: params[:article].inspect
end
end
I should get {"title"=>"First article!", "text"=>"This is my first article."} but the output turned out to be
Template is missing
Missing template articles/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}.`
Here is my related routes:
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
Update: render plain: is a new method introduced in Rails 4.1.0 referred to this issue.
In the render method, plain option was added in Rails 4.1 and you are using Rails 4.0.4. So, rails ignored this option and started looking for a template named articles/create as you are in ArticlesController#create action. Obviously, the template doesn't exist so you get the error Template is missing.
Refer to the discussion on this topic on Github: Introduce render :plain and render :html, make render :body as an alias to render :text
Now, for using the below mentioned syntax you would need to upgrade to Rails 4.1:
render plain: params[:article].inspect
With your current version of Rails 4.0.4, you can go for:
render text: params[:article].inspect
If you want to see textual information of params[:article] on your page then you can use render text
try this
class ArticlesController < ApplicationController
def new
end
def create
render text: params[:article].inspect
end
end
You will get
{"title"=>"First article!", "text"=>"This is my first article."}
# i.e. your params(whatever params hash contains)
Change Rails version in your Gemfile:
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.6'
Then run:
bundle install
Make sure that your Rails version now is > 4.1
You no need template means you could use render nothing: true
Try like this:
class ArticlesController < ApplicationController
def new
end
def create
params[:article].inspect
render nothing: true
end
end
Please refer this link click here
You may want to read through the following documentation.
Rendering pure text is most useful when you're responding to Ajax or
web service requests that are expecting something other than proper
HTML.
I wrote a Ruby script in which I use Nokogiri.
For Rails I made this module in the lib/ directory:
require "net/http"
require "uri"
require 'nokogiri'
Module gk_CT
class CT
def getCT
uri = URI.parse("http://www.website.com")
CT = Net::HTTP.get_response(uri)
proc = Nokogiri::HTML(CT.body)
CTQ = Array.new
CTQ << proc.css('td')
end
end
In the controller I have:
require 'gk_CT'
def show
#CT= gk_CT::CT.getCT()
respond_to do |format|
format.html # show.html.erb
format.json { render json: #CT}
end
end
It always gives me the error:
cannot load such file -- nokogiri
and I have no idea why.
If the script is part of an actual Rails project, then you need to add Nokogiri to the Gemfile (with the line gem 'nokogiri'). If you're not in a Rails project or aren't using Bundler or some such weird thing, you'll still need to install the gem (gem install nokogiri).