How to render partial.js in rails 3
Asked Answered
S

6

20

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

Sledgehammer answered 2/5, 2010 at 22:54 Comment(2)
Not sure but try: <%= render(:partial => "tasks/show.js", :collection => @project.tasks) %>Kempf
thanks, but unfortunately I get the same thing: Missing partial tasks/show.jsSledgehammer
T
29

Shouldn't it be in the file _show.js.erb?

From "Using Partials".

Tensible answered 3/5, 2010 at 4:18 Comment(1)
This 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.Nereid
U
12

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>
Unkenned answered 21/7, 2011 at 17:7 Comment(2)
For 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 :)Nauru
This approach can make the embedded Ruby more difficult to deal with IMHO.Legist
P
7

To make it work I needed to add the format:

 <script> 
  <%= render(:partial => 'shared/topmenujs', :formats => [:js] ) %>
  </script>
Pancake answered 24/2, 2016 at 3:20 Comment(0)
Y
5

Try:

render :partial => "tasks/show.js"

Yahrzeit answered 27/1, 2011 at 14:58 Comment(1)
if he did this, and changed the filename to _show.js.erb, it would workKrucik
L
3

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.

Libbie answered 5/11, 2010 at 17:18 Comment(1)
I 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.Masticate
C
2

The problem is that in Rails 3 the render from the .js file will only look for files in the form of show.js.erb or show.erb

To get around this, you would either need to create a _show.js.erb file, but since that sort of defeats the purpose of partials, you could alternatively just rename the show.html.erb file to show.erb

This is probably bad practice, though, to make a standard controller action without a type ending (e.g. show.erb)

What I would do if I were you is rename your partial something like _tasks.erb instead of _show.html.erb, and then if you needed a tasks/show.html.erb you could just render the _tasks partial inside of that view.

Custer answered 6/10, 2010 at 2:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.