Rails 4 - 脚手架生成器

Rails 4 - scaffold generator

我正在尝试用 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

是否需要更多的东西才能让它工作?

您的代码存在问题:

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

您需要将其更改为 return 超出 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