在静态页面上呈现部分表单

时间:2018-02-07 10:19:58

标签: ruby-on-rails ruby

我有一个名为home的静态页面,并使用scaffold生成一个名为Specialists的表单,当我尝试使用以下代码渲染表单时

<%= render :partial => 'specialists/form', locals: {specialist: @specialist} %>

我收到以下错误undefined method "errors" for nil:NilClass,这是错误的代码段

提取的来源(第2行):

<%= form_with(model: specialist, local: true) do |form| %>
  <% if specialist.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(specialist.errors.count, "error") %> prohibited this specialist from being saved:</h2>

      <ul>

我试图检查以前的类似问题,但没有一个因此而出现此错误,我对Ruby on Rails很新。

控制器代码

class SpecialistsController < ApplicationController
  before_action :set_specialist, only: [:show, :edit, :update, :destroy]

  # GET /specialists
  # GET /specialists.json
  def index
    @specialists = Specialist.all
  end

  # GET /specialists/1
  # GET /specialists/1.json
  def show
  end

  # GET /specialists/new
  def new
    @specialist = Specialist.new
  end

  # GET /specialists/1/edit
  def edit
  end

  # POST /specialists
  # POST /specialists.json
  def create
    @specialist = Specialist.new(specialist_params)

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

  # PATCH/PUT /specialists/1
  # PATCH/PUT /specialists/1.json
  def update
    respond_to do |format|
      if @specialist.update(specialist_params)
        format.html { redirect_to @specialist, notice: 'Specialist was successfully updated.' }
        format.json { render :show, status: :ok, location: @specialist }
      else
        format.html { render :edit }
        format.json { render json: @specialist.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /specialists/1
  # DELETE /specialists/1.json
  def destroy
    @specialist.destroy
    respond_to do |format|
      format.html { redirect_to specialists_url, notice: 'Specialist was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_specialist
      @specialist = Specialist.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def specialist_params
      params.require(:specialist).permit(:firstname, :lastname, :phone, :email, :interest)
    end
end'

1 个答案:

答案 0 :(得分:0)

尝试以下

在控制器上,我想错过了一些东西,试试下面的

@Component 
public class Employee implements SuperClass {

    @Autowired
    private FileTraverse fileTraverse;

    @Autowired
    private DatabaseAccessDao dbAccess;

    @Override 
    public void execute() throws Exception {
        List<String> traverse = fileTraverse.getFiles();
        Boolean t = isFileTraversed(traverse);
    } 

    @Override
    private Boolean isFileTraversed(List<String> param1) {
        Boolean flag;
        flag = dbAccess.checkFileTraversed(param1);
        return flag;
    }
}

在表格上

def new
    @specialist = Specialist.new
end

或更新如下表格

<%= form_with model: @specialist, local: true do |form| %>

Rails 5指南说明验证请参阅here

关于<%= form_with scope: :specialist, url: specialists_path, local: true do |form| %> 您可以了解以下link

的更多信息

希望有所帮助