Ruby on rails 4 Rails 4中的新表单失败,其中一个表单字段的方法未定义
轨道4.1 Ruby 2.0 Windows 8.1 当我尝试创建一个新的业余爱好者时,我得到以下错误:Ruby on rails 4 Rails 4中的新表单失败,其中一个表单字段的方法未定义,ruby-on-rails-4,ruby-2.0,Ruby On Rails 4,Ruby 2.0,轨道4.1 Ruby 2.0 Windows 8.1 当我尝试创建一个新的业余爱好者时,我得到以下错误: undefined method `organization' for #<Hobbyist:0x000000070d0ac8> 任何想法: 这是控制器: class hobbyistsController < ApplicationController before_action :set_hobbyist, only: [:show, :edit, :upda
undefined method `organization' for #<Hobbyist:0x000000070d0ac8>
任何想法:
这是控制器:
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 %>
这是业余爱好者的观点
<%= 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 %>
表中缺少组织。我通过添加一个迁移来包括组织,从而解决了这个问题。问题中的小写h是在发布stackoverflow之前使用智能编辑器撰写问题的副作用。请确认您的爱好者模型中有组织结构-可以作为表check schema.db中的物理列,也可以作为属性显示您的爱好者模型。此外,您的控制器代码显示为小写在你定义类的第一行,以及你引用模型的地方,比如hobbyist.new,不知道这是否就是你在这里展示代码的方式,因为如果你的控制器真的是这样的话,你肯定会遇到更多的问题。小写的h是因为我在智能编辑器中创建了这个问题。问题是组织不在表中,其他所有字段都在表中。我为它创建了一个迁移,解决了这个问题。很高兴你修复了它。您可能应该发布并接受答案,或者删除该问题,使其不再显示为未回答的问题。
<%= 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 %>