Search code examples
ruby-on-railsactionview

Ruby on Rails, ActionView UrlHelper link_to new object


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.


Solution

  • 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].