Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails render partial from controller

I'm trying to render different partials into my index view from the controller, depending on the params I receive. I have a simple unless-else condition inside my controller that checks the params

def index
 unless params[:check].nil?
  render :partial => /layout/check
 else
  render :partial => /layout/not_check
 end
end

And I have the files _check.html.erb and not_check.html.erb into the folder layout/

Now, how can I show that partials inside my index view? At the moment I can just visualize the partial alone as a single view, but not inside the requested view.

like image 212
parov Avatar asked Oct 14 '13 12:10

parov


1 Answers

The better way would be to call the partials from index.html.erb

<% unless params[:check].nil? %>
  <%= render :partial => '/layout/check' %>
<% else %>
  <%= render :partial => '/layout/not_check' %>
<% end %>

so your def index would look like this

def index
  respond_to do |format|
    format.html
  end
end

I did not understand what you are trying to do but partial which are related to controller/actions should not be in layout unless they are serving some layout.

like image 68
techvineet Avatar answered Sep 19 '22 13:09

techvineet