Leonardo M. wrote:
On Sun, Oct 25, 2009 at 3:23 PM, Misiek Sz
[email protected] wrote:
saved when the form is submitted?
OK, now the situation is clearer for me. There’s something you’re not
getting from associations, let’s see if with the example you get it
more clear.
If you want to create the association when you create the education
plan, why would you try to get it on the get_info action and not on
the create action?
When you get (render) the student info on the create form for
education plan, you should keep the student id and send it with the
rest of the education plan parameters as the
education_plan[student_id].
Then, in the create action, you can do
EducationPlan.new(params[:education_plan]) (or whatever your params
are called) and, if you have the right student_id the relationship
should be created automatically.
If it doesn’t work, you should “pastie” your code so we can debug it
better.
Hope it helps.
–
Leonardo M…
There’s no place like ~
was on the road past two days…
I want to do exactly what you are saying Leonardo.
Here is my code for
education_plans_controller.rb
def create
@education_plan = EducationPlan.new(params[:education_plan])
this is my sneaky way around, which I don’t want to use…You’ll see
what i’m doing when you see the view
@student = Student.find(params[:student_id])
@tutor = Tutor.find(params[:tutor_id])
@education_plan.student = @student
@education_plan.tutor = @tutor
respond_to do |format|
if @education_plan.save
flash[:notice] = 'EducationPlan was successfully created.'
format.html { redirect_to(@education_plan) }
format.xml { render :xml => @education_plan, :status =>
:created, :location => @education_plan }
else
format.html { render :action => “new” }
format.xml { render :xml => @education_plan.errors, :status =>
:unprocessable_entity }
end
end
end
def new
@education_plan = EducationPlan.new
@education_plan.plan_details.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @education_plan }
end
end
education_plan - new view
New Education Plan
<% form_for(@education_plan) do |f| %>
<%= f.error_messages %>
Enter Student OSIS: <%=text_field_tag :osis %><%= image_tag
'loading.gif', :id =>'loading', :style => 'display: none'%>
<div>
<table>
<thead>
<tr>
<th></th><th>Code #</th><th>Start
date
<% 6.times do |i| %>
<% f.fields_for :plan_details do |p| %>
<%= render :partial => ‘new_detail’, :object => p %>
<% end %>
<% end %>
</table>
</div>
Evaluation/Measurement/Assesment |
<%= f.label :comments %> |
<%= f.text_area(:comments, :cols => 30, :rows => 4) %> |
<%= f.submit 'Create'%>
<% end %>
<%= link_to ‘Back’, education_plans_path %>
<%= observe_field(:osis, :frequency => 0.25, :update => :student_info,
:url =>{:controller => ‘students’, :action =>
:get_student_info},
:with => ‘osis’,
:before => “Element.show(‘loading’)”,
:complete => “Element.hide(‘loading’)”) %>
students_controller.rb
def get_student_info
@student = Student.find_by_osis(params[:osis])
if @student.nil?
render :text => ‘Couldn' find the student’
elsif !@student.education_plan.nil?
render :text => ‘Student already has an education plan.’
else
render :partial => true, :action => ‘show’
end
end
students show view
<%=hidden_field_tag(:student_id, @student.id) %>
<%=h @student.first_name + ’ ’ + @student.last_name %>
So, with my workaround it does what I want, but then all kinds of other
problems are introduced i.e. error handling etc.
I hope this illustrates better what I’m trying to do.
Thanks!