【问题标题】:How to create a new controller in monologue blog engine in rails如何在 Rails 的独白博客引擎中创建新控制器
【发布时间】:2015-12-04 21:43:39
【问题描述】:

我正在尝试为博客文章作者创建描述页面。我创建了一条路线:

mount Monologue::Engine, at: '/blog'

#create routes for monologue
Monologue::Engine.routes.draw do
    get 'p/:name', to: 'authors#show', as: :author
end

我在 app/controllers/monologue 中创建了一个控制器:

class Monologue::AuthorsController
    def show
        puts "in show method"
    end
end

我不断收到此错误:

undefined method `action' for Monologue::AuthorsController:Class

长话短说:如何在独白中生成控制器?

【问题讨论】:

  • 安装引擎的应用程序的 $rake 路由输出是什么?

标签: ruby-on-rails ruby-on-rails-4 rails-engines monologue


【解决方案1】:
Monologue::Engine.routes.draw do
  get '/posts/:author_id', to: "posts#author_posts", as: "author_posts"
end

class Monologue::PostsController < Monologue::ApplicationController
  def author_posts
    @posts = Monologue::Post.where(:user_id => params[:author_id],).order(created_at: :desc)
    @author = Monologue::User.find_by_id(params[:author_id])
  end
end

这是添加控制器操作的方式。

【讨论】:

  • 请添加一些关于您正在回答的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 1970-01-01
相关资源
最近更新 更多