mcmajkel
mcmajkel

Reputation: 313

How to render different view from controller

Sort of noob-rails question ;):

I've got 2 actions in my controller - index and own. In index, i'm listing all posts and own generates only logged users' posts. Controllers are pretty similar, but view is identical and I assume can be shared between this two controllers.

In the own controler I put something like this:

respond_to do |format|
  format.html { render :action => "index" }
  format.json { render json: @ads }
end

And added to routes:

match "/ads/own" => 'ads#own', :via => :get

Is there any better solution to do this?

Upvotes: 2

Views: 1497

Answers (1)

Carson Cole
Carson Cole

Reputation: 4451

You can do this:

def index
  ....
end

def own
  ....
  render :index    
end

Everything (all variables) will directly pass to the index view in own. If you want the :json component, then add:

class SomeController < ApplicationController
  respond_to :html, :json

and put 'respond_with @posts' as the last item in each action.

Upvotes: 4

Related Questions