Rails:自动填充嵌套资源的参考
我有一个嵌套在资源“组”内的资源“项目”。在我的节目组页面上,我列出了该组拥有的所有项目。我还添加了一个“新项目”链接,
new_group_project_path(@group)
但在下面的新项目表单中,用户仍然需要输入组的 ID,即使他只是单击特定组页面上的“新建项目”链接。我尝试这样做,
<%= f.text_field :group_id, :value => @group.id %>
但 @group 似乎为零,即使我在链接中传递了它。
我怎样才能用 group_id 自动填充这个字段,或者甚至对用户隐藏整个内容,因为它已经预先确定,他不应该考虑这个?
谢谢,
MrB
[编辑]
这是我需要澄清的更多代码:
1 class ProjectController < 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
在 _form.html.erb
26 <% if @group.nil? %>
25 <div class="field">
26 <%= f.label :group_id %><br />
27 <%= f.text_field :group_id %>
28 </div>
29 <% end %>
Routes.rb 中:
5 resources :groups do
4 resources :projects
5 end
6
7 resources :projects
希望这会有所帮助。谢谢。
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
new_group_project_path(@group)
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
<%= f.text_field :group_id, :value => @group.id %>
But @group seems to be nil, even though I passed it in the link.
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?
Thanks,
MrB
[edit]
Here's more of my code to clarify:
1 class ProjectController < 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
And in the _form.html.erb
26 <% if @group.nil? %>
25 <div class="field">
26 <%= f.label :group_id %><br />
27 <%= f.text_field :group_id %>
28 </div>
29 <% end %>
Routes.rb:
5 resources :groups do
4 resources :projects
5 end
6
7 resources :projects
Hope this helps. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您已将控制器嵌套在 paths.rb 中,那么您不需要在表单中执行任何额外操作。因此,如果您的routes.rb看起来像这样:
从您的path_helper看起来像这样,那么您将始终从路由中获得可用的group_id。因此,当用户将表单提交到 ProjectsController 中的创建操作时,您可以像这样处理它:
编辑:
如果 @group 在您的表单中为零(我猜它在新操作中被调用),那么您可以选择在新操作中添加 @group = Group.find(params[:group_id]) (这将违背 Rails 的 DRY 原则),或者按照 Ryan 的建议将其添加到 before_filter 中,如下所示
:将使@group 变量在此控制器中的所有操作中可用。 private 声明仅确保 Controller 之外的任何内容都无法调用 find_group。
If you have nested the controllers in your routes.rb then you should not need to do anything extra in the form. So if your routes.rb looks something like this:
which it looks like from your path_helper, then you will always have the group_id available from the routing. So when the user submits the form to your create action in ProjectsController, you could handle it something like this:
Edit:
If @group is nil in your form (I'm guessing it is called in the new action) then you have the either add the @group = Group.find(params[:group_id]) in your new action as well (which would go against the DRY principal of Rails) or do what Ryan suggested and add it in a before_filter which could look like this:
That will make the @group variable available in all actions in this controller. The declaration of private only makes sure that nothing outside the Controller can make the call to find_group.