简体   繁体   中英

controller into another controller in rails?

So I have this Ruby on Rails course and I have to create a really simple blog application for it. But it's giving me a hard time!

I have this model called 'articles' and I can see a list with all the articles by navigating to /articles. I made a controller for it by following the Rails documentation. My teacher also wants me to make an 'admin' area, so I need to access the same list by going to /admin/articles.

How can I do that? I know this is a really dumb question but I couldn't find the answer anywhere.

Thank you for your time!

you need to create routes for /articles and for /admin/articles

routes.rb

get "/articles" => "articles#index"

namespace :admins do get "/articles" => "articles#index"
end

And create 2 controllers.

1)

class ArticlesController < ApplicationController

  def index
    @articles = current_user.articles
  end

end

and for admin, create another controller under folder admins.

2)

class Admins::ArticlesController < Admins::BaseController
  def index
    @articles = Articles.all
  end
end 

Here inherit Basecontroller for better coding and handling controllers with different namespace. Or you can inherit application controller also.

And one more thing, Please create model with singular name like article only, and plural for controller like articles as per rails standard naming convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM