Note that there are some explanatory texts on larger screens.

plurals
  1. PORails: Auto-fill reference on nested resource
    primarykey
    data
    text
    <p>I have a resource "Project" nested inside a resource "Group". On my show group page, I list all the projects the group has. I also have added a "New Project" link leading to </p> <pre><code>new_group_project_path(@group) </code></pre> <p>But in the following new project form, the user still needs to enter the id of the group, even though he just clicked the New Project link on a specific group's page. I tried doing</p> <pre><code>&lt;%= f.text_field :group_id, :value =&gt; @group.id %&gt; </code></pre> <p>But @group seems to be nil, even though I passed it in the link.</p> <p>How can I auto-fill this field with the group_id, or even hide the whole thing from the user, since it's already predetermined and he shouldn't have to think about that?</p> <p>Thanks,</p> <p>MrB</p> <p>[edit]</p> <p>Here's more of my code to clarify:</p> <pre><code> 1 class ProjectController &lt; ApplicationController 2 def new 3 @group = Group.find(params[:group_id]) 4 @project = @group.projects.build 5 respond_with(@project) 6 end 7 8 def create 9 @group = Group.find(params[:group_id]) 10 @project = @group.projects.build(params[:projects]) 11 end 12 end </code></pre> <p>And in the _form.html.erb</p> <pre><code> 26 &lt;% if @group.nil? %&gt; 25 &lt;div class="field"&gt; 26 &lt;%= f.label :group_id %&gt;&lt;br /&gt; 27 &lt;%= f.text_field :group_id %&gt; 28 &lt;/div&gt; 29 &lt;% end %&gt; </code></pre> <p>Routes.rb:</p> <pre><code> 5 resources :groups do 4 resources :projects 5 end 6 7 resources :projects </code></pre> <p>Hope this helps. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload