0

I have the following link_to:

<%= link_to 'user_' + user.id.to_s, {:controller => 'admin', :action => 'view_user', :id => user.id}, :remote => true %>

However, when I click that link, I get the following error:

ActionView::MissingTemplate at /admin/view_user
Missing template admin/view_user, application/view_user with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml, :rabl]}.
Searched in: /Users/me/Documents/production/my_app/app/views

It should be looking for view_user.js.erb. That file definitely exists.

This issue seems to have come up after I changed how I deal with jquery & jquery-ui. I am now using the jquery-ui-rails gem. I removed the lines requiring jquery-ui and jquery_ujs from my application.js file. I don't know of anyway to add jquery_ujs (maybe it is packaged with rails?)

Any clue what might be going on? What's puzzling is, I figured explicitly saying :remote => true would result in the JS file being rendered.

here's what view_user looks like:

def view_user
  respond_to do |format|
    format.js
  end
  @user = User.find(params[:id])
  user_show_helper
end

user_show_helper is a huge method that basically sets a bunch of instance variables necessary to show the snippet of a user profile (i.e. there is no render method called in user_show_helper).

Here's what view_user.js.erb looks like:

$("#user_under_review").html("<%= escape_javascript render(:partial => 'admin/view_user' ) %>");

And here's what the partial _view_user.html.erb looks like:

<%= render 'users/user_brief' %>
<div class="table_td_middle" id="<%= 'user_number_' + @user.id.to_s %>">
  <%= link_to 'Approve user?', approve_user_as_admin_path(@user.id), :remote => true, :confirm => 'Are you sure you want to approve this user?' %>
</div>
Ringo Blancke
  • 2,444
  • 6
  • 30
  • 54
  • This is likely an issue with your controller. Can you please post the controller action you're trying to render from? – zeantsoi Jun 06 '13 at 20:25
  • You're incorrectly invoking the use of helpers. Helpers are designed for use in views solely. There's no way you'll get one to work by default in a controller. – zeantsoi Jun 06 '13 at 20:47
  • what do you mean exactly? I could basically be doing the exact same stuff that I'm doing in the helper method within the controller itself ... but the helper is from a different controller (user controller). basically, in order to render a user, I user the user_show_helper in a bunch of places. all it does is DB queries and set instance vars to results of these queries. – Ringo Blancke Jun 06 '13 at 20:51
  • Rails helpers are actually _view_ helpers, and are not designated to work within controllers out of the box. If you need to use a helper in a controller (which as you said, is redundant, since you can do all the helper code in the controller anyways), you'll need to import it into your controller class: `include UserShowHelper` – zeantsoi Jun 06 '13 at 20:56
  • oh yeah, I have that all setup! no issue there actually. the issue is just that the controller is trying to serve up a .html format, though I set :remote => true and the view_user method is now set to respond with .js format ONLY. still I get served a (blank) HTML page and the server logs indicate "processing ___ as HTML". WHY, I don't know :( – Ringo Blancke Jun 06 '13 at 21:01
  • Understood. See my updated answer for a potential resolution to this issue. – zeantsoi Jun 06 '13 at 21:02
  • thanks for your help zeantsoi -- looks like manually including the jquery_ujs file and the requiring it in my application.js file fixed my issues. jquery_ujs helps with a bunch of auxiliary stuff that I needed it looks like – Ringo Blancke Jun 06 '13 at 21:36

2 Answers2

1

Your controller doesn't look like it's configured to render out Javascript. The second line of the ActionView::MissingTemplate error indicates that the action is only configured to render out HTML: :formats=>[:html].

Have you specified the formats that can be rendered from the controller action?

# app/controllers/admin_controller.rb
def view_user
    # Business logic
    respond_to do |format|
        format.html
        format.js
    end
end

EDIT:

The latter answer to this post suggests that rake assets:precompile can conditionally resolve issues relating to a :remote => true link rendering out HTML, rather than Javascript.

Community
  • 1
  • 1
zeantsoi
  • 25,857
  • 7
  • 69
  • 61
0

I had removed the jquery-rails gem from my Gemfile and thus had to manually add the jquery_ujs.js file into my assets folder.

I re-added the jquery-rails gem and now we're all good to go!

Great success!!

Ringo Blancke
  • 2,444
  • 6
  • 30
  • 54