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

Julian Mann
  • 6,256
  • 5
  • 31
  • 43

6 Answers6

29

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

From "Using Partials".

wombleton
  • 8,336
  • 1
  • 28
  • 30
  • 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. – Philip Jun 10 '14 at 16:23
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>
cortex
  • 5,036
  • 3
  • 31
  • 41
  • 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 :) – user569825 Mar 11 '13 at 05:26
  • This approach can make the embedded Ruby more difficult to deal with IMHO. – rantingsonrails Oct 28 '22 at 09:54
7

To make it work I needed to add the format:

 <script> 
  <%= render(:partial => 'shared/topmenujs', :formats => [:js] ) %>
  </script>
htafoya
  • 18,261
  • 11
  • 80
  • 104
5

Try:

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

xavi
  • 51
  • 1
  • 1
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.

ramontiveros
  • 309
  • 4
  • 11
  • 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. – B Seven May 31 '11 at 04:39
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.

Michael Waxman
  • 1,815
  • 3
  • 18
  • 32