NoMethodError (undefined method `ransack' for #<Class:0x000055725c12ba78>) - ruby-on-rails

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?

Related

Unknown format with wicked_pdf

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.

ruby on Rails-login page

I tried to create a simple login_app using 'Authlogic' gem and i got this error:
NameError in UsersController#create
undefined local variable or method `users' for #<UsersController:0x0000000354ba48>
Extracted source (around line #28):
code:
def create
#user = User.new(params users ) # <- this line
respond_to do |format|
if #user.save
Rails.root: /home/ameex/projects/login_app
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:28:in `create'
please help me to resolve this
If you are using Rails4,this should work.
class UsersController < ApplicationController
def create
#user = User.new(user_params)
respond_to do |format|
if #user.save
format.html { redirect_to #user, notice: 'User was successfully created.' }
else
format.html { render action: "new" }
end
end
end
private
def user_params
params.require(:user).permit(:your_user_attr1,:your_user_attr2,..)
end
end
Have a look at Strong Parameters in these Guides.
To back up Pavan's answer, the error you had was this:
UsersController#create undefined local variable or method `users' for
#
This is a typical problem, and basically means you've referenced a local object which doesn't exist (in your case users). The issue is that now you're trying to call this, Rails cannot find it, leading to the exception
--
Params
As Pavan pointed out, the problem you have is you're calling:
#user = User.create(params users)
The problem here is you're calling a non-existent object, as explained; but what is much more important is your actual syntax is wrong. You're getting confused with two variables inside Rails:
params hash
strong_params method
When you create a new object in Rails 4, you need to tell ActiveRecord which params values you wish to populate the new record with. This is currently done by using strong_params, which essentially whitelists different members of the params hash
The bottom line is when you reference data / objects in Rails, you have to have them available beforehand. Your referencing of params users references neither the relevant params[:key][:value], nor your strong_params method
Hopefully this gives you some more ideas

Instance variable not passed into view in Rails

EDIT: Solved. Solution:
Include
gem 'rabl' and gem 'oj' in your gemfile along with gem rabl-rails
For some reason, my instance variable isn't being passed into the view (I'm using Rabl).
Here's the relevant code:
articles_controller.rb
class ArticlesController < ApplicationController
respond_to :json, :xml
def index
#articles = Article.original.last(100)
end
def after
#articles = Article.where("id > #{params[:id]}")
end
def show
#article = Article.find(params[:id])
end
end
show.json.rabl
object #article
attributes :id, :headline, :source, :link
attributes :similar_articles => :similar
The error:
RuntimeError in Articles#show
Showing /Users/chintanparikh/Dropbox/Projects/Current/article_aggregator/app/views/articles/show.json.rabl where line #2 raised:
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
Any ideas?
The ID you're using to look up the record does not match one within your database.
Check what params[:id] is evaluating to and ensure that you can locate a record using that value.
Found the solution, you need these three lines in your Gemfile:
gem 'rabl-rails'
gem 'rabl'
gem 'oj'
Before, I only had rabl-rails, and I assume that was causing the problem.

rails debugger error

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])
...

active admin destroy action

im working in a project writen in ruby on rails and im currently using the active admin gem for its content manager system of my site im just wondering how active admin is using the delete action im trying to overide it but i my code doesent work, i think we are having a problem in how to get the specific line to be destroy
def destroy
#menu = Menu.find(params[:menu_recipe][:menu_id])
#menu_recipe = #menu.menu_recipes.find(params[:id])
#menu_recipe.remove_recipe
#menu_recipe.destroy
redirect_to #reservation, :notice => "recipe destroyed"
end
it comes with a error of
undefined method `[]' for nil:NilClass
Probably params[:menu_recipe] is not set. Try putting params[:menu_recipe] ||= {} at the beginning.
But why is it that you're finding through the Menu, anyway? Can't you just do 'MenuRecipe.find(params[:id])'?

Resources