How do I link_to VIEW a pdf resource (NOT download) in a custom engine on RefineryCMS? - pdf

I'm a novice RoRr and I'm building my company's website on RefineryCMS.
I've properly created an engine for my Boss' publications, within which I included a pdf:resource column and have uploaded all the articles to my backend. Everything works - renders properly and downloads work.
HOWEVER, I also want to be able to view those pdf's as a _blank target through a link_to on the article.title. SEE CODE BELOW !!!XXXXXXXXX_path , or any useable variation, is what I'm looking for!!!
The important thing is that the corresponding PDF is being rendered. That is, I know I can't manually code individual links while using the [.each do] loop.
I'm sure there is a way to get this done but, as I said, I'm a novice and could use some help.
here's the code:
<section id="body_content">
<section id="body">
<section id=articles>
<% #articles.each do |article| %>
<div class=wrapper>
<div class=particle>
<h1><%= link_to article.title, **(XXXXXXXXXXXXXXX.pdf)**, :target => '_blank' %></h1>
<p><span class=publisher><%= article.publication %></span>,
<span class=year><%= article.year %></span>
</p>
<% if article.pdf.present? %>
<%= link_to image_tag("/assets/pdf.png", :size => "30x30"), article.pdf.url, :title => "PDF download", :class => "pdflink" %>
<% else %>
pdf
<% end %>
</div>
<% end %>
</div>
</section>
</section>
Any help is much appreciated.
*SIDE NOTE: my next task is to add a link to email the article (as an attachment) to a third party. Again, I'd imagine there's something in ActionMailer that can help with this but any help before I get started would be great.
much obliged.

Sorry I read your question wrong. Try this instead
To indicate to the browser that the file should be viewed in the browser:
Content-Type: application/pdf
Content-Disposition: inline; filename.pdf
To have the file downloaded rather than viewed:
Content-Type: application/pdf
Content-Disposition: attachment; filename.pdf

Related

RefineryCMS Custom Layout Template works in host app but not with engine

So I followed the refinery guide to create a custom layout template. I switched my about page to use my new template in the advanced options and it worked like a charm.
Then I created a new engine called clients and selected the new template in the advanced options of the clients page, which as you know defaults to clients - index.html.erb.
But the engine ignores my template. Why? I know I set it up correctly because it works in about page. The difference is that the about page is not part of a refinery engine and the clients page is. I found this SO question and have tried setting my engine's index.html.erb like so
<% content_for :body %>
<ul id="clients">
<% #things.each do |t| %>
<li>
<%= link_to t.name, refinery.ts_t_path(t) %>
</li>
<% end %>
</ul>
<%= render :layout => 'layouts/client' %>
to no avail. Any help would be much appreciated. Thanks!

How do I update a list of partials rendered with a collection after a form submission of a newly created object in ruby on rails

This is a portion of my groups view. I have a list of partials that I render with a collection of objects:
<div id="container">
<ul>
<li id="form"></li>
<li id="partials">
<%= render :partial => 'groups/partial-list-row', :collection => #allpartials, :as => :f %>
</li>
</ul>
</div>
In the list item with id "form" there is a ruby on rails form that is a form_for #newpartial which is defined in the Partial controller as #newpartial = Partial.new(). I have successfully used jquery to toggle the show and hide of the two list items on a "New Partial" button click, and upon completing a finished form, a new partial object is definitely created. What I'm looking for here is the collection of partials that reappears after the form submission includes the newly created partial object. What is the best way to do this? My form which sits inside the list item with id "form" looks like this:
<%= form_for #newpartial, :html => {:multipart => true, :class => 'custom'}, remote: true do |f| %>
<% if #newpartial.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#newpartial.errors.count, 'error') %> prohibited this partial from being saved:</h2>
<ul>
<% #newpartial.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= render :partial => 'partials/form-partial', :locals => { :f => f } %>
<div class="actions">
<%= f.submit 'Launch', :class => 'large success button radius new_browser_tab' %>
<a class="cancel_campaign_new">Cancel</a>
</div>
<% end %>
I also have successfully created a function that is bounded to the "ajax:success" event of this form after the DOM is fully loaded. I just don't know if this is a right approach, and if it is, I don't know what the body of this event handler should consist of. I have created other questions relating to this same issue (I haven't found a solution to this for quite some time now). Here are the questions if you want additional context:
SO question 1
SO question 2
What I was failing to realize is that applying the remote: true on the form indicates that the controller's action method will respond with the models "action".js.erb file. After creating that file and performing the jquery I had always intended, I was able to fix a few kinks and got that part working.
The difference between putting the jquery to render the new partial in your client side javascript and your "action".js.erb file is that the "action".js.erb file is able to make the required request to the server, whereas your client side javascript is not.
Can someone read this and let me know if the above statements are accurate? I'd really like to provide an answer that is accurate as possible for anyone that comes across this...
There are some really good tutorials at CodeSchool.com which has some free courses to take that include video lectures, slides, and exercises that I used to finally give me enough guidance on this. Also worth mentioning that if you intend to learn this stuff quickly and plan on spending a lot of time developing your web development abilities, paying the 20 dollars a month for access to all the courses is well worth it (you can cancel at any time, so learn what you need to learn and move on!).

Changing Error Message in Rails Model

I have a Rails (3.1) app with a model validation that, when triggered, puts the model and field name before the message, e.g.:
Profile image profile image content type Only jpeg, gif and png files are allowed for profile pictures
Is there a way to avoid that, so it reads:
Only jpeg, gif and png files are allowed for profile pictures
model.rb validation:
validates_attachment_content_type :profile_image,
:content_type => ['image/jpeg', 'image/png', 'image/gif'],
:message => "Only jpeg, gif and png files are allowed for profile pictures"
The error appears as a part of this code in my layout:
<% if object.errors.any? %>
<div class="alert alert-message error" data-alert="alert">
<a class="close" data-dismiss="alert">×</a>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
My hunch is that msg is not actually the message but the entire error hash, and so calling <%= msg %> actually converts the whole hash to a string, including the keys. You could confirm this with <%= msg.class %>.
Assuming the view code you posted is a partial it would be helpful to see the view that includes the partial. If it's not a partial it would be useful to see the surrounding code.

partial local only accessible using local_assigns, not exposed by name

I am passing two locals from a view ERB to a partial. Both locals are successfully passed in local_assigns. However, I am only able to use the FormBuilder via a local variable name in the partial. The other value is usable within my partial as local_assigns[:disable_edits], but not as disable_edits.
_form.html.erb
<div>
<%= f.fields_for :panels do |builder| %>
<%= render "panel_fields", :f => builder, :disable_edits => true %>
<% end %>
</div>
_panel_fields.html.erb
<div>
<p>
<%= local_assigns[:disable_edits] %>
</p>
<p>
<%#= disable_edits ? 'disable edits true' : 'disable edits false' %>
</p>
<p>
<%= local_assigns.keys %>
</p>
local_assigns[:disable_edits] results in "true" being displayed.
local_assigns.keys results in "[:f, :disable_edits, :panel_fields]" being displayed.
Uncommenting the ternary statement results in "undefined local variable or method `disable_edits' for #<#:0x4d58768>"
I am following what I understand is the latest suggested Rails syntax, but have tried manipulations using :partial=>, :locals=>, :as=>, etc. to no avail. I also do not think I've made this an optional argument in any way, so a lot of the information about testing with has_key vs. nil? isn't helping me. My understanding is everything in local_assigns should be exposed as in the partial as a local variable with the same name as the hash key.
I'm getting by for now using local_assigns[:disable_edits], but would like to understand what is happening and correct things so I can use more conventional syntax. Thanks!
try using
render :partial => "panel_fields", :locals => {:f => builder, :disable_edits => true }

tinymce issue with rails 3

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