我一直在使用Rails脚手架来构建一个Rails 4应用程序。
我目前的问题如下所示 - 当我尝试在我的应用中渲染文章新页面时,该问题就出现了:
ActionController::UnknownFormat
它指的是文章控制器中的创建操作(它提取此错误位置):
@article = Article.new(article_params)
respond_to do |format|
if @article.save
format.json { render :show, status: :created, location: @article }
else
我有一个文章控制器:
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
# respond_to :html
# GET /articles
# GET /articles.json
def index
@articles = Article.all
end
# GET /articles/1
# GET /articles/1.json
def show
end
# GET /articles/new
def new
@article = Article.new
end
# GET /articles/1/edit
def edit
end
# POST /articles
# POST /articles.json
def create
# before_action :authenticate_user!
# authorize @article
@article = Article.new(article_params)
respond_to do |format|
if @article.save
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
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])
end
# Never trust parameters from the scary internet, only allow the white list through.
def article_params
params[:article].permit(:body, :title, :image,
comment_attributes: [:opinion])
end
end
我在控制器的顶行回复了html。我不知道为什么或这样做。我已经读过其他问题,说你需要给它一些回应,或者使用响应者宝石。我不想要任何特别的东西 - 我只想要渲染页面。
谁能看到这里的错误?
答案 0 :(得分:0)
如果你不想做任何特别的事情,这就是我要如何清理这个控制器:
class ArticlesController < ApplicationController
before_action :set_article, except: [:new, :create, :index]
def index
@articles = Article.all
end
def show
end
def new
@article = Article.new
end
def edit
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render :new
end
end
def update
if @article.update_attributes(article_params)
redirect_to @article
else
render :edit
end
end
def destroy
if @article.destroy
redirect_to articles_path
else
redirect_to @article
end
end
private
def set_article
@article = Article.find(params[:id])
end
def article_params
params[:article].permit(:body, :title, :image,
comment_attributes: [:opinion])
end
end
请注意,我删除了您的授权,因此在一切正常时将其重新添加。
答案 1 :(得分:0)
您似乎需要format.html
,因此您可以添加:
@article = Article.new(article_params)
respond_to do |format|
if @article.save
format.html { redirect_to(@article,
:notice => 'Article was successfully created.') }
format.json { render :show, status: :created, location: @article }
else
format.json { render :show, status: :created, location: @article }
end
end
我希望这对你有所帮助。