【发布时间】:2015-01-04 22:26:24
【问题描述】:
我正在尝试将模型的记录(列出所有记录,如 Posts.all)调用到绑定到另一个控制器的视图。
所以,我想访问 Posts_controller 的索引操作,其中包含我想访问的 .all 列表和 .group_by 列表,并将它们列在 Pages_controller 中列出的静态页面中(命名为 yonetim)
这仅用于列出管理员视图的帖子(如活动管理员中的列表)。
我认为,我不需要发布任何代码,因为问题很抽象,但如果需要我会编辑问题。
* 为澄清而编辑*
这是我的 posts_controller.rb
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
load_and_authorize_resource
def index
@posts = Post.all.order('postdate DESC')
@posts_by_month = @posts.group_by { |post| post.postdate.strftime('%m - %Y')}
end
def show
end
def new
@post = current_user.posts.build
end
def create
@post = current_user.posts.build(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def edit
end
def update
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post.destroy
redirect_to root_path
end
private
def post_params
params.require(:post).permit(:id, :title, :body, :postdate)
end
def find_post
@post = Post.find(params[:id])
end
end
可以看出它是一个基本的博客应用程序。到达 root_path (posts#index routed) 的访问者可以看到基于月份和年份分组的帖子记录。
我要添加的是从我为管理界面创建的静态页面(类似于活动的管理 gem)到达新的、编辑销毁和索引。@posts。
** 这是 pages_controller.rb **
class PagesController < ApplicationController
def yonetim
end
end
所以当我点击 /yonetim(路由到获取 pages#yonetim)时,我希望用户看到帖子控制器的索引操作,其中包含指向新建、显示、编辑和销毁记录的链接。
***系统也有 admin boolean 和 cancan 的设计,所以如果用户没有登录或没有管理员使用的授权,他们会被移动到 root_path,但会出现异常。
我的问题出现了,我几乎尝试了所有方法来列出 pages/yonetim 视图或 pages_controller.rb yonetim 方法中 posts#index 方法的 @posts 记录。
这样我就可以在我的管理视图中列出它们并使用它们。
如果需要其他任何内容,请告诉我。
提前致谢, 穆斯塔法
【问题讨论】:
标签: ruby-on-rails activerecord