Rails 4中的新表单失败,其中一个表单字段的方法未定义

时间:2014-05-09 12:05:00

标签: ruby-on-rails-4 ruby-2.0

Rails 4.1 Ruby 2.0 Windows 8.1

当我尝试创建一个新的爱好者时,我收到以下错误:

undefined method `organization' for #<Hobbyist:0x000000070d0ac8>

使用以下附加错误详细信息:

    block in _app_views_hobbyists__form_html_erb__2100369257_59197480
    app/views/hobbyists/_form.html.erb, line 8
    _app_views_hobbyists__form_html_erb__2100369257_59197480
    app/views/hobbyists/_form.html.erb, line 1
    _app_views_hobbyists_new_html_erb__583593320_59167000
    app/views/hobbyists/new.html.erb, line 3

任何想法:

这是控制器:

  class hobbyistsController < ApplicationController
  before_action :set_hobbyist, only: [:show, :edit, :update, :destroy]

  # GET /hobbyists
  # GET /hobbyists.json
  def index
    @hobbyists = hobbyist.all
  end

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

  # GET /hobbyists/new
  def new
    @hobbyist = hobbyist.new
  end

  # GET /hobbyists/1/edit
  def edit
  end

  # POST /hobbyists
  # POST /hobbyists.json
  def create
    @hobbyist = hobbyist.new(hobbyist_params)

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

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

  # DELETE /hobbyists/1
  # DELETE /hobbyists/1.json
  def destroy
    @hobbyist.destroy
    respond_to do |format|
      format.html { redirect_to hobbyists_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def hobbyist_params
      params.require(:hobbyist).permit(:first, :last, :salutation, :organization, :work_phone, :mobile_phone, :fax_phone, :other_phone, :address1, :address2, :city, :state, :zip)
    end
end

这是业余爱好者的新观点

<h1>New hobbyist</h1>

<%= render 'form' %> (THIS IS LINE 3)

<%= link_to 'Back', hobbyists_path %>

这是业余爱好者__form视图

<%= simple_form_for(@hobbyist) do |f| %> (THIS IS LINE 1)
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :first %>
    <%= f.input :last %>
    <%= f.input :salutation %>
    <%= f.input :organization %> (THIS IS LINE 8)
    <%= f.input :work_phone %>
    <%= f.input :mobile_phone %>
    <%= f.input :fax_phone %>
    <%= f.input :other_phone %>
    <%= f.input :address1 %>
    <%= f.input :address2 %>
    <%= f.input :city %>
    <%= f.input :state %>
    <%= f.input :zip %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

1 个答案:

答案 0 :(得分:0)

表中缺少“组织”。我通过添加迁移来包含“组织”来解决问题。问题中的小写h是在发布stackoverflow之前使用“智能”编辑器撰写问题的副作用。