I am working with Wicked_PDF to generate pdf files for my application. Most examples normally have it generating based on a show page, but I want a link to get to a pdf for an index page.
Product Controller
def index
#search = Product.search(params[:q])
#products = #search.result.includes(:supplier, :prodcategory, :prodstatus)
respond_to do |format|
format.html
format.pdf do
render :pdf =>"product_report", :template => 'products/index.html.erb'
end
end
end
Product Index
<p><%= link_to "Printable Invoice (PDF)", products_path(#products, format:"pdf") %></p>
I know for certain that localhost:3000/products.pdf generates it just fine, but I'm having issues figuring out the correct path for the link.
Related
If I have the following code, Rails is setup to automatically look in the views folder and find photos/feed.js.erb. But what I want to do is to tell it to run users/feed.js.erb instead.
PhotosController
def feed
#title = "Favorites"
#user_feed_items = current_user.favorites.order('created_at desc').paginate(page: params[:page], per_page: 15)
respond_to do |format|
format.html {
render 'users/feed' }
format.js
end
end
Rails: Render a .js.erb from another controller?
format.js { render :file => "/users/feed.js.erb"}
render accepts the full path (relative to app/views) of the template to render. So, you can just enter users/feed
render "users/feed"
Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the :template option.
render template: "users/feed"
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
How do I get dynamic content when user clicks on different links?
views/dashboard/index.html.haml
.container
- #trips.each do |trip|
= link_to trip.id, quick_view_trips_path, remote: true
I'm pretty sure the quick_view_trips_path is incorrect as all the links are this:
1
13
51
Somehow I need to make these links dynamic, and then when user clicks on it, the modal window would be dynamic too.
If I replace quick_view_trips_path with just trip
= link_to trip.id, trip, remote: true
Nothing happens, and the url changes to the correct url, but my modal window doesn't get rendered through ajax. Here's an example of what the url looks like now:
1
In addition to what I'm trying to accomplish with dynamic content, is there a way to maybe change my url like so:
1
Is it possible to get ?quick_view=on appending to end of URL and make everything work again?
Here are the rest of my code:
views/trips/quick_view.js.erb
$('body').append('<%= j render partial: "trips/quick_view" %>');
views/trips/_quick_view.html.haml
.root-container
= #trip.title
= #trip.image
= #trip.more_details
This doesn't work either right now, as my application returns undefined method
routes.rb
resources :trips do
collection do
get 'quick_view'
end
end
trips_controller.rb
def quick_view
respond_to do |format|
format.html # quick_view.html.erb
format.js # quick_view.js.erb
format.json { render json: #trip }
end
end
Do I need to add anything to this controller as well to ensure the correct content will be generated through the partial?
How about this way,
Path
link_to trip.id, quick_view_trips_path(:js, trip_id: trip.id), remote: true
This will render, 1
Controller
def quick_view
#trip = Trip.find(params[:trip_id])
respond_to do |format|
format.html # quick_view.html.erb
format.js # quick_view.js.erb
format.json { render json: #trip }
end
end
It will respond views/trips/quick_view.js.erb file
I have problem collection of pdfs file and save it to file. I create this for show action so when I clink the link it generate pdf and store them in public folder:
def show
add_breadcrumb "Inovice details"
respond_to do |format|
format.html
format.pdf do
render :pdf => "file_name", :save_to_file => Rails.root.join('public', "Invoice no. #{#invoice.format_id}.pdf")
end
format.csv {send_data Invoice.where(id: #invoice.id).to_csv,
filename: "Invoice no. #{#invoice.format_id}.csv"}
end
end
Now I want to create the same functionality but for collection of objects. For examples I have 10 invoices and I want for all of them generate pdf and save it to public folder. I was trying something like that:
def index
#invoices = #company.invoices
respond_to do |format|
format.html
format.js
format.csv { send_data #invoices.to_csv }
format.pdf do
#invoices.each do |invoice|
render :pdf => "file_name", :save_to_file => Rails.root.join('public', "Invoice no. #{invoice.format_id}.pdf")
end
end
end
authorize #invoices
end
But it didnt work. I have no ideas how to solve this problem. I will be grateful for every help.
You can not send multiple PDF's in the same request.
I think a best solution is generate the PDF's in a background job ( https://github.com/mileszs/wicked_pdf/wiki/Background-PDF-creation-via-delayed_job-gem ) and present an HTML page with links to all you PDF's.
If that doesn't work for you, you can merge all the content in a big PDF file.
This is my view: (show.html.erb)
<%=link_to vote_for, vote_for_question_path(#question), :remote => true%>
This is the action that the link calls in the question_controller:
def vote_for
#quest_vote_for = Question.find(params[:id])
current_user.vote_exclusively_for(#quest_vote_for)
#total_vote = current_user.total_votes
respond_to do |format|
format.js{}
format.html
end
end
What I want, is to update the content of the show.html.erb after the link is clicked. (I am using ajax (remote=>true)). The show.html.page should update itself with the new value of #total_vote.
How can I do this?
Add vote_for.js.erb, then add the following content to your file:
$("body").prepend("<h1><%= #total_vote %></h1>")
I have an index controller action which responds to the pdf format which looks something like
class ProposalsController < ApplicationController
respond_to :pdf, :html
def index
#proposals = Proposal.all
respond_with #proposals do |format|
format.html
format.pdf
end
end
end
Proposal has a to_pdf method which creates Prawn::Document. How can I loop over each of the propsals, grab it's PDF, append it to a newly created PDF, and then render that in the browser via respond_with?
One can use rails API to create a personalized renderer. In fact, the book Crafting Rails 4 Applications by José Valim show you how to create personalized PDF renderer in the first chapter.