Forum: Ruby on Rails Help with associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
34ca442e1797773d1634eb9ec1d764b9?d=identicon&s=25 hlists (Guest)
on 2005-12-11 12:44
(Received via mailing list)
Hello all!

I have the following models:

Document belongs to a Task and to a Documentgroup and I want to select
the
Task that Document belongs to using a link where I can select the Task,
but
I also want to allow the user to select the Documentgroup using in the
same
way. How can I maintain the document state between these requests?

Here is the code:

Models:

class Document < ActiveRecord::Base
  belongs_to :task
  belongs_to :documentgroup
end

class Task < ActiveRecord::Base
end

class Documentgroup < ActiveRecord::Base
end



Views:

edit view:

<%= start_form_tag :action => "update" %>

<%= text_field_tag("name", @document.name) %>
<%= text_field_tag("description", @document.description) %>

<%= text_field_tag("task", @task.name) %>
<%= link_to "Select", :action => 'select_task' %>

<%= text_field_tag("documentgroup", @documentgroup.name) %>
<%= link_to "Select", :action => 'select_documentgroup' %>

<%= submit_tag "Change", { 'name' => 'change'} %>

<%= end_form_tag %>


select_task view:

<table>

<% for task in @tasks %>
<tr><td><%= task.name %></td><td><%= link_to "Select", :action =>
"edit",
:value => task.id %></td></tr>
<% end %>

</table>



select_documentgroup view:

<table>

<% for documentgroup in @documentgroups %>
<tr><td><%= documentgroup.name %></td><td><%= link_to "Select", :action
=>
"edit", :value => documentgroup.id %></td></tr>
<% end %>

</table>
This topic is locked and can not be replied to.