0

I have two models "projectaim" and "tender", relation:

class Projectaim < ActiveRecord::Base
  has_many :tenders
end

... almost standard controllers

... and views: lets say that master object @projectaim is already created.

On its edit view edit.html.erb I have to make a link to create subordinate @tender on separate form:

 <% @tender.projectaim_id = @projectaim.id %>
 <%=link_to "Add new tender", new_tender_path(@tender)%>

Could you please advise me how to parametrize new tender action (or view) to hold relation based on models? Technically I need to fill tender.projectaim_id item in new tender action? Thanks to all.

Zdenek Sin
  • 25
  • 2

1 Answers1

0

You should probably use nested resources in this case.

Your routes file would look something like this

resources :projectaims do
  resources :tenders
end

and you would make a link like this

<%=link_to "Add new tender", new_projectaim_tender_path(@projectaim, @tender)%>

Inside your controller you will now have params[:projectaim_id] in addition to params[:id].

Dabrorius
  • 1,039
  • 1
  • 8
  • 17
  • Great hint, thank you very much for information. Now Im trying to edit controller and views manually with parial success. Tell me please, is there any way to regenerate them because params and form_for? Thakns. – Zdenek Sin Mar 03 '15 at 14:41
  • I'm not sure if I understand your question correctly, but you if you need to get form_for helper working with nested resources you can check out this SO question http://stackoverflow.com/questions/2034700/form-for-with-nested-resources – Dabrorius Mar 03 '15 at 16:42
  • I thing you understand me well, the post is valuable. I have to edit proper controller and views manually. The principles is that both objects should be created in tenders controller (@projectaim and @tenders) and then I can use it on tenders form form_for([@projectaim, @tenders]). I have to customize controller an params manually, there is probably not any automation if generating scaffolds. Thank you for help. – Zdenek Sin Mar 03 '15 at 17:21
  • Great, feel free to mark the answer as correct if your issue has been resolved :) thanks – Dabrorius Mar 03 '15 at 17:35
  • It looked like url helper issue In the beginning, then I found that I need nested resources and controllers customizations. great post about this issue is here [link](https://gist.github.com/jhjguxin/3074080) – Zdenek Sin Mar 06 '15 at 09:40