0

I'm trying to debug why a piece of HTML code isn't rendering properly in (someone else's) Rails code.

I noticed that when my .erb template is called, the console outputs the following:

Started GET "/sub_account/new?_=1389112701545" for 127.0.0.1 at 2014-01-07 18:39:02 +0200
Processing by SubAccountController#new as q=0.5
  Parameters: {"_"=>"1389112701545"}
  Rendered sub_account/new.erb (0.5ms)
Completed 200 OK in 16ms (Views: 4.9ms | ActiveRecord: 1.7ms)

This returns nothing. I removed all the variables from "sub_account/new.erb" and just put in a "hello world", but that isn't showing up either.

This is what my "sub_account" controller looks like:

#encoding: utf-8
class SubAccountController < Devise::RegistrationsController
  before_filter :authenticate_user!
  layout 'main'
  respond_to :js, :html, :json
  ..

  def new
    build_resource({})
    respond_with self.resource
  end

  def resource
    instance_variable_get(:"@#{resource_name}")
  end

  private
  def build_resource(hash=nil)
    self.resource = resource_class.new_with_session(hash || {}, session)
  end

  def resource_name
    'SubAccount'
  end

  def resource_class
    Classes::SubAccount
  end

Looking at other code that actually renders on the same site, I see the following at one point:

Started GET "/admin/driver/new" for 127.0.0.1 at 2014-01-07 18:38:55 +0200
Processing by Admin::DriverController#new as JS
  Rendered admin/driver/_form_fields.erb (11.9ms)
  Rendered admin/driver/new.erb (79.9ms)
Completed 200 OK in 98ms (Views: 84.2ms | ActiveRecord: 1.9ms)

I noticed this got rendered as JS, in another example, I see the following:

Started GET "/personal/new" for 127.0.0.1 at 2014-01-07 18:46:28 +0200
Processing by PersonalController#new as HTML
  Rendered shared/_summary_errors.erb (0.2ms)
  Rendered personal/_hidden_fields.erb (1.4ms)
  Rendered personal/_card.erb (9.8ms)
  Rendered personal/_form_fields.erb (28.9ms)
  Rendered devise/shared/_links.erb (0.8ms)
  Rendered personal/new.html.erb within layouts/main (104.3ms)
  Rendered layouts/_head.erb (43.2ms)
  Rendered shared/_logo.erb (1.0ms)
  Rendered shared/_sign_up_header.erb (3.1ms)
  Rendered layouts/_header.erb (4.1ms)
Completed 200 OK in 7442ms (Views: 168.7ms | ActiveRecord: 0.7ms)

So this was rendered as HTML. But, I have no idea how or why? My code that never gets rendered is rendered as q=0.5, but I have no idea what that means or how to change it.

update

this is the content of new.html.erb (the logs above say new.erb but i was trying both):

<% content_for :scripts do %>
  <script type="text/javascript">
    //<![CDATA[
    window.resource_name = '<%= resource_name %>';
    //]]>
  </script>
  <%= javascript_include_tag  'regex-mask-plugin', :profile_page, 'custom_validators', 'custimize_validator', 'steps' %>
<% end %>

<div class="row" style="margin-top: 30px;">
  <div class="col-md-11">
    <%= form_for(resource, :as => resource_name, :url => sub_account_index_path, :validate => true) do |f| %>
      <div class="row steps">
        <div class="col-md-12">
          <%= render 'form_fields', :f => f, :focus => true %>
        </div>
      </div>

      <!--<div class="col-md-2">
        <div class="row">
          <div class="col-md-8">
            <%= f.submit 'Create', :class => 'btn btn-default btn-block'%>
          </div>
        </div>
      </div>-->
    <% end %>
  </div>
</div>

update 2:

looking at chrome's dev tools.. I noticed that the request made when click the button does actually return html the same way the other successful renders do:

enter image description here

however, the request headers are different.. compare the belowenter image description here

with

enter image description here

the correct one accepts */*;q=0.5, text/javascript,.. and the failing one accepts q=0.5, text/javascript.... how can I make it accept */* as well?

also another difference is that the failing one has a query string parameter of _:1389152333528.. would that make a difference?

update: i just confirmed that it is this weird query string that's causing all the trouble.. i tested the same request on postman without the query string.. and surely I got the correct html back:

enter image description here

abbood
  • 23,101
  • 16
  • 132
  • 246
  • Just venturing a guess but you may need to change the name of the ```sub_account``` folder to ```sub_accounts```. Generally I've seen things in the views folder being plural. – Travis Pessetto Jan 07 '14 at 17:19
  • BTW I've googled about 'q=0.5' http://stackoverflow.com/questions/8552927/what-is-q-0-5-in-accept-http-headers Where from do you request this? I mean is it just plain browser request or some javascript on the page? – TheRusskiy Jan 07 '14 at 17:24
  • @TheRusskiy it's just a button on the browser.. `` – abbood Jan 07 '14 at 17:33
  • tried the plural/singular thing @TravisPessetto.. no luck – abbood Jan 07 '14 at 17:34
  • Judging by 'data-target="#newUser" data-toggle="modal"', and that there's no link, actual loading happens from javascript. So it depends on what kind of dataType does jQuery request. http://api.jquery.com/jquery.ajax/ – TheRusskiy Jan 07 '14 at 17:48
  • 1
    can you post what's inside de sub_accounts/new.erb file? – Marc-Alexandre Bérubé Jan 07 '14 at 18:26
  • @Marc-AlexandreBérubé posted.. however I did try replacing all that html stuff with just a single `hello world` line to test if the file was actually rendered, but even hello world wasn't rendered! – abbood Jan 08 '14 at 03:10
  • @TheRusskiy i updated the question with some details you may find interesting.. – abbood Jan 08 '14 at 03:55

1 Answers1

0

Try renaming files in your view from something.erb to something.html.erb and something.js.erb. When you write
respond_to :js, :html, :json
...
respond_with self.resource Controller first tries to render something.js.erb then something.html.erb etc. Without extension it doesn't know what view to render.

TheRusskiy
  • 1,031
  • 12
  • 13