There are many approaches to this problem.  Some involve setting up
the code in the controller, others use a "presenter."  Google
presenter pattern for more info on that.  The "build" method might be
what you are looking for.  This is created as you define your model
relationships.  If you use it, it sets the foreign key id properly.

#controller
def new
  @project = Project.new
  @worktrack = @project.worktracks.build
end

def create
  @product = Project.new(params[:product])
  @worktrack = @project.worktracks.build(params[:worktrack])

  if @project.valid? & @worktrack.valid?
    Project.transaction do
      @project.save!
      @worktrack.save!
    end
  else
    render 'new'
  end
end

If you name the attributes properly in the view, this sort of scheme
will work.  The problem with it is that the controller will become
bloated quickly as you add more complexity.  For example if you add
your tasks piece, it will require another instance variable,
validation, save, etc.  This is where a presenter model can help clean
up the controller.


As for your second question.  You are likely looking for a
"collection_select" helper.

You could do something like this...

collection_select(:post, :author_id,
Author.all, :id, :name_with_initial, {:prompt => true})

collection_select(:post(..this is the object), :author_id(...this is
the method), Author.all(...this is the collection), :id(...this is the
value), :name_with_initial(...this is the text displayed), {:prompt =>
true}(...this is an option))

Andrew

On Dec 9, 1:32 pm, SMR <[email protected]> wrote:
> Good Afternoon.
>
> I am creating a time tracking application as learning exercise.
> Each Project has_many Worktracks, each Worktrack has_many tasks, each
> Proect has_many tasks through Worktracks.
>
> Everything is great - model works, views work, controller functions.
>
> Problem is, how can I have a single view incorporating Projects,
> Worktracks and Tasks, allowing for creating Worktracks within Projects
> (for example) - in one view.
>
> I can do this in irb using '<<', but trying to translate this into
> Rails is proving problematic. Searching this forum, I found a
> Pragmatic book chapter, but the solution was complex and blew up my
> app - undoubtedly, I erred somewhere trying to implement it, but I
> cannot determine where, and at this point, my learning curve is
> flatlined.
>
> Any suggestions for general approach would be welcomed - my ideal
> solution would be to be able to mix @project, @worktrack and @task all
> in the same structure.
>
> As a side-issue, but related, I have a valid_values table that I am
> trying to use to populate drop-down menus within tasks, etc - but
> again, I can only populate via direct coding in the view, rather than
> passing in an array from database query. I know it can be done, but
> the 'how' is defeating me. I suspect the solution is simple, but non-
> obvious to me.
>
> Many thanks in advance for suggestions, and I will accept flames in
> good cheer if accompanied by workable suggestions.

--

You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" 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/rubyonrails-talk?hl=en.


Reply via email to