我的rails 4应用程序中有一个名为Article的模型。
我想在我的views / articles文件夹中添加一个名为foldaway.html.erb的静态页面。
我添加了该视图页面,并使用以下内容更新了我的文章控制器:
def foldway
end
我还试图从set_article方法中排除foldaway:
before_action :set_article, except: [:foldway], only: [:show, :edit, :update, :destroy ]
在我的路线文件中,我添加了:
get '/foldway' => 'articles#foldway'
当我保存所有这些并尝试测试页面是否呈现时,我收到一条错误消息:
Couldn't find Article with 'id'=foldway
当我从rails控制器中的set_article方法中排除它时,为什么要设置文章ID。我只是想让它呈现一个静态页面。
如何将其添加到我的文件中?
我的完整文章控制器是:
class ArticlesController < ApplicationController
before_action :set_article, except: [:foldway], only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show ]
layout "article"
respond_to :html, :json
# GET /articles
# GET /articles.json
def index
@articles = policy_scope(Article)
# query = params[:query].presence || "*"
# @articles = Article.search(query)
end
# def index
# if params[:query].present?
# @books = Book.search(params[:query], page: params[:page])
# else
# @books = Book.all.page params[:page]
# end
# end
# GET /articles/1
# GET /articles/1.json
def show
end
def foldway
end
# GET /articles/new
def new
@article = Article.new
@article.comments.build
end
# GET /articles/1/edit
def edit
authorize @article
end
# POST /articles
# POST /articles.json
def create
# before_action :authenticate_user!
# authorize @article
@article = current_user.articles.new(article_params)
respond_to do |format|
if @article.save
format.html { redirect_to(@article) }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /articles/1
# PATCH/PUT /articles/1.json
def update
# before_action :authenticate_user!
authorize @article
respond_to do |format|
# if @article.update(article_params)
# format.json { render :show, status: :ok, location: @article }
# else
# format.html { render :edit }
# format.json { render json: @article.errors, status: :unprocessable_entity }
# end
# end
if @article.update(article_params)
format.html { redirect_to(@article) }
format.json { render :show, status: :ok, location: @article }
else
format.json { render json: @article.errors, status: :unprocessable_entity }
end
format.html { render :edit }
end
end
# DELETE /articles/1
# DELETE /articles/1.json
def destroy
before_action :authenticate_user!
authorize @article
@article.destroy
respond_to do |format|
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_article
@article = Article.find(params[:id])
authorize @article
end
# Never trust parameters from the scary internet, only allow the white list through.
def article_params
params.require(:article).permit(:body, :title, :image, :tag_list,
comment_attributes: [:opinion])
end
end
答案 0 :(得分:0)
问题来自您的路线文件。
我建议你把它放在routes.rb文件中:
get 'articles/foldway' => 'articles#foldway', as: :foldway
match '/foldway', to: 'articles#foldway', via: :get
对于您的文章控制器,您只需要输入:
before_action :set_article, only: [:show, :edit, :update, :destroy ]