2015-11-14 69 views
0

我一直在使用Rails脚手架来构建Rails 4应用程序。导轨 - 脚手架 - 控制器设置 - unknownFormat

我现在的问题是载列如下 - 走到哪当我尝试和呈现的文章新页面在我的应用程序:

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

我不知道你在做什么。这看起来像是一起制作和展示动作。你能提供更多细节吗?你有意使用JSON吗? –

回答

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 

我希望这可以帮助您。

+0

为什么脚手架产生的东西不起作用? – Mel

+0

我已经做了几个星期前在我的项目中尝试使用脚手架来使用rails 4.它会生成并正常工作。也许,这事发生在rails gem中。你使用什么版本? – akbarbin

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 

注意,我去掉了您的授权,所以加回来时,一切工作英寸