ActionView::TemplateError (Missing template) In ruby on rails - 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

Related

Rails js.erb response not sending locals to Haml partial

I realize this is similar to some other questions, but I have been through a fair number of those and I think I'm doing what they suggested, but my code is still not working.
So, as the title says, I'm attempting to load a partial from a js.erb file (in response to an AJAX call.) However, it is not working correctly.
full_list.html.haml:
=link_to(test_path(#work), id: "privacy-button", remote: true) do
= render "privacy_button_content", locals: {:show_others => #work.show_others}
_privacy_button_content.html.haml:
-if locals[:show_others] == false
-test_string = "twas false"
-else
-test_string = "twas true"
%p = test_string
works_controller.rb:
def test_function
#work.model_function
respond_with(:layout => false)
end
test_function.js.erb:
$("#privacy-button").html("<%= escape_javascript render(:partial => 'privacy_button_content', locals: {:show_others => #work.show_others}) %>");
So the full_list has privacy_button, which is rendering the partial, and the response to clicking the #privacy-button should be to modify something in the controller, and get the response.js.erb.
This works correctly on page load (so passing the local to the partial does work,) but whenever I run the AJAX call I'm getting
(undefined local variable or method `locals' for #<#<Class:0x00000005006338>:0x000000068481f8>):
Any help would be appreciated.
Update _privacy_button_content.html.haml as below:
-if show_others == false
-test_string = "twas false"
-else
-test_string = "twas true"
%p = test_string
When you pass show_others in locals hash, a local variable would be created for you named show_others. You can access it as show_others in the partial and not locals[:show_others].
Refer to Passing local variables in Rails Guides.
Apply Kirti Thorat's change and then change
= render "privacy_button_content", locals: {:show_others => #work.show_others}
for
= render "privacy_button_content", show_others: #work.show_others
Also remember that views should not have logic, it should be in a helper or in a decorator
BTW: I think %p = test_string should be %p= test_string
So, I've figured it out. It seems that specific error I was getting had to do with the render syntax when working when haml (I got the same issue when I specified render :partial from the view, so it wasn't unique to the js.erb.)
The code in partial.js.erb now looks like:
$("#privacy-button").html("<%= escape_javascript render("privacy_button_content", :locals => {:show_others => #work.show_others}) %>");
As you can see, the main difference is having render "works_group_list" rather than render( partial: => "works_group_list". As I said, I believe this may be related to my views being written in Haml, though it should be noted that the render(partial:...) syntax does work when not attempting to pass local variables from a js.erb.
I was also getting some little issues because I kept referring to something as #my_element in the js when in the html it was #my-element. So if you're encountering this issue, checking your spelling (for the thirtieth time) never hurts.
Edit:
Just a note, as others have pointed out using the locals hash is not mandatory. So the line of code could instead be
$("#privacy-button").html("<%= escape_javascript render("privacy_button_content", :show_others => #work.show_others ) %>");
This will work as long as you update your partial appropriately (referring to var instead of locals[:var]).

wicked_pdf with rails 3.1 and ruby 1.9.2

First of all i know this type of posts are already been made but i've tried almost all of them and i'm not been able to get the results so here i'm again posting the same kind of question.
Second of all i'm not having an asset pipeline issue here, so please forget about that.
Now let me explain what i'm doing. I'm using rails 3.1 and ruby 1.9.2, i installed wicked_pdf as a gem and installed wkhtmltopdf as mentioned in the wiki by purging the already installed wkhtml and downloading and extracting the new one to /usr/bin/wkhtmltopdf
I have an initializer that contains the following:
wicked_pdf.rb
WickedPdf.config = { :exe_path => '/usr/bin/wkhtmltopdf'}
In my view i have a link_to method as follows:
_filters.html.haml
= link_to 'show pdf', jobs_report_jobs_path(:format => :pdf), :method=>"post"
note if i remove the :format => :pdf option it works fine
in my controller i'm doing the following:
report_jobs_controller.rb
respond_to do |format|
format.html
format.js
format.pdf{
render :pdf=>"jobs",
:template => 'jobs.html.erb',
:layout=>"jobs.html"
}
end
note that i have tried from format.pdf alone without any options. I tried "jobs.pdf.erb", with and without layout option, all sorts of other options i don't even remember. All i get is a 406 not acceptable in the end.
Please guide me coz i need to implement this feature asap.
Regards,
406 means the request isn't valid (in regards to what is acceptable to that controller action)
I've had trouble with :format => :pdf before. Try :format => 'pdf'
The barebones implementation should just be:
format.pdf {
render :pdf => 'jobs'
}
Also, is the link_to really supposed to be :method => "post"?
i had a before_filter in my application controller that was checking every request with the mentioned format for authentication and i was missing pdf format there, so as soon as i put the :pdf in the list of formats for each incoming request it worked fine.
I too had the same issue. I work on ubuntu. AFter I installed wkhtmltopdf, I am not getting this error any more.
in terminal run the command below:
$sudo apt-get install wkhtmltopdf
I hope this helps :)
I don't how much about wicked-pdf, but I once used pdfkit and this is how I did the rendering part:
def pdf
respond_to do |format|
format.pdf { render :text => PDFKit.new( Pdf.find(params[:id]).content ).to_pdf }
end
end
I hope the code is clear enough and self-explanatory. My view code is:
<p><%= link_to "Download PDF", pdf_pdf_path(#pdf, :format => "pdf") %></p>

Basic Ruby on Rails AJAX Error

I'm working through Agile Web Development with Rails, Edition 4 with some tweaks (mostly just naming variations), and I've arrived at Iteration F2. In this iteration, you modify the index button with :remote => true, you add format.js to the respond_to section of the controller, and you generate a js.rjs file to execute the AJAX render. Or at least that's my interpretation of it. The goal of these steps is to have a cart (in this case, a team) in the sidebar update using AJAX when adding new line items (in this case, members)
In my case, I'm trying to add members to a team. Her's some code snippets I've added:
index.html.erb:
<%= button_to 'Add to Team', members_path(:player_id => player),
:remote => true %>
members_controller:
def create
#team = current_team
player = Player.find(params[:player_id])
#member = #team.add_player(player.id)
respond_to do |format|
if #member.save
format.html { redirect_to(nba_url) }
format.js
format.xml { render :xml => #member,
:status => :created, :location => #member }
else
format.html { render :action => "new" }
format.xml { render :xml => #member.errors,
:status => :unprocessable_entity }
end
end
end
create.js.rjs:
page.replace_html('team', render(#team))
The page is able to render, and I'm still able to click the button to add members to the team. However, the AJAX isn't working. When I reload, I can still see that the members have been added in the sidebar. All of the other team functionality remains, as I'm able to empty the team and add whichever members I wish. When I check the server log, I find the following error:
Error:
ActionView::Template::Error (undefined local variable or method `page' for #<
#<Class:0x413e1b8>:0x413cb20>):
1: page.replace_html('team', render(#team))
app/views/members/create.js.rjs:1:in `block in _app_views_members_create_js_rj
s___908569197_34199712_807066544'
app/views/members/create.js.rjs:1:in `_app_views_members_create_js_rjs___90856
9197_34199712_807066544'
app/controllers/members_controller.rb:47:in `create'
Based on this it seems like it has found the create.js.rjs but is having trouble interpreting it. I'm not sure what the weird symbols are in front of page.
Edit: I've also found that if I view the source code before and after clicking the button, the button is indeed refreshing the code and adding the desired items. The problem seems to be exclusively in trying to refresh the partial.
Any help is appreciated. Thanks!
It seems your rjs file has some invalid bits at the start. Maybe try to re-create the file?
What did you expect to do with render(#team) ?
I've taken a look at the action view's method "render" and didn't found how you were expecting it to function. Maybe there is another functionality that you are aware and I don't.
You can also use erb and not rjs, using it just like a view
Along the lines of Bert Goethal's answer, is your editor saving your text file as UTF-8 with BOM?
A BOM will add two unicode encoded characters to the beginning of the file, and that might be where those are coming from...

Rails 3 rendering template missing without file ending

I have an action in my controller as below:
def show
#post = Post.find_by_setitle(params[:setitle])
if !#post
render 'error_pages/404'
return
end
respond_to do |format|
format.html
end
end
If the render error_pages/404 I get a template missing. Switching it to render error_pages/404.haml.html works fine.
Why is this?
N.B. There is no actual error_pages controller or model. Just a convenient place to keep them.
Edit: I'm using mongoid and hence don't have access to ActiveRecord. Controller base can't be looking for a particular ActiveRecord exception?
From the documentation
The render method can also use a view that’s entirely outside of your application (perhaps you’re sharing views between two Rails applications):
Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the :file option (which was required on Rails 2.2 and earlier):
You need either to pass the :file option, or to start the location string with a slash. Alternatively, you could use the Rails functionality to rescue from errors, and recover from ActiveRecord::RecordNotFound with a 404. See this post for details.
You should probably use render :template => 'error_pages/404'.
I think Rails is looking for a partial called _404.
Try it out 1:
render 'error_pages/404' (and name the file _404.html.erb)
Try it out 2:
render :template => 'error_pages/404' (and name the file 404.html.erb i.e. no leading underscore)

Rails ignoring render and redirect_to

I've got a really simple rails question here but I can't seem to find the answer anywhere. I guess some of the problems stem from me following a tutorial for Rails 1.2 with Rails 2.1. Anyway..
I'm writing a blog system and I'm implementing the comments bit. I have comments displaying fine once I've created them using script/console, but getting the comment form itself working is the hard bit.
In posts_controller.rb I have
def comment
Post.find(params[:id]).comments.create(params[:comment])
flash[:notice] = "Added comment"
#render :action => show
redirect_to :action => show
end
and in show.html.erb (the view) I have
<%= form_tag :action => "comment", :id => #post %>
<%= text_area "comment", "body" %><br>
<%= submit_tag "Post Comment" %>
When I submit the form it tries to go to the urb /posts/comment/1 which is obviously incorrect, and it complains that it can't find a template. Obviously I don't want a template there because I've told it to redirect to the show action because I want it to just re-display the post's show page, with the new comment there.
I've tried both the commented out line (render :action => show) and the redirect_to line, and neither seem to do anything at all.
I'm sure I'm missing something simple, but what is it?
Does redirect_to :action => 'show', :id => params[:id] with quotes around show work?
Rails 2.1 embraces "RESTful resources". show just happens to be the name of one of the predefined REST actions that all rails controllers use.
Rails does some magic behind the scenes, and :show is equivalent to "display this one specific element with a specific given ID". Sounds like it's getting mixed up with that. The ID is probably defaulting to "1". Hence the generated URL you're seeing from the render call
The Rails 2.1 way of doing it would use the following actions and templates:
index - displays the full list of comments
create - add a new comment
show - display a specific comment only (not the full list). Doesn't sound like this is what you want, but the "magic" inside rails will default to this.
There are also actions for new (show view to enter a new comment) edit (show view to do an edit of an existing comment) update (handle update submission) and destroy (duh), but it doesn't look like you'd use them in this example.
Do you have a link to the tutorial? Wouldn't be too hard to port it to Rails 2.1 style.
yes, you use old rails style.
Something new:
form_for :comment, :url => { :post_id => #post } do |f|
f.text_area :body
submit_tag "Post"
end
you can use resources for posts and comments, search google for better tutorial or install rails 1.2.6:
gem install -v 1.2.6 rails

Resources