I read about the surveyor gem and in the webpage http://nubic.github.io/surveyor/
Where do I put this line to export the survey report to a pdf?
%li= link_to "PDF", view_my_survey_path(:survey_code => response_set.survey.access_code, :response_set_code => response_set.access_code, :format => 'pdf')
I am not understanding please help.
From the looks of the website that you supplied you should have generated the surveyor:custom controllers. From this it then tells you to install the two following gems
gem 'pdfkit'
gem 'wkhtmltopdf'
But personally I prefer to use wicked_pdf in my opinion. So slightly changing the tutorial a little I would do the following:
rails generate surveyor:custom
This will generate the controllers. So then we can later configure this using the options that wicked_pdf will supply us.
Add the following lines to your Gemfile:
gem 'wicked_pdf' #https://github.com/mileszs/wicked_pdf
gem 'wkhtmltopdf' #https://code.google.com/p/wkhtmltopdf/
In order to have wicked_pdf export correctly you need to have something like this inside an action in your surveyor controller that is generated for you. So you may have something like this in your index action of your surveyor_controller.rb
respond_to do |format|
format.html
format.pdf do
render :pdf => "Survey Report",
:header => {:html => {:template => 'layouts/pdf.html.erb'}}
end
end
From this respond block you could/should I think have the following directory in views - app/views/survey/index.html.erb Inside the index.html.erb you would then have your above link inside this.
<%= link_to "PDF", view_my_survey_path(:survey_code =>
response_set.survey.access_code, :response_set_code =>
response_set.access_code, :format => 'pdf') %>
I have never used the surveyor gem before. But based upon what you have supplied, and what you want I have tried to give as much of a detailed explanation as I can. Hope this potentially helps you or anyone else
Example of Wicked_PDF usage
def index
#bookings= Booking.scoped
booking = #bookings
#user = current_user
if params[:format] == 'pdf'
#bookings= #bookings.where(:day => Date.today.beginning_of_month..Date.today.end_of_month)
end
respond_to do |format|
format.html
format.pdf do
render :pdf => "#{Date.today.strftime('%B')} Overtime Report",
:header => {:html => {:template => 'layouts/pdf.html.erb'}}
OvertimeMailer.overtime_pdf(#user, booking).deliver
end
end
end
View.html.erb
<%= Link_to "Overtime", "booking.png", bookings(:format => "pdf") %>
In order to generate the PDF you need to create a file which has the following extension .pdf.erb what this means is that when the link is processed it processes the embedded ruby code first and then outputs PDF. So in this case I want to do bookings. So I did created in my views/bookings/index.pdf.erb inside this I have the following:
views/bookings/index.pdf.erb
<br />
<% #bookings.each do |booking| %>
<p>
<b>Employee</b>
<%= booking.user.try (:full_name)%>
</p>
<p>
<b>Hospital</b>
<%= booking.hospital.try(:name)%>
</p>
<p>
<b>Day</b>
<%= booking.day.to_s(:long_ordinal) %>
</p>
<p>
<b>Overtime</b>
<%= booking.overtime %>
</p>
<br />
<% end %>
<h2>Total Overtime is <b><%=#bookings.to_a.sum(&:overtime)%></b></h2>
This should give you some core grounds to work off of
Related
I'm new to Rails, and I'm having an issue where I can't render a .js.erb file. I think the root of the issue is that Rails' internal routing mechanism expects me to name and configure my files just so, but I'm missing one or two pieces, and I'm not sure how to look for what needs to be fixed.
I have an HTML view with a link to a controller action:
<%# snip %>
<div id="holding_issues_list">
<%= link_to "Show issues on hold", {
:action => "show_user_issues",
:controller => "support",
:issue_type => "holding",
:user_id => #user.id },
:remote => true %>
</div>
<%# snip %>
I think (but I'm not sure) that :remote => true causes the link to make an AJAX call.
This is the corresponding controller action in the controller app/controllers/support_controller.rb:
def show_user_issues
#target_div = params[:target_div] || "holding_issues_list"
user = User.find(params[:user_id])
issue_type = params[:issue_type]
#snip - set the value of #issues
end
I want this file, named show_user_issues.js.erb and placed in app/views/support, to be rendered when the controller exits:
$("#<%= #target_div %>").show();
alert('test');
$("#<%= #target_div %>").html(
"<%= escape_javascript render :partial => '_show_user_issues', :locals => {:target_div => #target_div, :issues => #issues} %>");
This is app/views/support/_show_user_issues.html.erb, the partial I want show_user_issues.js.erb to render:
<% for issue in #active_issues %>
<div id="issue_<%= issue.id %>_display">
<%= render :partial => 'show_issue_mini', :locals => {:issue => issue} %>
</div>
<% end %>
When I try clicking the link in my original HTML view, nothing happens. When I open it up in a new tab, I get this error message:
Template is missing
Missing template support/show_user_issues,
application/show_user_issues with {:locale=>[:en],
:handlers=>[:builder, :erb], :formats=>[:html]}. Searched in: *
"/home/<>/app/views" *
"/home/<>/gems/kaminari-0.14.1/app/views"
The alert('test') that I put into show_user_issues.js.erb doesn't show up, so I think that Rails is getting hung up on rendering that file - that is, the routing mechanism can't find it. How can I correct this issue?
P.S. I double-checked that I put in all the file names exactly as they are in the code base.
Change your controller action to handle the type of request.
def show_user_issues
#target_div = params[:target_div] || "holding_issues_list"
user = User.find(params[:user_id])
issue_type = params[:issue_type]
#snip - set the value of #issues
respond_to do |format|
format.js
end
end
This will check the format of the request which is .js in case of :remote => true. So it will handle it by rendering the show_user_issues.js.erb file.
A couple other problems that I ran into after applying Manoj Monga's answer that I suspect other new Rails devs might run into:
In show_user_issues.js.erb, I had
[...].html("<%= escape_javascript render :partial => '_show_user_issues',[...]
The underscore before '_show_user_issues' caused the ERB builder to fail. It should have just been 'show_user_issues'.
In _show_user_issues.html.erb, I had
<% for issue in #active_issues %>
If you look closely at show_user_issues.js.erb, though, I named the variable #issues, not #active_issues:
[...]:locals => {:target_div => #target_div, :issues => #issues}[...]
So I changed the line in the HTML partial to
<% for issue in #issues %>
After these last couple changes, the new functionality I was adding worked as expected.
I know prawn is working because I have a show action that if I add .pdf on to the end of it loads show.pdf.prawn. However, I have a form:
<%= form_tag(:controller => "/holders", :action=> "generate", format: "pdf") do %>
<%= label_tag(:count, "How Many Students?") %>
<%= text_field_tag(:count) %>
<%= hidden_field_tag :holder_id, value: #holder%>
<%= submit_tag("Generate Course Lesson") %>
<% end %>
That submits count to the the generate action.
Inside my generate action I have the following:
def generate
prawnto :filename => "print.pdf"
respond_to do |format|
format.html
format.pdf { render :layout => false}
end
end
and my generate.pdf.prawn looks like:
pdf.text "HELLO WORLD"
When I submit the form I get the URL: http://localhost:3000/generate.pdf and Chrome tells me
Failed to load PDF document with no other errors or information.
I noticed I am getting: Rendered holders/generate.pdf.erb in my dev logs which indicates its not even looking for the .prawn file.
What am I doing wrong?
Setup your action like this:
def generate
respond_to do |format|
format.html
format.pdf { prawnto :filename => 'print.pdf' }
end
end
I followed instructions from the "AJAX file uploads in Rails using attachment_fu and responds_to_parent" article. In my case I am using Ruby on Rails 3 and Paperclip.
What I made is the following:
I have installed the 'respons_to_parent' plugin running this command in the Terminal:
rails plugin install git://github.com/itkin/respond_to_parent.git
After installing, I restart Apache.
In my view I have:
<%= form_for(#user, :html => { :multipart => true, :target => 'upload_frame' }) do |f| %>
<%= f.file_field :avatar %>
<%= f.submit "Submit" %>
<% end %>
<div id="test">Here is a test</div>
<div id="stuff">Here is some stuff</div>
<iframe id='upload_frame' name="upload_frame" style="width:1px;height:1px;border:0px" ></iframe>
In my controller I have
def action
respond_to do |format|
...
format.js {
responds_to_parent do
render :update do |page|
page.replace_html :test, "This is the resulting test"
page << "alert($('stuff').innerHTML)"
end
end
end
end
end
Trying to submit the form, all about the file uploading works (Paperclip handle correclty files, ...) and in the log file there aren't errors.
The only thing that isn't working is the AJAX part. In the example
page.replace_html :test, "This is the resulting test"
page << "alert($('stuff').innerHTML)"
don't update the page (BTW: where these should have effect? in the "main view" or in the "iframe view"?). It doesn't work also if I try to delete the 'respons_to_parent' statement or if I put them in the 'action.js.rjs' file.
Where I am wrong?
SOLVED
I had forgotten to add :url => users_account_path +. "js". So the view become:
<%= form_for(#user, :url => users_account_path +. "js", :html => { :multipart => true, :target => 'upload_frame' }) do |f| %>
I have an erb file named index that has a form in it. As a simple example:
<% form_for #foo, :url => {:action => 'bar'} do |f| %>
<%= f.submit "BAR!" %>
<%end%>
When I click the BAR! button it preforms the actions I expect and it forwards me onto the bar.erb file, displaying the expected output. What I would like to be able to do, however, is to take the generated html from this page and stuff it into the innerHTML of a div on the index page. I assume there is a way but I must ask, is there a way to achieve this? Are there any examples available that would be helpful? Thanks!
You should be able to pass the id of the div to update like so:
<% remote_form_for #foo, :url => {:action => 'bar'}, :update => 'id-of-div-to-update' do |f| %>
<%= f.submit "BAR!" %>
<%end%>
In the controller:
def bar
# your code here
respond_to do |format|
format.html { redirect_to(xxx) }
format.js
end
end
Rails will look for a template named bar.js and will render it and return it's content to the browser without a redirect.
So my background is in Java web services, but I'm trying to make the move to ROR.
I'm using FlexImage to handle image uploading and thumbnail generation. I followed the guide and CRUD behavior was working fine at one point. However, at some point, CRUD behavior for one of my models (Images) was broken.
The error code I'm getting back is as follows: ActiveRecord::RecordNotFound in ImagesController#show -- Couldn't find Image with ID=#<Image:0x4e2bd74>. In other words, when I'm telling Rails to create/update/destroy, it is confusing the object with the id. This seems to indicate there might be a routing issue. I thought adding a partial for images might have been the trouble, but rolling back the changes didn't fix it.
Following are the new, show and update methods of the controller for the Images model:
# images_controller.rb
# ...
def new
#image = Image.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => #image }
end
end
# ...
def show
#image = Image.find(params[:id])
respond_to do |format|
format.jpg # show.jpg.erb
format.html # show.html.erb
format.xml { render :xml => #image }
end
end
# ...
def create
#image = Image.new(params[:image])
if #image.save
redirect_to image_url(#image)
else
flash[:notice] = 'Your image did not pass validation.'
render :action => 'new'
end
end
# ...
Note that show() is, of course, expecting an appropriate id. Here's the new.html.erb for uploading a new image:
# new.html.erb [upload image]
<h1>New image</h1>
<% form_for #image, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<table><tr><td width="50%">
<p>
<%= f.label :filename %><br />
<%= f.text_field :filename %></p>
</td>
<td><p><b>Upload Image</b><br />
<%= f.file_field :image_file %><br />
or URL: <%= f.text_field :image_file_url %>
<%= f.hidden_field :image_file_temp %>
</td>
<td>
<b>Uploaded Image:</b><br />
<%= embedded_image_tag(#image.operate { |img| img.resize 100 }) if #image.has_image? %>
</td>
</tr>
</table>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', images_path %>
The relevant portion of routes.rb are as follows:
# routes.rb [excerpt]
map.resources :images
map.image 'images/:action/:id.:format', :controller => 'images'
Also note that a new image does actually get uploaded and the error is thrown on redirect to show (which is expecting a valid ID in params[:id] and not the object which for whatever reason it is being handed.)
Thanks for your help in advance, and please let me know if anything jumps out at you.
From looking at the code it appears to me that the problem may be caused by using image_url(#image) in combination with the non-RESTful image route.
You will probably want to remove the line
map.image 'images/:action/:id.:format', :controller => 'images'
from your routes.rb.
The line
map.resources :images
should actually be enough to expose all CRUD actions in your ImagesController.
My suggestion is to use ruby-debug and set a break point right before the Image.find call. Inspect params[:id] and see what it actually is.
A more ghetto approach, put this before the Image.find call
logger.info params[:id].class
and see what is in that variable. Is it possible that you have some sort of before filter that is manipulating it?
try
redirect_to :action => "show", :id => #image
I think that's a more idiomatic way to code the redirect. And +1 to molf's advice about RESTful routes.