Unfortunately, this won't work either.
Because there many-to-many association (has_and_belongs_to_many)
association between two tables, external ID is stored in join table
(in my case projects_tasks).
What I figured out is that naming <select> control with either :tasks
or :task_id doesn't work.
I managed to get all working with following changes:
1. app/views/projects/new.html.erb and app/views/projects/
edit.html.erb
...
<%= select :name => "tasks", :label => "Tasks", :multiple =>
"true", :value_method => :id, :text_method => :task_name, :collection
=> Task.all %>
...
2.
controllers/projects.rb
...
def create(project, tasks)
@tasks = Task.all(:id => tasks.to_a)
@project = Project.new(project.merge(:tasks => @tasks))
if @project.save
...
def update(id, project, tasks)
@tasks = Task.all(:id => tasks.to_a)
@project = Project.get(id)
@tasks.each do |task|
@project.tasks << task
end
raise NotFound unless @project
if @project.update_attributes(project)
...
With this changes I managed to get staring, but I'm not sure if this
is the right way.
z
On Mar 9, 3:44 pm, marshall <[email protected]> wrote:
> A few days ago, I've had the same puzzle. :)
>
> Try to change the *:tasks* to *:task_id*
>
> <%= select :tasks, :label =>
> "Tasks", :value_method=> :id, :text_method => :task_name, :collection
> => Task.all %>
> ^^^ => :task_id
>
> On Mar 6, 12:19 am, zdenko <[email protected]> wrote:
>
>
>
> > hi,
>
> > I have two models with many to many associations:
>
> > class Project
> > include DataMapper::Resource
>
> > property :id, Serial
> > property :project_notes, Text
> > property :project_name, String
> > property :estimate, Integer
> > property :project_code, String
>
> > has n, :tasks, :through => Resource
> > end
>
> > class Task
> > include DataMapper::Resource
>
> > property :id, Serial
> > property :hour_rate, Float
> > property :task_name, String
> > property :attr, Integer
>
> > has n, :projects, :through => Resource
> > end
>
> > In the router.rb I have this:
> > Merb::Router.prepare do
> > resources :projects do |project|
> > project.resources :tasks
> > end
> > ....
>
> > For the association in the view I'm using a select control.
>
> > <%= select :tasks, :label => "Tasks", :value_method
> > => :id, :text_method => :task_name, :collection => Task.all %>
>
> > And this is HTML code of the form:
> > <form method="post" action="/projects/1">
> > <input type="hidden" value="put" name="_method">
> > <p>
> > <label for="project_project_name">Project name</label><input
> > type="text" class="text" value="test 1" name="project[project_name]"
> > id="project_project_name">
> > </p>
> > <p>
> > <label for="project_project_code">Project code</label><input
> > type="text" class="text" value="t101" name="project[project_code]"
> > id="project_project_code">
> > </p>
> > <p>
> > <label for="project_project_notes">Notes</label>
> > <textarea name="project[project_notes]"
> > id="project_project_notes">
> > </textarea>
> > </p>
> > <p>
> > <label for="project_tasks">Tasks</label> <select
> > name="project
> > [tasks]" id="project_tasks">
> > <option selected="selected" value="1">
> > task 1
> > </option>
> > <option value="2">
> > task 2
> > </option>
> > </select>
> > </p>
> > <p>
> > <input type="submit" value="Update" name="submit"
> > id="submit">
> > </p>
> > </form>
>
> > When the model gets saved I get error:
> > No Method Error 500
> > undefined method `collection=' for "2":String
>
> > Params: {"format"=>nil, "submit"=>"Update", "project"=>
> > {"project_notes"=>"", "project_name"=>"test 1", "tasks"=>"4",
> > "project_code"=>"dsds"}, "action"=>"update", "_method"=>"put",
> > "id"=>"1", "controller"=>"projects"}
>
> > I guess the actual problem is in incorrect naming of the select field.
> > Could somebody point me in the right direction?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"merb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/merb?hl=en
-~----------~----~----~----~------~----~------~--~---