2015-11-14 66 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 

注意,我去掉了您的授權,所以加回來時,一切工作英寸