views:

27

answers:

1

I rendering a view partially like this.

<%= render(:partial => "index" ,:controller=>"controller_name") %>

so this will partially render controller_name/_index.html.erb

here is my doubt. can i write an action method for this _index. something like this?

class ControllerNameController < ApplicationController
  def _index
  end
end

thanks.

+3  A: 

No this should be

class ControllerNameController < ApplicationController
  def index
   render :partial=>'index'
  end
end

EDITED:- Expalaining my answer in detail when you write a method "method_name" & you not render (or not using redirect_to) anything it will look for page "method_name.html.erb" by default. But when you write a method and render action or partial it will care only what write in a render.

For Example

class ControllerNameController < ApplicationController
  def some_method_name
   render :partial=>'index'  #look for the _index.html.erb
  end
end


class ControllerNameController < ApplicationController
  def some_method_name
   render :action=>'index'  #look for the index.html.erb
  end
end


class ControllerNameController < ApplicationController
  def some_method_name  #look for the "some_method_name.html.erb"

  end
end
Salil
so do you mean i should have both index and _index? two files in place of one..?
ZX12R
No when you write "render :partial=>'index'" then it will look only for _index.html.erb it simply don't care what is in index.html.erb
Salil
thanks...that was really clear..!!
ZX12R