1

私は ruby​​/rails/POO に少し慣れていません。私はgem formtasticを使用しており、hamlで実行しています。

私はこのモデルを持っています

class Help < ActiveRecord::Base
  attr_accessible :answer, :category, :question

  validates :category, presence: true, uniqueness: true
  validates :question, presence: true
  validates :answer, presence: true
end

私のフォームでは、そのカテゴリで新しい質問/回答を作成できるようにしたいと考えています。カテゴリは選択ボックスで選択する必要がありますが、必要なカテゴリがまだリストされていない場合は、追加できるようにしたいと考えています。

フォームはこちら

 = semantic_form_for @help do |f|
  = f.inputs do
    = f.input :category, :as => :select, :collection => Help.category
    = f.input :category
    = f.input :question
    = f.input :answer

  = f.action :submit, :as => :button

編集 :

class HelpsController < ApplicationController
  # GET /helps
  # GET /helps.json
  def index
    @helps = Help.all.sort_by {|f| f.category}

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @helps }
    end
  end

  # GET /helps/1
  # GET /helps/1.json
  def show
    @help = Help.find(params[:id])

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

  # GET /helps/new
  # GET /helps/new.json
  def new
    @help = Help.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @help }
    end
  end

  # GET /helps/1/edit
  def edit
    @help = Help.find(params[:id])
  end

  # POST /helps
  # POST /helps.json
  def create
    @help = Help.new(params[:help])

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

  # PUT /helps/1
  # PUT /helps/1.json
  def update
    @help = Help.find(params[:id])

    respond_to do |format|
      if @help.update_attributes(params[:help])
        format.html { redirect_to @help, notice: 'Help was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @help.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /helps/1
  # DELETE /helps/1.json
  def destroy
    @help = Help.find(params[:id])
    @help.destroy

    respond_to do |format|
      format.html { redirect_to helps_url }
      format.json { head :no_content }
    end
  end
end

/help/new にアクセスしようとすると、実際には次のように表示されます。

NilClass:Class の未定義メソッド `model_name'

目的は、選択ボックスにカテゴリが既に登録されていることです。ユーザーが選択ボックスで使用したいカテゴリを作成していない場合は、入力に入力して作成できます。

これを行うのに役立つ手がかりはありますか?

敬具 ロブ

4

2 に答える 2

1

これを試して:

= f.collection_select :category
于 2013-07-04T17:10:16.587 に答える