How to render partial.js in rails 3
Solution 1
Shouldn't it be in the file _show.js.erb
?
From "Using Partials".
Solution 2
You can try this. Instead of have a Javacript partial you can make an html one _show_js.html.erb
and surround your JS code with the html <script>
tag.
<script type='text/javascript'>
$("tasks").append(new TaskWidget(task.id))
</script>
and,
<div id="tasks">
<%= render(:partial => "tasks/show_js", :collection => @project.tasks) %>
</div>
Solution 3
To make it work I needed to add the format:
<script>
<%= render(:partial => 'shared/topmenujs', :formats => [:js] ) %>
</script>
Solution 4
Try:
render :partial => "tasks/show.js"
Solution 5
Well I don't know if is the same issue, but I went through a similar thing with rails 3. I had for example a partial view in app/views/invoices/_select.html.erb
and I was able to render in a view with
<%= render :partial => "invoices/select" %>
with out any problem and if in the controller I place
render :partial => "invoices/select"
I was able to opened the action in the browser, the problem arise when I try to call the action from an ajax call, then I get the ActionView::MissingTemplate
error. The only way I found to solved was to rename the view to _select.rhtml
and then every thing works fine.
Julian Mann
Updated on July 09, 2022Comments
-
Julian Mann almost 2 years
Using rails3 - I have a project with many tasks. I want to use javascript to build the UI for each task. I figured I could display those tasks on the projects show page by rendering a javascript partial for each. I can't get 'tasks/show' to see tasks/show.js.erb Any ideas?
In projects/show.html.erb
<div id="tasks"> <%= render(:partial => "tasks/show", :collection => @project.tasks) %> </div>
tasks/show.js.erb
$("tasks").append(new TaskWidget(task.id))
I get the errors
ActionView::MissingTemplate in Projects#show Missing partial tasks/show with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths .... around line #13
Thanks
-
B Seven almost 13 yearsI am having the same issue. I got it working by copying the file and changing html to js in the file name, as suggested by Michael. But that doesn't seem to be a very good solution.
-
bonhoffer almost 13 yearsif he did this, and changed the filename to _show.js.erb, it would work
-
user569825 about 11 yearsFor the sake of progressive enhancement you should consider attaching your desired functionality via good old JS selectors - once the basic HTML-only version is working :)
-
Philip almost 10 yearsThis worked for me as long as I didn't use " for plain text. By replacing alert("test") with alert('test') it started working correctly for me.