简体   繁体   English

Rails 4新表单默认来自params

[英]Rails 4 new form defaults from params

I am using form_for in the _form.html.erb view in order to create my form for both the edit and new actions, as per a standard scaffold. 我正在_form.html.erb视图中使用form_for ,以便根据标准支架为编辑操作和新操作创建表单。

I have a model Owner which has_many pets. 我有一个has_many宠物的模型所有者。

I would like to put an html link on my views/owners/show.html.erb to create a new pet for said owner. 我想在我的views/owners/show.html.erb上放置一个html链接,以为所述所有者创建新的宠物。 This link will point to the new action of pets_controller.rb which when accessed will render the view in pets/new.html.erb 此链接将指向pets_controller.rb的新操作,该操作在访问后将在pets/new.html.erb呈现视图。

What I want to happen is for the the owner_id to be passed with the link in the url to the new action of pets_controller.rb and then be used as the default for a collection_select in pets/new.html.erb 我想要发生的是将owner_id与URL中的链接传递到pets_controller.rb的新操作,然后用作pets/new.html.erb collection_select的默认值

So I have a link to create a new pet but because that link was on a specific owner page, I want the form to create a new pet to have that owner already set, so the user does not have to select from the list. 因此,我有一个创建新宠物的链接,但是由于该链接位于特定的所有者页面上,因此我希望表单创建一个新宠物,使其已经设置了该所有者,因此用户不必从列表中进行选择。

This has to be done without changing the behaviour of the edit action/view in pets. 必须在不更改宠物中编辑动作/视图行为的情况下完成此操作。

I know that I can pass GET arguments then access them in the controller via params, then create variables in the action which are passed to the view. 我知道我可以传递GET参数,然后通过params在控制器中访问它们,然后在操作中创建变量,这些变量将传递给视图。 I can then manually check for a default and set it in the view. 然后,我可以手动检查默认值并在视图中进行设置。 I do not need assistance in coding if this is the only solution. 如果这是唯一的解决方案,我不需要编码方面的帮助。

Is there is a better way to do this? 有没有更好的方法可以做到这一点? A format with which I can pass the params such that the view will just pick them up? 我可以通过这些格式传递参数,以便视图将其拾取吗? Without manually editing my controllers and views? 是否无需手动编辑控制器和视图?

While my personal inclination would be to do as you said and pass a parameter in the link helper and then access the params array in the pets view, you may find that this is the perfect opportunity to explore Nested Resources . 尽管我个人倾向于按照您所说的做,然后在链接帮助器中传递参数,然后在pets视图中访问params数组,但您可能会发现这是探索Nested Resources的绝佳机会。 Essentially, you could declare owners/:owner_id/pets/:pet_id route with: 本质上,您可以使用以下命令声明owner /:owner_id / pets /:pet_id路由:

resources :owners do
  resources :pets
end

You could then link to this route, and reference :owner_id without having to append the query string to the URI (making somewhat cleaner for reuse). 然后,您可以链接到该路由,并引用:owner_id,而不必将查询字符串附加到URI(使重新使用起来更为简洁)。

This is likely more work for you, but also potentially more extensible (and certainly more inline with the Rails way of doing things). 这可能为您带来更多工作,但也可能更具扩展性(并且当然与Rails的处事方式更加内联)。

REVISION 改版

Added the following regarding link helpers to the comments, but wanted to reflect it in the answer as well. 在注释中添加了以下有关链接助手的内容,但也希望在答案中反映出来。

To show a pet should be: 展示宠物应该是:

<%= link_to owner_pet_path( owner_variable, pet_variable) %>

To view pets' index index should be: 查看宠物的索引索引应该是:

<%= link_to owner_pet_path( owner_variable ) %>

The answer given to this question is fantastic. 这个问题的答案是太棒了。

As @ConnorCMcKee suggests it would be wise to consider nesting your routes. 正如@ConnorCMcKee所建议的那样,考虑嵌套路由是明智的。 However, if you are a beginner as myself I found that it helped my learning to simply nest my second controller into the first (ie nest PetsController into OwnersController) as a first step. 但是,如果您像我这样的初学者,那么我发现它的第一步是将我的第二个控制器简单地嵌套到第一个控制器中(即,将PetsController嵌套到OwnersController中),这对我的学习有所帮助。 Then afterwards I would continue with the routes. 然后,我将继续路线。

The method would be something like: 该方法将类似于:

1./ In owners/index.html.erb : 1. /在owner / index.html.erb中

Links to PetsController index action 链接到PetsController索引操作

The key to make this work is to send the :owner_id in your link parameters. 进行此操作的关键是在链接参数中发送:owner_id Then that Pets index action will have access to that :owner_id and know which :owner_id called it. 然后,该Pets索引操作将可以访问该:owner_id并知道哪个:owner_id调用。

2./ In PetsController you would then be able to find that Owner using that id, like so: 2. /在PetsController中,您将能够使用该ID查找该所有者,如下所示:

params[:owner_id]

Then your actions can start to take advantage of knowing what Owner called them. 然后,您的操作就可以开始利用了解所有者称呼它们的优势。 Remember though that all your redirects inside your PetsController need to preserve params[:owner_id] . 请记住,尽管您在PetsController内部进行的所有重定向都需要保留params[:owner_id] That is because once you are inside that nested structure you have to maintain it and stay inside it and always know which :owner_id you are working with. 这是因为一旦您进入该嵌套结构,就必须对其进行维护并留在其中,并始终知道要使用哪个:owner_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM