class Area < ActiveRecord::Base
belongs_to :taskschedule
belongs_to :service
end
class Service < ActiveRecord::Base
has_many :taskschedules, :through => :areas
has_many :areas
end
class Taskschedule < ActiveRecord::Base
has_many :services, :through => :areas
has_many :areas
end
Following View
Taskschedule#new
<h2>New Taskschedule</h2>
<%= form_for @taskschedule, :url=>taskschedules_path do |f| %>
<p>Area: <%= f.text_field :name %></p>
<% for service in Service.find(:all) %>
<p><%= check_box_tag :service_ids, service.id,
@taskschedule.services.include?(service), :name =>
'taskschedule[service_ids]' -%> <%= service.name %></p>
<% end %>
<%= submit_tag "Submit" %>
<% end -%>
Taskschedule Controller
class TaskschedulesController < ApplicationController
def index
@taskschedules = Taskschedule.find(:all)
end
def new
@taskschedule = Taskschedule.new
@taskschedule.build.build_area
end
def create
@taskschedule = Taskschedule.new(params[:taskschedule])
if @taskschedule.save
redirect_to taskschedules_path
end
end
def edit
@taskschedule = Taskschedule.find(params[:id])
end
def update
@taskschedule = Taskschedule.find(params[:id])
if @taskschedule.update_attributes(params[:taskschedule])
redirect_to taskschedules_path
else
render 'edit'
end
end
end
i have a text_field in areas that i want to be able to use in the
taskschedule#new view?
Thanks for any help.
ed
class TaskschedulesController < ApplicationController
def index
@taskschedules = Taskschedule.find(:all)
end
def new
@taskschedule = Taskschedule.new
@taskschedule.build.build_area
end
def create
@taskschedule = Taskschedule.new(params[:taskschedule])
if @taskschedule.save
redirect_to taskschedules_path
end
end
def edit
@taskschedule = Taskschedule.find(params[:id])
end
def update
@taskschedule = Taskschedule.find(params[:id])
if @taskschedule.update_attributes(params[:taskschedule])
redirect_to taskschedules_path
else
render 'edit'
end
end
end
i have a text_field in areas that i want to be able to use in the
taskschedule#new view?
The syntax of your question looks like a statement. What is your question? You are making whoever reads your question have to put thought into what you mean to ask.
<%= form_for @taskschedule, :url=>taskschedules_path do |f| %>
<p>Area: <%= f.text_field :name %></p>
<% for service in Service.find(:all) %>
<p><%= check_box_tag :service_ids, service.id,
@taskschedule.services.include?(service), :name =>
'taskschedule[service_ids]' -%> <%= service.name %></p>
<% end %>
<%= submit_tag "Submit" %>
<% end -%>
as you can see i am assigning services to the taskschedule through the
model "area". that works.
the problem is i have a textfield in the model area that i want to
fill over this view. i tried fields_for(:area) but get an error
message?
thanks,
ed
Ok, I think I understand what you are doing. I think what you should look into is using nested forms, meaning where you can have forms for multiple models and have rails handle all this automatically… if I am understanding, once you post this you have some work to do on the controller side to save your data, right? There are two railscasts (at least) where you could start: