Rails 4 Asset Pipeline link - ruby-on-rails

While using the asset pipeline in production and trying to keep the folder less cluttered, I have set up nested folders. I am trying to link to a pdf within a sub-folder, but I am not sure how to call the file precisely.
<%= link_to ' | PDF', asset_path(pub.file), :target => "_blank" %>
This works in development. For production I will need to call pubfiles/pub.file, but that syntax seems like it is missing something.
I figured out it should be something like this:
<%= link_to ' | PDF', asset_path("pubfiles/#{pub.file}"), :target => "_blank" %>
But when I try to use the link I error out with:
Missing template people/show, application/show with {:locale=>[:en], :formats=>[:pdf], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/rails/neuro/app/views"
def find(*args)
find_all(*args).first || raise(MissingTemplate.new(self, *args))
end
def find_all(path, prefixes = [], *args)
In development without the folder argument setup, the pdf opens without a hitch.

When using asset_path, rails will prepend /assets/ to the path you specified as its argument.
Thus make sure your sub-folder is within assets folder this way:
-public
-assets
-yourFolder
-yourFile.ext
That way, you can use asset_path as asset_path('yourFolder/yourFile.ext') and rails will serve your file at /assets/yourFolder/yourFile.ext
Please notice that rails adds fingerprint to files in order to favor caching. Make sure your assets is properly compiled and configuration for production is properly defined.

Related

Missing template error for Ruby on Rails Application

I have a view in the LoginController with the following code
def home
render 'home'
end
However, whenever I load my web server, it shows the error message:
ActionView::MissingTemplate (Missing template login/home, application/home with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in:
* "C:/Users/60984/prairielearn-eecs/app/views"
* "C:/Ruby30/lib/ruby/gems/3.0.0/gems/actiontext-6.1.7.2/app/views"
* "C:/Ruby30/lib/ruby/gems/3.0.0/gems/actionmailbox-6.1.7.2/app/views"
):
app/controllers/login_controller.rb:16:in `home'
Even though I have the file in the directory app/views/login/home.html.erb.
I tried changing it to a more specific directory such as
def home
render 'login/home'
end
but it still fails. However, I tried using the whole path like
render file: 'C:/Users/.../app/views/login/home.html.erb'
With the code above, the problem changed into the html.erb is not being interpreted, it will display the code blocks on the browser no matter what browser I use.
I am just confused about why it doesn’t work since I have the html file in the designated place.

Template missing in render :file in ajax call when the file exists in Rails 4.2

Ajax is used for Bootstrap modal call. The js.erb file is called successfully and this js.erb file then should load html.erb residing in the same subdir as the js.erb is under biz_workflowz/app/views/application/. Here is the js.erb file:
$("#newworkflow .modal-content").html('<%= j render(:file => "/biz_workflowx/application/event_action.html.erb") %>');
$("#newworkflow").modal();
However the event_action.html.erb is never found. The error is raised in action_view/path_sets.rb:
def find(*args)
find_all(*args).first || raise(MissingTemplate.new(self, *args)) #find_all(*args).first returns NIL
end
The error is:
ActionView::Template::Error (Missing template c:/d/code/rails_proj/engines/biz_workflowx/app/views/application/event_action.html.erb with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee]}. Searched in:
.......
* "C:/D/code/rails_proj/engines/biz_workflowx/app/views" #<<== did search the subdir but did not find anything
* "C:/D/code/rails_proj/engines/searchx/app/views"
* "C:/D/code/rails_proj/engines/commonx/app/views"
* "C:/D/code/rails_proj/engines/authentify/app/views"
* "C:/D/code/rails_proj/webportal"
* "C:/"
):
1: $("#newworkflow .modal-content").html('<%= j render(:file => "biz_workflowx/application/event_action.html.erb") %>');
2: $("#newworkflow").modal();
actionview (4.2.0) lib/action_view/path_set.rb:46:in `find'
Even with hardcoded path to the file event_action.html.erb, it still returns template missing. The similar js.erb code has been used in a few places for ajax call and I does not see why here it does not work. What could cause this error?
Updated
Here is the debug windows showing the biz_workflowz/app/views has been searched by resolver which does not see the file under /application/. Strange!
add event_action.html.erb file under c:/d/code/rails_proj/engines/biz_workflowx/app/views/application

Rails 5 render a template located outside of the 'views' directory

The following code
def show
render File.join(Rails.root, 'app', 'themes', 'default_theme', 'template'), layout: File.join(Rails.root, 'app', 'themes', 'default_theme', 'layout')
end
Works in rails 4.2.x but outputs the following error when using Rails 5.0.0
ActionView::MissingTemplate:
Missing template vagrant/app/themes/default_theme/template with {:locale=>[:nl], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]}. Searched in:
* "/vagrant/app/views/themes/default_theme"
* "/vagrant/app/views"
* "/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/devise-ebe65b516b38/app/views"
* "/home/vagrant/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-i18n-1.1.0/app/views"
It looks like it is searching only inside the 'app/views/*' directory. Is there a way to render a template located outside of the 'views' directory using rails 5.0.0, so it works just like it used to in previous Rails versions?
Start by appending the view path:
class ApplicationController
prepend_view_path( Rails.root.join('app/templates') )
# ...
end
You can then render templates in app/templates by calling:
render template: 'default_theme/template',
layout: 'default_theme/layout'
Using the template option tells rails to not append the path with the name of the controller.
You have to add the new folder to your configuration:
config.paths["app/views"].unshift(Rails.root.join("/vendor/myapp/views/myctrl").to_s)
Or your controller:
before_filter {
prepend_view_path(Rails.root.join("vendor/myapp/views/myctrl").to_s)
}
source

In rails what else should you do to render a file in footer?

I am a beginner in rails and I have to add a footer to my site.
I just copied the line in the application.html where it renders a file
= render :partial => "shared/navigation"
and put my file
= render :partial => "shared/footer"
Then in the shared folder I created my file _footer.html.haml and put my html code there.
But the site gives me the following error:
ActionView::MissingTemplate at /
Missing template shared/footer with {:locale=>[:en], :formats=>[:html]...
What else should I do so that rails can fetch that file like any other file from the shared folder?
Thanks!
Edit: Full error is this one:
ActionView::MissingTemplate - Missing partial shared/footer with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml, :rjs]}. Searched in:
* "/home/username/workspace/myapp/app/views"

ActionView::MissingTemplate: Rails not looking for JSON format

I'm using Backbone.js and thus bootstrapping data using a JSON (jbuilder) partial like so (using HAML):
App.users = new App.UserList(#{render('users/index', :formats => [:json], :handlers => [:jbuilder], locals: {users: #users})})}, {silent:true});
It gives me this error:
ActionView::Template::Error (Missing partial users/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :jbuilder, :arb, :coffee, :haml]}. Searched in:
Notice that it is only looking for the ':html' format, despite me passing in 'formats: [:json]'. Should I be doing something differently?
Thank you for any help.
What version of Rails are you using? This problem was fixed for 3.2.3, but exists in earlier versions.
For a quick fix, though it will cause deprecation warnings in Rails 3.2 and later, you can add the format to the name of the template, i.e. render('users/index.json' ...

Resources