First of all i know this type of posts are already been made but i've tried almost all of them and i'm not been able to get the results so here i'm again posting the same kind of question.
Second of all i'm not having an asset pipeline issue here, so please forget about that.
Now let me explain what i'm doing. I'm using rails 3.1 and ruby 1.9.2, i installed wicked_pdf as a gem and installed wkhtmltopdf as mentioned in the wiki by purging the already installed wkhtml and downloading and extracting the new one to /usr/bin/wkhtmltopdf
I have an initializer that contains the following:
wicked_pdf.rb
WickedPdf.config = { :exe_path => '/usr/bin/wkhtmltopdf'}
In my view i have a link_to method as follows:
_filters.html.haml
= link_to 'show pdf', jobs_report_jobs_path(:format => :pdf), :method=>"post"
note if i remove the :format => :pdf option it works fine
in my controller i'm doing the following:
report_jobs_controller.rb
respond_to do |format|
format.html
format.js
format.pdf{
render :pdf=>"jobs",
:template => 'jobs.html.erb',
:layout=>"jobs.html"
}
end
note that i have tried from format.pdf alone without any options. I tried "jobs.pdf.erb", with and without layout option, all sorts of other options i don't even remember. All i get is a 406 not acceptable in the end.
Please guide me coz i need to implement this feature asap.
Regards,
406 means the request isn't valid (in regards to what is acceptable to that controller action)
I've had trouble with :format => :pdf before. Try :format => 'pdf'
The barebones implementation should just be:
format.pdf {
render :pdf => 'jobs'
}
Also, is the link_to really supposed to be :method => "post"?
i had a before_filter in my application controller that was checking every request with the mentioned format for authentication and i was missing pdf format there, so as soon as i put the :pdf in the list of formats for each incoming request it worked fine.
I too had the same issue. I work on ubuntu. AFter I installed wkhtmltopdf, I am not getting this error any more.
in terminal run the command below:
$sudo apt-get install wkhtmltopdf
I hope this helps :)
I don't how much about wicked-pdf, but I once used pdfkit and this is how I did the rendering part:
def pdf
respond_to do |format|
format.pdf { render :text => PDFKit.new( Pdf.find(params[:id]).content ).to_pdf }
end
end
I hope the code is clear enough and self-explanatory. My view code is:
<p><%= link_to "Download PDF", pdf_pdf_path(#pdf, :format => "pdf") %></p>
Related
I have a ransack search form which is working wonderfully, I would like to add an export for the user to send the contents of the result set to an XLS file.
I have implemented the to_xls sucessfully as well, however it is giving me back the fullest possible scope of the object I am searching, and not the filtered results that are shown in the view.
def index
#search = Expense.search(params[:q])
#expense_list = #search.result.sort_by(&:expense_date)
respond_to do |format|
format.html
format.xml { render :xml => #expense_list }
format.xls { send_data #expense_list.to_xls, :filename => '123.xls'}
end
end
Does it have something to do with how ransack uses the GET method? Any help would be great.
Thanks!
I know this is such a hack, after spending many hours of not getting it, I used it anyway.
make xls
so basically it takes the searchpath after the ?, then adds it to your model.xls output path and then it works. I hate it myself, there must be a better way, but deadlines.
There was a good link here.
Ronin gave a simple solution to this related question, but with CSV instead of XLS . In my case, using Ronin's answer, I just rewrote the link to work with XLS as shown below
<%= link_to "Download Excel", reports_path(params.merge(format: "xls")) %>
%= link_to "Download Excel", yours_controller_path(params.merge(format: "xls")) %>
I have a RoR app that tracks the status of values in a database and displays the visualization of this information in the form of charts, graphs, and tables generated by an erb file. This is very handy and I am able to save snapshots of the status' of the DBs by simply saving the page when I open it in a browser. What I would like, however, is for my app to automatically do this saving for me on a nightly basis. I assume this is possible but I'm not having much luck with this so far. Any suggestion on this point would be very helpful.
you can always use render_to_string method to render your erb. For example you need to render show.html.erb for show action in statistics controller:
my_page = render_to_string :controller => 'statistics', :action => 'show', :layout => 'application'
but firstly you should define all it's variables which you use int show view.
#data = Data.last_data
#users = User.active
enter code here
my_page = render_to_string :controller => 'statistics', :action => 'show', :layout => 'application'
snapshot = Snapshot.new :page => my_page
snapshot.save
API: http://apidock.com/rails/ActionController/Base/render_to_string
You can use whenever: https://github.com/javan/whenever
From the readme:
Whenever is a Ruby gem that provides a
clear syntax for writing and deploying
cron jobs.
There is also delayed-job: https://github.com/tobi/delayed_job
From the Readme:
Delayed_job (or DJ) encapsulates the
common pattern of asynchronously
executing longer tasks in the
background.
I am running a RoR application (rails 2.3.8, ruby 1.8.7), the application runs fine on my local machine. but on production the logs show the following error:
ActionView::TemplateError (Missing template folder/_file_name.erb in view path app/views) on line #19 of app/views/layouts/main.rhtml:
19: <%= render :partial => "folder/file_name" -%>
the file name exists as folder/_file_name.html.erb, I tried to reproduce the problem on the production environment but didnt have any luck, for some reason rails application asks for folder/_file_name.erb at some times while other times it searches for the right file folder/_file_name.html.erb.
Could someone explain to me what is going on?
The same also occurs for .rhtml files, rails application requests .erb at times while others get the right .rhtml file
update:
<%= render :partial => "shared/meta_tags" -%>
<%= render :partial => "shared/common_resources" -%>
<%= render :partial => 'shared/ads/oas' -%>
Any pointers on this issue will be helpful,
thanks in advance
Whats the format of the request?, for the first template (folder/_file_name.html.erb) it will only be correct if the request format is html, but not if it is ajax or any other custom type you have in your app. One quick soluction would be to rename it to folder/_file_name.erb if you want to use the same partial for all the formats
Is there a controller action with the same name as that file?
If you have a foo controller with a bar action and no response defined in your action, Rails will try and render views/foo/bar.html.erb.
If that's not what you want, you need to define a response in your controller and tell Rails to render the appropriate partial, like so:
respond_to do |format|
format.html do
render :partial => "/foo/bar"
end
end
In the latter case, Rails will render "views/foo/_bar.html.erb"
In some cases You can't prevent this error as there are load reasons like missing cache, unknown request format and etc
You can try to restrict the number of predefined formats like:
get '/about-us' => 'controller#about', :format => /(?:|html|json)/
However, I added the following method in my application_controller.rb file so that such errors will render a 404 page rather failing with a error message on screen
rescue_from ActionView::MissingTemplate, :with => :rescue_not_found
protected
def rescue_not_found
Rails.logger.warn "Redirect to 404, Error: ActionView::MissingTemplate"
redirect_to '/404' #or your 404 page
end
you can wrap this code in a if statement, something like this if Rails.env.production? given that the env is setup so your dev environment wont be affected
I'm trying to generate a PDF file using AJAX call in Rails3. The following code generates a PDF file which I have created using PRAWN gem.
<%= link_to "Generate pdf", books_path(#book, :format => 'pdf') %>
I do not want user to view the PDF until they order it. So, the goal is to create a PDF file in the server.
Any ideas or thoughts much appreciated.
Use this, make sure your remote action does not return the PDF, but simple generates and stores it on the server.
link_to "Generate PDF", prepare_books_path(#book), :remote => true, :method => :put
This will work in Rails 3. If you're using jQuery, make sure to read this article on how to set things up correctly.
Your controller action may look like this:
def prepare
# Do your thing to generate the PDF
render :text => "PDF Generated", :status => 200
end
I used the PUT-method because you are altering the state of your data (e.g. you are generating something new, you don't want a bot or crawler to automatically call that).
Firstly, it beats me why you would do something on a request like generating a PDF, when the user is not expecting that action. Isn't better to only generate the pdf when the user requests for it?
Thanks Ariejan.
I modified your code as following and it did just what I wanted.
<%= link_to "Generate Story Book", pdfbook_stories_path(:format => 'pdf'), :remote => true %>
And for the controller,
def pdfbook
#stories = current_account.stories
respond_to do |format|
format.pdf {}
end
end
I've got a really simple rails question here but I can't seem to find the answer anywhere. I guess some of the problems stem from me following a tutorial for Rails 1.2 with Rails 2.1. Anyway..
I'm writing a blog system and I'm implementing the comments bit. I have comments displaying fine once I've created them using script/console, but getting the comment form itself working is the hard bit.
In posts_controller.rb I have
def comment
Post.find(params[:id]).comments.create(params[:comment])
flash[:notice] = "Added comment"
#render :action => show
redirect_to :action => show
end
and in show.html.erb (the view) I have
<%= form_tag :action => "comment", :id => #post %>
<%= text_area "comment", "body" %><br>
<%= submit_tag "Post Comment" %>
When I submit the form it tries to go to the urb /posts/comment/1 which is obviously incorrect, and it complains that it can't find a template. Obviously I don't want a template there because I've told it to redirect to the show action because I want it to just re-display the post's show page, with the new comment there.
I've tried both the commented out line (render :action => show) and the redirect_to line, and neither seem to do anything at all.
I'm sure I'm missing something simple, but what is it?
Does redirect_to :action => 'show', :id => params[:id] with quotes around show work?
Rails 2.1 embraces "RESTful resources". show just happens to be the name of one of the predefined REST actions that all rails controllers use.
Rails does some magic behind the scenes, and :show is equivalent to "display this one specific element with a specific given ID". Sounds like it's getting mixed up with that. The ID is probably defaulting to "1". Hence the generated URL you're seeing from the render call
The Rails 2.1 way of doing it would use the following actions and templates:
index - displays the full list of comments
create - add a new comment
show - display a specific comment only (not the full list). Doesn't sound like this is what you want, but the "magic" inside rails will default to this.
There are also actions for new (show view to enter a new comment) edit (show view to do an edit of an existing comment) update (handle update submission) and destroy (duh), but it doesn't look like you'd use them in this example.
Do you have a link to the tutorial? Wouldn't be too hard to port it to Rails 2.1 style.
yes, you use old rails style.
Something new:
form_for :comment, :url => { :post_id => #post } do |f|
f.text_area :body
submit_tag "Post"
end
you can use resources for posts and comments, search google for better tutorial or install rails 1.2.6:
gem install -v 1.2.6 rails