저는 루비/레일/POO에서 약간 새로운데, 저는 깨닫고있는 형태로 조금 잃어 버렸습니다. 나는 formtastic 한 보석을 사용하고 있는데, 나는 그것을 햄에서하고있다.선택 상자 및 값 ruby
나는이 모델을 내 양식에
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
내가 실제로 나에게 말한다/도움말/새로운 접촉을 시도 :
정의되지 않은 메서드`MODEL_NAME '에 대한 클래스 : 클래스
목표는 이미 등록 된 카테고리를 선택 상자에 넣는 것입니다. 사용자가 선택 상자에서 사용하려는 카테고리를 만들지 않으면 입력에 입력하여 카테고리를 만들 수 있습니다.
이 작업을 도와 줄 수있는 단서가 있습니까?
진심으로, 롭
당신은 너무 당신의'help' 컨트롤러의 코드를 추가 할 수 있습니다하시기 바랍니다 나는 나의 모델의 정적 메서드를 정의? –
괜찮음 : – user2462805
': collection => Help.category'를': collection => Category.all'로 변경하십시오. – MrYoshiji