Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7173539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:50:43+00:00 2026-05-28T15:50:43+00:00

Problem domain We have Projects and Milestones Once created, a Milestone has a Project

  • 0

Problem domain

  • We have Projects and Milestones
  • Once created, a Milestone has a Project

Routing

In my original approach, I had something like:

  resources :projects do
    resources :milestones
  end

So all the RESTful routes looked like: projects/PROJ_ID/milestones/ID

This was getting a bit long for my liking and you don’t need the Project ID all the time: only just when you create the Milestone.

Now, the routes look like:

  resources :projects do
    resources :milestones, :only => [:new, :create]
  end

  resources :milestones, :except => [:new, :create]

When I create a new Milestone (first route):

  • I use the nested resource
  • This is so as to get the Project ID
  • We are creating a new Milestone, we need a Project ID

With other Milestone operations (second route):

  • I use standard Milestone resource
  • We’re already operating on an existing Milestone
  • The Milestone already carries a Project ID

Problem: Rendering __form.html.erb

All is fine, except when I get to the views for new.html.erb and edit.html.erb for Milestone. These render _form.html.erb.

Original approach (all resources nested)

<%= form_for([@project, @milestone]) do |f| %>
  ...
<% end %>

Due to nested resource, we need to have Project and Milestone as parameters.

The same code above works fine for new and edit. because the original approach assumed Project/Milestone nested routing for all REST operations.

Current approach (only create/new nested)

We want _form.html.erb to look like this when we are creating a new Milestone:

<%= form_for([@project, @milestone]) do |f| %>
  ...
<% end %>

And for all other REST operations on a Milestone, we want _form.html.erb to look like this:

<%= form_for(@milestone) do |f| %>
  ...
<% end %>

So …

Current solution is not pretty:

  • View code checks if “new” or non-“new” action called it
  • Use “if” test to choose which code (as above) the view should execute

It’s not DRY, it’s messy and it feels like there should be a more elegant way.

Would appreciate any input. Very much still coming up to speed with Rails. Thanks in advance 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:50:44+00:00Added an answer on May 28, 2026 at 3:50 pm

    You can pass variable with you resource to _form partial. For example:

    New:

    render "form", :resource => [@project, @milestone]
    

    Edit:

    render "form", :resource => @milestone
    

    Inside the form

    <%= form_for resource do |f| %>
      ...
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three projects in my C# 4 solution. MooDB: Has my domain objects
I have this problem... I have a VS solution with these projects: Persistance, Domain,
I've got a problem where I have a .co.uk domain of which I am
I have a problem with the code analysis rule CA1726:UsePreferredTerms . Our business domain
Has anyone else had any problems using google's Domain Tracking API, I am specifically
Problem: I have an address field from an Access database which has been converted
I have the following project structure using a Domain Model, StructureMap and Fluent NHibernate:
I have created a RIA services class library project and things are not going
imagine the following problem in grails you have some kind of audit trail domain
I have always developed my projects using MAMP locally and once done simply uploaded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.