Can't render a rails partial inside CoffeScript file - ruby-on-rails

I try something like this on CoffeScript file located the assets pipeline.
<%= escape_javascript(render partial: "shared/mypartial")%>
But I get the following error
throw Error("NoMethodError: undefined method `render' for #<# <Class:0x007fb68f801a40>:0x007fb68d3c3fe8>\n (in /Users/user/Sites/app/app/assets/javascripts/application/app.js.coffee.erb)")
Any idea why is not getting the render method?

Coffeescript files aren't processed within the context of a request, therefore you won't be able to access the render method.

Related

How to use a rails helper in a PDF template inside services layer?

I have a PDF template and I need to use a view helper method inside a HTML file that is in the services folder to generate the PDF. However, when I try to use the default view helpers, I get an exception that the method doesn't exist.
ActionView::Template::Error: undefined method `helper_method' for #<#<Class:0x000000000e309bd0>:0x000000000e3a0350>
How can I make this work?
You can make your module methods work as they were class methods by using
module_function :method
The code looks like this:
module Helper
def helper_method; end
module_function :helper_method
end
Then in your html.erb you do this:
<% Helper.helper_method %>

Rails: Calling Render From Within a Generator

I've just started experimenting with generators. In one of my generated view templates, I want to call render like so:
index.js.slim
transition("#main", "<%= escape_javascript(render 'index') %>");
When I try using the generator, I get this error:
(erb):1:in `template': undefined method `render' for #<Slim::Generators::ScaffoldGenerator:0x000000041b2a20> (NoMethodError)
Is Rails really incapable of calling render from within a generator? Or am I doing something wrong?
Railscast 218 goes into more detail:
The first thing to note is that because we’re using the template
method, all of the erb tags in the code will be executed when the
generator runs. If we want to include any erb in the generated file
we’ll have to escape the percent sign at the beginning of each erb tag
and we’ve done that for most of the erb code above.
In this instance:
transition("#main", "<%= escape_javascript(render 'index') %>");
should become:
transition("#main", "<%%= escape_javascript(render 'index') %>");
All it needed was an extra % to escape the erb.

How to render partial in HAML that only contains a conditional block

I'm new to Ruby and HAML and attempting to render the following partial inside of main HAML file. The partial consists of only an if conditional. I would like the if conditional to return the output of the partial's HAML code to the main template if the condition is met, and to render nothing if it isn't. The following code works if the array "attachments.each_file" is empty (it renders nothing as I would like), but if it isn't empty it throws an error when it tries to proceed into the if conditional's code. Here are the relevant code snippets:
Error Message:
LocalJumpError in Questions#show
Showing /questions/_attachments.html.haml where line #1 raised:
no block given (yield)
Main HAML template code:
= render "slashbias/questions/attachments", :attachments => #question.attachments, :editing => false
Partial HAML code:
- if !attachments.each_file.empty?
%dl#attachments_list
%dt.header Attached files:
%dd
-attachments.each_file do |key, file|
= link_to file.name, question_attachment_path(question.group, question, file, key)
-if editing
= link_to t("scaffold.destroy"), remove_attachment_question_path(question, :attach_id => key), :class => "remove_attachment_link"
I believe attachments.each_file on line 1 is expecting a block. It doesn't throw an error in the case of having 0 files, because it never attempts to yield anything. But in the case that there are files, each_file is trying to yield the files to a block and raising the error you're seeing because there is no block to yield to.
Is there another way for you to test if there are any files? Something like !attachments.files.empty? or attachments.files.count > 0?

ActionView::TemplateError (Missing template) In ruby on rails

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

render erb from database into view problem please help!

i am saving some erb in my database and rendering it in the view like this:
erb = ERB.new(content)
render :text => erb.result
I am getting errors when trying render erb that has the image_tag in the erb saved in the database. The error is :
undefined method `image_tag' for main:Object
Anyone help on this ? i also get the error with the stylesheet_link_tag ?
Thank alot
rick
I think that you would need to pass the optional binding parameter to the ERB::render method. This effectively provides the local variables in the scope of the ERB template. In other words the binding needs to provide the image_tag variable to the template.
I don't know what 'content' is in your case but the following will pass the binding from the 'parent' view assuming that #obj.image_tag is visible from that view:
<%= ERB.new("image tag - \<\%= #obj.image_tag \%\>").result(binding) %>
It's because you haven't helper in your controller. You need include all Helper do you use.

Resources