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.
Related
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.
I'm upgrading an application to Rails 4 and I cannot for the life of me figure out what is wrong with this method. The offender's the update method:
def update
respond_to do |format|
if #doc.articles.find_index { |a| a.changed? }
#doc.publications.destroy_all
end
if #doc.update_attributes(params[:doc])
#doc.create_activity :update, owner: current_user
if current_user.brand.editable? && params[:editing]
format.html { redirect_to editing_url(#doc) }
else
format.html { redirect_to share_url(#doc.user.ftp, #doc) }
end
end
end
end
Clicking submit generates this error:
ActionController::UnknownFormat in DocsController#update
and highlights this line:
respond_to do |format|
The create method works fine, it looks like this:
def create
#doc = Doc.new(params[:doc])
respond_to do |format|
if #doc.save
#doc.create_activity :create, owner: current_user
if current_user.brand.editable? && params[:editing]
format.html { redirect_to doc_editing_url(#doc) }
else
format.html { redirect_to share_url(#doc.user.ftp, #doc) }
end
else
format.html { render action: "new" }
end
end
end
Any thoughts at all? I'm totally stuck.
Oh, I've got this private method as a before_action too, so it's not that:
private
def set_document
#doc = Doc.find(params[:id])
end
EDIT
I found this quasi-explanation:
In Rails 4.0, ActionController::UnknownFormat is raised when the
action doesn't handle the request format. By default, the exception is
handled by responding with 406 Not Acceptable, but you can override
that now. In Rails 3, 406 Not Acceptable was always returned. No
overrides.
which makes me think it's something to do with routes, but my routes should be default if I've declared them like so, yes?
resources :docs, :except => [:new, :show] do
get "adjust/:state" => "docs#adjust", :as => :adjust
patch "editing" => "docs#editing", :as => :editing
patch "reupdate/" => "docs#reupdate", :as => :reupdate
get "pdf" => "docs#pdf", :as => :pdf
collection { post :sort }
end
EDIT 2
Adding the JSON to the controller, i.e.:
format.html { redirect_to share_url(#doc.user.ftp, #doc) }
format.json { render action: 'share', status: :created, location: #doc }
gives me a no method error and seems to redirect me back to the edit page:
Showing .../fin/app/views/docs/_form.html.erb where line #19 raised:
undefined method `covers?' for nil:NilClass
Really no idea what's going on here.
One possible reason can be that if #doc.update_attributes(params[:doc]) returns false there is no format block being executed in the update method.
Usually you are rendering the edit action in that case.
If you are only serving HTML then you don't need respond_to and format.html at all.
I'm using the respond_to and respond_with in a rails app, but in one use case I need to respond with just a text for just one of the resource formats (:json)... But I can't find how to do this...
I want something like this (I know this doesn't work)
def create
...
respond_with(:json, render :text => "Successfully Done!")
end
Any Idea??
Thanks!
It seems that this may be what you are looking for:
def create
respond_to do |format|
format.html
format.json { render :text => "Successfully Done!" }
end
end
Andres,
The solution is this:
class TextController < ApplicationController
respond_to :json, :text
def index
respond_with do |format|
format.json {
render :text => "I'm a text provided by json format"
}
format.text {
render :text => "I'm a text"
}
end
end
end
And at your routes.rb:
match '/text' => 'text#index', defaults: { format: 'text' }
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
I want my Rails rest application has json as default output format , also I want remove html format. Any suggestions? Thanks
Yes you can do it.
In your controller,
respond_to do |format|
#format.html # show.html.erb
#format.xml { render :xml => #post }
format.json { render :json => #post }
end
or you can handle it as javascript
respond_to do |format| format.js { render :json { :only => :name}.to_json end
then you just access your action with ".js" in the end
Try like this
format.any(:xml, :html, :json) { render :json => #post }