rails 3 sending view_context to Prawn - ruby-on-rails

I'm trying Prawn to generate pdf's, following this RailsCasts
If I tried the next code, it works fine:
pdf = OrderPdf.new(#order)
But, if added "view_context"
pdf = OrderPdf.new(#order, view_context)
I got this error: "SyntaxError in xxxController#index"
syntax error, unexpected ',', expecting ')'
I tried put in my controller helper_method :view_context but the error still.
Enviroment: rails 3.2.5
My Controller:
def show
#liquidacion = Liquidacion.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #liquidacion }
format.pdf do
pdf = LiquidacionPdf.new (#liquidacion, view_context)
send_data pdf.render, filename: "liquidacion_#{#liquidacion.anio.to_s()+''+#liquidacion.mes.to_s().rjust(2,'0')+''+#liquidacion.numeroliquidacion.to_s()}", type: "application/pdf", disposition: "inline"
end
end
end
And my classPDF:
class LiquidacionPdf < Prawn::Document
def initialize(liquidacion, view)
super(top_margin: 20, :page_layout => :landscape)
#liquidacion = liquidacion
#view = view
numero_liqui
nombre_usuario
lineas_liqui
total_liquidacion
firmas
end
How I writte this?

Your initialize method should accept two parameters. I am guessing that you still only have one it.
Change your initialize method to the below
def initialize(order, view)
super(top_margin: 70)
#order = order
#view = view
order_number
line_items
end
EDIT
take off parenthesis or empty space
respond_to do |format|
format.html # show.html.erb
format.json { render json: #liquidacion }
format.pdf do
pdf = LiquidacionPdf.new #liquidacion, view_context

Related

How to render view in parent controller

I want to pass down values from a parent controller, to the views of the child, or ideally, render the view directly from the parent controller.
Here is my parent controller:
class SuperBlogPostsController < ApplicationController
def index(country)
if country == "AUSTRALIA"
#posts = AustraliaBlogPost.all
hash = { posts: #posts, featured_post: AustraliaBlogPost.find_by(featured_post: true) }
respond_to do |format|
format.html {render 'super_blog_post/index'}
format.json {render json: hash}
end
end
end
def show(country)
if country == "AUSTRALIA"
#post = AustraliaBlogPost.find(params[:id])
respond_to do |format|
format.html {render 'super_blog_post/show'}
format.json {render json: #post}
end
end
end
And here is my child:
class AustraliaBlogPostsController < SuperBlogPostsController
def index
super(country: 'AUSTRALIA')
end
def show
super(country: 'AUSTRALIA')
end
end
This is the error that I get, even when I append .json at the end of the url:
Is it possible to do the rendering of the view in the parent, or at least pass down the return values of the variables generated by the parent to the child? If I've been unclear at all, let me know!
Thanks!
Your parent controller methods accept a normal parameter, not named parameter.
Instead of:
super(country: 'AUSTRALIA')
You need
super('AUSTRALIA')
But I must say, the entire logic of your controllers doesn't make a lot of sense.

How to avoid the rendering to the next page

When I'm trying to download the CSV files it is rendering the next page, it should stay on the current page.
class EmployeeProofsController < ApplicationController
def index
#employeeproofs = EmployeeProof.all.order('id ASC')
respond_to do |format|
format.html { render "employees/index", :layout => false }
format.csv { send_data #employeeproofs.to_csv, :disposition => "attachment" }
end
end
end
Can any one tell me how to avoid the page rendering?
Remove the format.html part from your code along with this add , defaults: { format: :csv } to your route used for this action.

Add Virtual attribute to active record object to output

I have a custom method in my model called "file_url(:thumb)" to receive a specific thumbnail file URL. The method is provided by the carrierwave gem.
It is not stored in my database. How can I add this virtual attribute to #document so when I convert to json it's included?
module Api
module V1
class DocumentsController < ApiController
respond_to :json
def show
#document = Document.find(params[:id])
respond_to do |format|
format.json { render json: #document }
format.xml { render xml: #document }
end
end
end
end
end
You would need to define your own as_json method in the Document model. Something like this would do the trick:
def as_json(options = { })
h = super(options)
h[:thumb_url] = file_url(:thumb)
h
end

Rails - How to render json with extra field in index controller method

I'm using Rails 4.0.2 with paperclip for image upload in my project. Also I need to send a full image path of paperclip. So I can do it with add new field and set image path manually in my show controller method for particular record.
show
def show
respond_to do |format|
format.html
format.json { :json => JSON::parse(#demo.to_json.merge("new_field" => #demo.image_url.url).to_json}
end
end
When I view Json for any of my record, this is will showing good.
{
id: "1",
name: "demo",
new_field: "/demo/1/original/file.jpg"
}
In same scenario, I need to get the full image path of paperclip image for all records when I am requesting to index method on controller
index
def index
#demos = Demo.all
respond_to do |format|
format.html
format.json { :json => Demo.all.to_json}
end
end
I tried some of codes, but I don't know how exactly to write
def index
#demos = Demo.all
#demos.each do |demo|
new_field = {"new_field" => #demo.image_url.url}
# After I stucked with logic, how to uppend with 'demo'.
end
respond_to do |format|
format.html
format.json { :json => Demo.all.to_json}
end
end
How do I iterate my individual Demo model and How to merge full image path into each record.
I found the solution for my question,
def index
#demos = Demo.all
#demos_data = []
#demos.each do |demo|
new_field = {"new_field" => #demo.new_field.url}
demo = JSON::parse(demo.to_json).merge(new_field)
#demos_data << demo
end
respond_to do |format|
format.html
format.json { :json => #demos_data}
end
end
I suggest you to use two approaches, 1)use active model serializer to expose json response. 2) use jbuilder library to expose custom fields. Still you need help please let me know.
Try this:-
def index
#demos = Demo.all
#demos_data = []
#demos.each do |demo|
demo["new_field"] = #demo.image_url.url
#demos_data << demo
end
respond_to do |format|
format.html
format.json { :json => #demos_data}
end
end
maybe you can try:
def index
#demos = Demo.all
#demos.map do |demo|
new_field = {"new_field" => #demo.image_url.url}
demo.attributes.merge(new_field)
end
respond_to do |format|
format.html
format.json { :json => #demos}
end
end
attributes method returns a hash of all the object attributes, just need merge new key-value into the returned hash.
Another way of doing it is in your controller where you are rendering the json
render json: #merchants,
include: {
offers: {
except: [:created_at, :updated_at],
include: {
categories: {
except: [:created_at, :updated_at]
}
}
},
location: {
methods: :country_name,
except: [:created_at, :updated_at]
}
},
except: [:created_at, :updated_at]
Note the methods: :country_name, there you can render methods from your model as json attributes. and through include: ... you can eager load and render related models.

ArgumentError rails

I have a module that returns an array:
module Module1
class Class1
def self.get
num << 1
return num
end
end
end
But when I call it from the controller like this:
def index
#trans = Module1::Class1.get()
respond_to do |format|
format.html # index.html.erb
format.json { render #trans }
end
end
Show me the following error:
'1' is not an ActiveModel-compatible object that returns a valid partial path.
But if I do in json:
def index
respond_to do |format|
format.html # index.html.erb
format.json { render Module1::Class1.get() }
end
end
It returns the right result, what am I doing wrong in the first example?
Try this
format.json { render :json => #trans }

Resources