koloa wrote:
end
when i tried the render option, it looks for form in my controller
folder.
can the validate be done like this?
I think part of the problem is you’re trying to do things the non-Rails
way – which can be done but it does make things more difficult.
There are two idiomatic Rails ways of doing something like this – the
new 1.2 REST-ful way, and the old verb-based action way. Either way,
Rails works much of its magic by assumptions on your naming convetions
– you can override them, but it’s so much easier if you don’t have to.
Assuming you haven’t got into the RESTful goodness yet – and it’s worth
looking at), I would suggest something like the following (not tested,
and I’ve made some possibly incorrect assumptions about your models):
AllPurposeController < ApplicationController
def new
@item = params[:fid].constantize.new #instantiate new item of class
params[:fid], e.g. “Job”
@ddvd_cover = Repicture.new if params[:fid] == “House”
end
end
def create
@item = params[:fid].constantize.new(params[:item])
if @item.save
flash[:notice] = ‘Profile was successfully created.’
redirect_to :action => ‘index’
else
render :action => ‘new’
end
end
form.rhtml
<%= error_messages_for ‘item’ %>
<%= start_form_tag :action => ‘create’, :fid => @item.class.to_s %>
put stuff here that’s common to all items
<%= render :partial => ‘displays/#{@item.class.to_s.downcase}_form’ %>
can your form_partials “job_form”, “house_form”, etc
<%= submit_tag “Create”%>
<%= end_form_tag%>
_job_form.rhtml
Company
<%= text_field :item, 'company' %>
Description
<%= text_area :item, 'description', :cols => "30", :rows=> "10" %>
...etc...
modelJob
class Job < ActiveRecord::Base
validates_presence_of : company, : description, :on => :create,
:message => “can’t be blank”
end