views:

90

answers:

3

Maybe my logic is not restful or know if this is how you would do it but this is what I am trying to do.

I'm getting a category inside a category controller and then once I get that category I want to return to an index page in a different controller but keep that @category and the Category.busineses.

Before rest I would have just done this:

render :controller => "businesses"

and it would have rendered the view of the index action in that controller.

now in my respond_to block I have this

   format.html {redirect_to(business_path)} # index.html.erb
   format.xml  { render :xml => @businesses }

but of course with a render it looses the instance variable and starts with a new action.

So what I want to do is render the action instead of redirecting to that action. is this possible?

Should i just replace the respond_to with render :controller => ?

+1  A: 

I am not 100% clear of what you are trying to do, but you have several options for presentation in a RESTful app.

You can pass a parameters in a redirect. In this case we pass the category id as a parameter in the HTTP GET. The page on the end of the redirect can then handle this appropriately:

format.html {redirect_to( business_path(:category_id => @category.id) }

You can also render the view of a specified action or template. In this case we render the view defined in "{current_controller}/business.html.erb":

format.html { render :action => "business" }
Toby Hede
I'm also not very clear on what I want.
Sam
format.html { render :action => "index" } was what I was looking for
Sam
+1  A: 

You cann't use redirect_to if you want instance variable as it is use render as follows.

format.html { render :controller=> 'buisness'  ,:action => "index" }

OR JUSt

format.html { render :controller=> 'buisness'}
Salil
Thanks, that was what I was looking for. I think you answered just after Toby otherwise I would have given you the check instead of a vote
Sam
it's ok Sam. but remember format.html { render :action => "business" }as in Toby's Answer must be format.html { render :controller => "business" } otherwise it will look for the page at the categories/buisness.html.erb. I know it's just typo or may be in hurry but take a look at it. else is fine.
Salil
+1  A: 

I think what you are trying to do (if I understand it correctly) would be best achieved using nested resources. If there are businesses separated into categories, and you want to display a listing of all businesses in a specific category, you could set up your application like this:

Models

class Category < ActiveRecord::Base
  has_many :businesses
end

class Business < ActiveRecord::Base
  belongs_to :category
end

Routes

map.resouces :businesses

map.resources :categories do |categories|
  categories.resources :businesses
end

Controller

class BusinessesController < ApplicationController
  def index
    @category = Category.find(params[:category_id]) if params[:category_id]
    conds = params[:category_id] ? { :category_id => params[:category_id] } : nil
    @businesses = Business.all(:conditions => conds)
  end
end

Then simply access the list of a category's businesses like this: /category/1/businesses

Jimmy Cuadra
so what about if I just want to get the index action with a full list of businesses not by categories. check for params[:category_id] ?
Sam
@Sam - Check out my edit for an example of how to use the same action for both `/businesses` and for `/category/1/businesses`.
Jimmy Cuadra
Thanks really great. BTW - What do you think is the best resource for learning rest on rails?
Sam
The guide on the official site is quite good if you haven't seen it already: http://guides.rubyonrails.org/routing.html There are also several good Railscasts in the routing category including nested routing and custom REST actions: http://railscasts.com/tags/14
Jimmy Cuadra
def index is missing a closing parenthesis on params[:category_id]
Sam
@Sam - Thanks, fixed.
Jimmy Cuadra