0

当我在我的 Jobs 表单上按 new 时看到一个错误,它在我的 JobsController 中找不到“create”。

Unknown action

The action 'create' could not be found for JobsController

这是我的控制器:

class JobsController < ApplicationController

  private
  def load_clients
        @clients = collection_select :client, :client_id
  end

  def index
    @job = Job.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @job }
    end
  end

  def create
    @job = Job.new(params[:job])

    respond_to do |format|
      if @job.save
        format.html { redirect_to @job, notice: 'Job was successfully created.' }
        format.json { render json: @job, status: :created, location: @job }
      else
        format.html { render action: "new" }
        format.json { render json: @job.errors, status: :unprocessable_entity }
      end
    end
  end

  def show
    @job = Job.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @job }
    end
  end
end

如你看到的。它显然在那里。为什么 Rails 看不到它?

4

1 回答 1

5

这是因为您将 create 方法分配为私有。

尝试以这种方式重新定义您的控制器。

这是我的控制器:

class JobsController < ApplicationController

  def index
    ...
  end

  def create
    ...
  end

  def show
    ...
  end

  private

  def load_clients
    @clients = collection_select :client, :client_id
  end

end
于 2012-05-12T14:37:56.683 回答