开发者

Rails 3: rails newbie confused about routes/controllers

I've created a small twitter clone app where users make posts, follow other users, and have a feed of all the pos开发者_如何学Cts by the user's they follow. I've added a choice of categories for the posts to fall into, "thriller" "western" "horror" and so on.

When a user is logged in, the root web address is their dashboard.

routes.rb

root :to => "pages#home"

And pages#home is where the user's feed is located: @feed_items = current_user.feed

PagesController

def home
    @title = "Home"
    @featured_posts = Post.featured.limit(10)
    if user_signed_in?
      @user = current_user
      @post = current_user.posts.build
      @feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
    end
end

Now I've figured out how to parse the feed and pull out a subset-feed that contains only the posts with category_id '2' (also know as 'thriller')

 @thriller_feed_items= current_user.feed.where(:category_id => '2')

When a logged in user goes to the root_path they see their dashboard and entire feed. I'd like there to be a link that says 'thrillers' that will change the current entire @feed_items into the @thriller_feed_items but I'm confused about how the routes and views would work. Twitter uses /!#/mentions as the address for sub-set feeds, do I need to do the same thing? How would I set this up?

EDIT: to show how my feed method works.

User model

  def feed
    Post.from_users_followed_by(self)
  end

Post model

   def self.followed_by(user)
      followed_ids = %(SELECT followed_id FROM relationships
                       WHERE follower_id = :user_id)
      where("user_id IN (#{followed_ids}) OR user_id = :user_id", :user_id => user)
    end


I'm assuming at this point that this controller is non-RESTful.

Edit: I've updated this to also allow a category name. Adjust routes and variable indices accordingly.

Routes.rb

match "/category/:category" => "pages#home"

Pages#home

def home
  @title = "Home"
  @featured_posts = Post.featured.limit(10)
  if user_signed_in?
    @user = current_user
    @post = current_user.posts.build
    # BEGIN NEW
    if params[:category]
      # is :category an integer id?
      if params[:category].to_i.to_s == params[:category]
        @feed_items= current_user.feed.where(:category_id => params[:category])
      else
        # assuming Category has_many :feeds
        @feed_items= current_user.feed.includes(:category).where(
          ['`categories`.name = ?', params[:category]]
        )
      end
    else
      @feed_items = current_user.feed
    end
    @feed_items = @feed_items.paginate(:per_page => "10", :page => params[:page])
    # END NEW
  end
end

View:

<%= link_to "Thriller Feed Items (by id)", "/category/2" %>
<%= link_to "Thriller Feed Items (by name)", "/category/Thriller" %>


so why dont you use your Pages controller to create a new controller action called thriller, and a new view called pages/thriller.html.erb.

In your routes.rb, you could then do something like:

get 'page/thriller'

or

match 'thriller' => 'page#thriller'

More generally, you can create a genre action and pass the genre as an ID.

like

match 'genre/:genre_id' => 'page#genre' (you get the parameter with params[:genre_id])

Also read the routing guide.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜