Calling methods that render views conditionally in a Rails Controller

ruby-on-rails, ruby-on-rails-3, web-applications

Solution

You need to declare these new actions that you have created in the routes file, as they don't belong to the RESTful routes. I sugest to keep only the show action in your controller and create the IFs in the show view using the render method to include the partials(_showMine.html.erb, showAnother.html.erb, showUnauthorized)

example:

show view:

if (something)
  <%= render 'showMine' %>
elsif (something else)
  <%= render 'showAnother' %>
else
  <%= render 'showUnauthorized' %>
end

I hope it helps...

Problem

I am writing a Ruby on Rails application with a controller called "pages_controller" that is responsible for displaying pages to users. There are 3 different types of pages that can be displayed, and different things have to happen on the back end in each case, so I decided to break the functionality out into 3 methods within the controller. When the user requests a page, the "show" method is called, which figures out whether the page: 1. Belongs to the user 2. Belongs to another user, and can be viewed by the user requesting it 3. Belongs to another user, and cannot be viewed by the user requesting it (unauthorized) The appropriate method is then called from there to display the page. The code looks something like this: ``` def show if (something) showMine elsif (something else) showAnother else showUnauthorized end end def showUnauthorized respond_to do |format| format.html # showUnauthorized.html.erb end end def showMine respond_to do |format| format.html # showMine.html.erb end end def showAnother respond_to do |format| format.html # showAnother.html.erb end end ``` I am getting a template missing error because rails wants to render a view when "show" is called, but I do not want any views to be rendered when "show" is called. I simply want "show" to call the correct method from there, and the corresponding view for that method (showMine, showAnother, or showUnauthorized) to be rendered. How can I do this? Or am I going about this the wrong way entirely?

Original source