Prawn table method won't work - ruby-on-rails-3

I have been creating pdf documents fine with prawn. I am using prawn 0.8.4. I have created a class in pdf folder in the app directory like so.
class SchoolPdf < Prawn::Document
def initialize(school)
super(top_margin: 70)
#school = school
school_name
line_items
end
def school_name
text "School: #{#school.school_name}", size: 30, style: :bold
end
def line_items
move_down 20
table [[1,2],[3,4]]
end
end
This is code from my show action in the controller
def show
#school = School.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = SchoolPdf.new(#school)
send_data pdf.render,filename: "#{#school.school_name}_report.pdf",
type: "application/pdf",
disposition: "inline"
end
I get the error undefined method 'table' what could be wrong?

Upgrade to Prawn version 0.12.0 and it should work.

Related

strong param method expects to `define method_name` as `controller name`

rails (5.2.2.1)
ruby 2.5.0p0
Parent controller of Country, State, City
class LocalityController < ApplicationController
def create
locality = model_name.new(locality_master_params)
respond_to do |format|
if locality.save
format.html { redirect_to locality, notice: 'Record was successfully created.' }
else
format.html { render :new }
end
end
end
private
def model_name
"#{controller_name.titleize.delete(' ').singularize}".constantize
end
def locality_params
#locality_params = %i|name code status|
end
def locality_master_params
params.require("#{controller_name.singularize}".to_sym).permit(locality_params)
end
end
State controller
class StateMastersController < LocalityController
alias_method :state_master_params, :locality_master_params
private
def locality_params
#locality_params = %i|name code status country_code|
end
end
Expectation: country-state-city controllers should be inherited from one controller and manage same templates, methods for all those controllers to DRY.
This code works fine as per the expectation.
Issue: after removing below code(as it is unnecessary):
alias_method :state_master_params, :locality_master_params
it gives error as:
ActiveModel::ForbiddenAttributesError
I've added alias_method to prevent above error.
Getting same error in other controllers too: country-city controllers.
Is there any convention to define method as state_master_params for state_master_controller?
`

How to destroy an instance variable of one class within the controller of another class

I'm doing the chapter exercises of Agile Web Development with Rails. It's a store with products which you can add to a cart. A product inside a cart is called a LineItem. You can delete individual line items inside the cart. When the last line item is deleted I want the entire cart to be destroyed. The code I have doesn't work:
CartsController
def destroy
#cart = current_cart
#cart.destroy
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_path }
format.json { head :no_content }
end
end
LineItemsController
def destroy
#cart = current_cart
#line_item = LineItem.find(params[:id])
quantity = #line_item.quantity
if quantity > 1
quantity -= 1
#line_item.update_attribute(:quantity, quantity)
else
#line_item.destroy
end
respond_to do |format|
format.html {
if #cart.line_items.empty?
#cart.destroy
else
redirect_to #cart
end
}
format.json { head :no_content }
end
end
This produces the following error:
Template is missing
Missing template line_items/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/mike/Projects/depot/app/views"
When the cart is destroyed I want to end up at store_path.
thanks,
mike
See the addition (as a comment):
if #cart.line_items.empty?
#cart.destroy
# redirect_to store_path
else

How can I troubleshoot this JSON method in DataTables gem?

I'm trying to implement the Railscast 340 that demos how to use DataTables, which looks like an awesome gem for my project.
My model is different, of course; but the datatables class the Mr Bates builds (very quickly), in order to do server-side processing, is rather complicated to follow. I got the source code, and basically attempted to follow along. My view comes up with zero records (but there are > 10,000 records), but does not break.
However, here is what the error message output from the rails server says just before it stops:
NameError (undefined local variable or method `genotypes' for #<GenotypesDatatable:0xa9e852c>):
app/datatables/genotypes_datatable.rb:12:in `as_json'
app/controllers/genotypes_controller.rb:8:in `block (2 levels) in index'
app/controllers/genotypes_controller.rb:6:in `index'
Just before this, there appears to be this JSON error, which starts:
Started GET "/genotypes.json?sEcho=1&iColumns=8&sColumns=&iDisplayStart=0&iDisplayLength=10&mDataProp_0=...
The relevant part of the genotypes controller looks like this:
def index
respond_to do |format|
format.html
format.json { render json: GenotypesDatatable.new(view_context) }
end
end
And my genotypes model looks like:
class Genotype < ActiveRecord::Base
attr_accessible :allele1, :allele2, :run_date
belongs_to :gmarkers
belongs_to :gsamples
end
My datatables class is given below. This is from Mr Bates code, modified (most likely incorrectly) to replace his Products model with my Genotypes model:
class GenotypesDatatable
delegate :params, :h, :link_to, to: :#view
def initialize(view)
#view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Genotype.count,
iTotalDisplayRecords: genotypes.total_entries,
aaData: data
}
end
private
def data
genotypes.map do |genotype|
[
link_to(genotype.name, genotype),
h(genotype.category),
h(genotype.released_on.strftime("%B %e, %Y")),
genotype.run_date
]
end
end
def Genotypes
#Genotypes ||= fetch_Genotypes
end
def fetch_genotypes
genotypes = Genotype.order("#{sort_column} #{sort_direction}")
genotypes = genotypes.page(page).per_page(per_page)
if params[:sSearch].present?
genotypes = genotypes.where("name like :search or category like :search", search: "%#{params[:sSearch]}%")
end
genotypes
end
def page
params[:iDisplayStart].to_i/per_page + 1
end
def per_page
params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
end
def sort_column
columns = %w[gmarker gsample allele1 allele2 run_date]
columns[params[:iSortCol_0].to_i]
end
def sort_direction
params[:sSortDir_0] == "desc" ? "desc" : "asc"
end
end
Any hints on how to troubleshoot (or fix!) this error much appreciated! (Getting this working for my project would be awesome!)
TIA,
rixter
I'm not sure if this is it, but your class has a Genotype method with capital G, it should be all lowercase.

how to display an alternate message ,if particular record is not available?

presently i am playing with rails3.Can it possible to show a message like"Sorry , this data is not available", in view if we do not have that data in record.
This is a way to...
class ModelController < ApplicationController
def show
respond_to do |format|
begin
#model = Model.find(params[:id])
rescue ActiveRecord::RecordNotFound
format.html { render :text => "Sorry , this data is not available" }
end
end
end
end

Rails - Capitalize Article Tags and Titles

Am trying to find a way of capitalizing the 1st letter of all Titles and Tags when a user submits an article. I can use the capitalize method, but where do I add it to the controller code blocks for it to work?
Thx
controllers/articles_controller:
def new
#article = Article.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #article }
end
end
controllers/tags_controller:
class TagsController < ApplicationController
def show
#tag = Tag.find(params[:id])
#articles = #tag.articles
end
end
models/article:
def tag_names
#tag_names || tags.map(&:name).join(' ')
end
private
def assign_tags
if #tag_names
self.tags = #tag_names.split(/\,/).map do |name|
Tag.find_or_create_by_name(name)
end
end
...
Where do you plan to capitalize it? before saving in the database? or when you're showing it to the user?
There are two ways to this:
Use rail's titleize function or capitalize
or do it using CSS with:
<p class="tag">im a tag</p>
#CSS
.tag {
text-transform:capitalize;
}
I would do something like this to force them to be capitalized before saving.
class Article < ActiveRecord::Base
def title=(title)
write_attribute(:title, title.titleize)
end
private
def assign_tags
if #tag_names
self.tags = #tag_names.split(/\,/).map do |name|
Tag.find_or_create_by_name(name.capitalize)
end
end
end
end
Try to use .capitalize
e.g. "title".capitalize will make "Title"
As Francis said, use .capitalize in your controller
I use this
#article= Article.new(params[:article])
#article.name = #article.title.capitalize
#article.save