I'm a bit new in ruby/rails/POO and I'm a bit lost in a form that I'm realizing. I'm using the gem formtastic and I'm doing it in haml.
I have this model
class Help < ActiveRecord::Base
attr_accessible :answer, :category, :question
validates :category, presence: true, uniqueness: true
validates :question, presence: true
validates :answer, presence: true
end
In my form, I want the possibility to create a new Question/Answer with its category. The category should be selected in a selectbox but if the category I want is not listed yet, I want to have the ability to add it.
Here's the form
= 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
EDIT :
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
When I try to reach /help/new it actually says to me :
undefined method `model_name' for NilClass:Class
The aim is to have in the selectbox, categories already registered, and if the user is not founding the category he wants to use in the selectbox, he can create one by typing it in the input.
Any clues to help me doing this ?
Cordially , Rob
I found a method that does half what i wanted.
It's the method pluck
.
I defined a static method in my model :
def self.getcat
Help.pluck(:category)
end
Then in my form in simply call this method on my collection :
= f.input :category, :as => :select, :collection => Help.getcat