I'm attempting to have fancybox display a pdf in a iframe, right now it currently just downloads the pdf to the users computer, I'd like to have it viewable in the browser inside the modal/fancybox.
current view is:
<% for issue in #issues %>
<tr>
<td><%= issue.date %></td>
<td><%= link_to "view pdf", issue.pdf_url, :class => "fancybox" %>
<td><%= link_to "Show", issue %></td>
<td><%= link_to "Edit", edit_issue_path(issue) %></td>
<td><%= link_to "Destroy", issue, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
I figure there is a way i could use the google doc viewer found here but i'm not entirely sure how i would create the link? and how would i test locally since the viewer would obviously not be able see the pdf if i'm running from a localhost.
What solutions are available for rails? I'd like to host the pdfs locally.
Also is there a way to use rmagick to generate a png or jpg of the 1st page to use as a preview?
Update:
I went ahead and tried using this as my link however it pulls an error.
<%= link_to "view pdf", 'http://docs.google.com/gview?url=#<%= issue.pdf_url %>', :class => "fancybox" %>
error is
/app/views/issues/index.html.erb:10: syntax error, unexpected $undefined, expecting ')'
...);#output_buffer.safe_concat('\', :class => "fancybox" %>
... ^
you cant use erb code inside erb.
Use as following:
<%= link_to "view pdf", 'http://docs.google.com/gview?url=#{u(issue.pdf_url)}', :class => "fancybox" %>
url encode issue.pdf_url
Related
I am developing a web app where people can post new questions. Each question contains many sub-questions, and each sub-question can have many images. To post new questions, I use a Rails plug-in called 'nested-form', and to add pictures I use Paperclip.
Here is my question. When I add new questions, everything works fine. But when I edit the question, the fields for the pictures are still there but the picture directories in the fields are gone. If I submit the form with the blank fields, all the pictures would be gone. Therefore, I have to upload all the pictures again when I edit a question, even if I don't want to do any change to the pictures.
Here is my code:
new_question.html.erb:
<%= nested_form_for #question, :url => {:action => 'create_question'} do |f| %>
<%= f.label :name # some fields for #question %>
<%= f.text_area :name %>
<%= f.fields_for :subquestions do |builder| %>
<%= render 'subquestion_fields', :f => builder %>
<%= builder.link_to_remove 'Remove Sub-question' %>
_subquestion_fields.html.erb:
<%= f.hidden_field :_destroy %><br>
<%= f.link_to_add 'Add an image', :qimages %><br>
<%= f.fields_for :qimages, :html => { :multipart => true } do |builder| %>
<%= render 'qimage_fields', :f => builder %>
<%= builder.link_to_remove 'Remove this image' %><br>
<% end %>
_qimage_fields.html.erb:
<%= f.file_field :image %>
edit_question.html.erb uses the same partials above, but the picture that are already added to the question are not displayed(leaving blank 'qimage' fields). Is there a way to display all the images when editing the question?
Thank you!
Sorry. I just found out that there is a solution on Ryanb's Nested Form page. It is possible to track the event that generates new fields by listening to the nested:fieldAdded event, and the generated elements can be found by via event.field.
I am working with the GoCardless API in my Rails 3 application. I have a subscription model which has a resource_id column which matches the id of the remote record.
I am trying to get extra information for a local subscription record from the API.
Subscription Index View
<% #subscriptions.each do |subscription| %>
<tr>
<td><%= subscription.resource_id %></td>
<td><%= subscription.resource_type %></td>
<td><%= subscription.signature %></td>
<td><%= subscription.state %></td>
<td><%= #gocardless.next_interval_start %></td>
<td><%= link_to 'Show', subscription %></td>
<td><%= link_to 'Edit', edit_subscription_path(subscription) %></td>
<td><%= link_to 'Destroy', subscription, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
Subscription Controller
def index
#subscriptions = Subscription.all
#gocardless = GoCardless::Merchant.find("XXXXXXXXXX").subscriptions
respond_to do |format|
format.html # index.html.erb
format.json { render json: #subscriptions }
end
end
As you can see I'm using the following line to retrieve the next interval start date
<%= #gocardless.next_interval_start %>
This doesn't work and returns nil. However, if I change it to the following all records show the value from the first record from the API.
<%= #gocardless.first.next_interval_start %>
According to the GoCardless API documentation you can lookup a subscription using the following:
GoCardless::Subscription.find("XXXXXXXXXX") # => #<GoCardless::Subscription ...>
So, my question. How do I pass the current subscription resource_id to the API so that I can view the next_internal_start value for each of the local subscription records?
Update
Using Adam's example below I get a 404 response from GoCardless. I think it's due to me not setting the merchant ID in the request. When I change the method to:
def gc_subscription
#gc_subscription ||= GoCardless::Merchant.find("MERCHANTID").subscriptions.find(self.resource_id)
end
I get undefined methodinterval' for #` for any attribute I try and request using the following code:
<% for subscription in #subscriptions %>
<%= subscription.gc_subscription.amount
<% end %>
When using the same code to get local content it works as expected:
<% for subscription in #subscriptions %>
<%= subscription.resource_id %>
<% end %>
I've checked that the value I am trying to retrieve is available in the API. https://sandbox.gocardless.com/docs/api_guide#resources-available
Update Two
As before calling the first result works using Adam's method:
<%= subscription.gc_subscription.first.amount %>
This returns 2.5 which is the value of the first subscription.
If I had a local Subscription model which linked to a remote data source I would have a method like this on my Subscription model which provided access to the remote object:
def gc_subscription
#gc_subscription ||= GoCardless::Subscription.find(self.resource_id)
end
Then you can access this method as necessary without any need for any GC logic in the view or controller. For example:
for subscription in #subscriptions
subscription.gc_subscription.some_method #=> "output"
end
Or, if you are only working with one subscription:
subscription = Subscription.find(id)
subscription #=> Your local Subscription instance
subscription.gc_subscription #=> GoCardless::Subscription instance
With it configured like that, you will be able to remove the #gocardless from your controller and replace your view with this:
<% #subscriptions.each do |subscription| %>
<tr>
<td><%= subscription.resource_id %></td>
<td><%= subscription.resource_type %></td>
<td><%= subscription.signature %></td>
<td><%= subscription.state %></td>
<td><%= subscription.gc_subscription.next_interval_start %></td>
<td><%= link_to 'Show', subscription %></td>
<td><%= link_to 'Edit', edit_subscription_path(subscription) %></td>
<td><%= link_to 'Destroy', subscription, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
I hope that helps.
I have legacy rails 3 app, where I need to modify a page to use tinymce to edit a text_area.
There are existing pages in this app that already use tinymce.
For reasons I cannot go into here, I cannot use any of the tinymce plugins that are available.
Now my problem is as follows.
I have a model called Sections, that has two attributes, section_name and html.
I want to be able to edit the html using tinymce.
My view has a form which is as follows
<%= form_for #section , :url => update_section_path , :method => :put do |f| %>
<%= f.hidden_field :id %>
<%= f.label :section_name , "Section Name" %>
<%= f.text_field :section_name %> <br />
<%= f.label :html, "Html" %>
<%= f.text_area :html %>
<%= f.submit "Update" %>
<% end %>
The form appears as expected.
The TinyMCE editor also appears on the page with the original html.
The problem is that when I click on the Update button, the put query sent to my server, does not contain the new modified content of the Html text_area. It sends back the original text that was in that text_area.
Could anyone help me understand why.
Thanks in advance
=Puneet
I found an issue in my html which was causing this. Once I fixed that issue it worked fine
I have a form that takes date range value to generate a report with Jasper Reports
reports/statistic.html.erb
<%= form_tag("/reports/statistic", :method => "post", :target => "_blank") do %>
<%= label_tag(:from_date, "From Date:") %>
<%= text_field_tag :from_date %>
<%= label_tag(:to_date, "To Date:") %>
<%= text_field_tag :to_date %>
<br><br>
<%= submit_tag("Generate Report") %>
<% end %>
and this is reports_controller.rb
def statistic
#details=StatisticTable.where(:dateindb => (params[:from_date])..(params[:to_date])
send_doc(render_to_string(
:template => 'reports/statistic.xml', :layout => false), #source of xml and template
'/statistic/detail', #xml xpath2 query in reports
'statisticreport', #name of .jasper file
'StatisticReport', #name of pdf file
'pdf')
end
When I clicked Generate Report button, the report displays nicely in a pdf viewer in a new window. But when I try to save the pdf file, the pdf is empty and the date value is returned as null.
Also, I followed this tutorial http://oldwiki.rubyonrails.org/rails/pages/HowtoIntegrateJasperReports which explains where the send_doc method comes from.
I don't think the problem is in Jasper because if I replace this
#details=StatisticTable.where(:dateindb => (params[:from_date])..(params[:to_date])
with a pre-defined date value
#details=StatisticTable.where(:dateindb => ('2011-12-01')..('2011-12-31')
the report displays and saves perfectly. So I'm guessing there is something wrong with my Ruby on Rails variables setting?
Thanks!
I solved this by only changing form method to GET
<%= form_tag("/reports/statistic", :method => "get", :target => "_blank") do %>
<%= label_tag(:from_date, "From Date:") %>
<%= text_field_tag :from_date %>
<%= label_tag(:to_date, "To Date:") %>
<%= text_field_tag :to_date %>
<br><br>
<%= submit_tag("Generate Report") %>
<% end %>
and after report is generated in PDF, I clicked Save and the PDF saves the whole data perfectly.
My view:
<h1><%= #territory.name %></h1>
<%= link_to 'List of Territories', territories_path %>
<%= render 'shared/address_form' %>
<table>
<tr>
<td><strong>Name</strong></td>
<td><strong>Street</strong></td>
<td><strong>District</strong></td>
<td><strong>Note</strong></td>
<tr>
<%= #addresses.each do |address| %>
<tr>
<td><%= address.name %></td>
<td><%= address.street %></td>
<td><%= address.district %></td>
<td><%= address.note %></td>
</tr>
<% end %>
</table>
The form I render here is:
<%= form_for [#territory, #new_address] do |f| %>
<div>
<p>
<%= f.label :address %><br />
<%= f.text_area :address %>
</p>
</div>
<div class='file-wrapper'>
<%= f.submit "Submit" %>
</div>
<% end %>
Here is the territories controller, where the instance variable addresses is defined:
class TerritoriesController < ApplicationController
def index
#territories = Territory.all
end
def show
#territory = Territory.find(params[:id])
#new_address = #territory.addresses.build
#addresses = #territory.addresses
end
.
.
.
Why is Rails displaying
#<Address:0x7e088224>#<Address:0x7e0881d4>#<Address:0x7e088134>#<Address:0x7e088094># <Address:0x7e087ff4>#<Address:0x7e087f54>#<Address:0x7e087eb4>#<Address:0x7e087e14>#<Address:0x7e087d74>#<Address:0x7e0bce48>
after the form and before the table?
Thanks
Thomas
Check your layouts (app/views/layouts/*). Most likely you have included some ERB code in the one that is being rendered with this page that displays these addresses. Is that the full code of your view?
Edit: I found your solution. Right now, you have <%= #addresses.each ... %>. The each method runs the block on all elements, and then returns the list of elements. You do not want this code to be displayed. Remove the = so that <%= is just <%
You have some view code somewhere (in a layout or a view helper) that is implicitly calling the to_s method of your Address model instances. Look for something like <%= #address %>.
As you have seen, the non-overridden behaviour of the to_s method is to output the memory address of the object instance.
Those are not memory addresses. Those are instances of your Address class. If you'd override the to_s method in that class, you'd see that output there instead. And the reason you see those object printed out is your use of <%=. Changing this line
<%= #addresses.each do |address| %>
to this
<% #addresses.each do |address| %>
should fix it.
First: I can see no form in your view.
Second: Your view looks ok.
Have a look at your layout files.