导轨4 - 脚手架发电机

时间:2016-01-01 00:57:00

标签: ruby-on-rails scaffolding respond-to ruby-on-rails-5

我正在尝试使用Rails 4制作应用程序。

我已使用generate scaffold命令设置文章控制器。

目前看起来像这样:

class ArticlesController < ApplicationController
  before_action :set_article, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show, :search]


  respond_to :html, :json
# GET /articles
  # GET /articles.json
  def index
    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

  # 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.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

  def search
    if params[:search].present?
      @articless = Article.search(params[:search])
    else 
      @articles = Articles.all
    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(:user_id, :body, :title, :image,
        comment_attributes: [:opinion])
    end
end

我将此行添加到文件顶部:

respond_to :html, :json

我发现这篇文章解释了为什么需要这样:

http://www.justinweiss.com/articles/respond-to-without-all-the-pain/

但是,当我保存更改并尝试更新文件中的条目时,我收到此错误:

ActionController::UnknownFormat in ArticlesController#update

错误指向更新操作中的respond_to行:

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

是否还需要更多功能才能使其正常工作?

1 个答案:

答案 0 :(得分:2)

您的代码存在问题:

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的范围:

respond_to do |format|
  if @article.update(article_params)
    format.json { render :show, status: :ok, location: @article }
  else
    format.json { render json: @article.errors, status: :unprocessable_entity }
  end
  format.html { render :edit }
end