[英]collection_select with nestled resources
So, my first question on Stackoverflow! 所以,我对Stackoverflow的第一个问题! Lets see how it goes... 让我们看看进展如何...
So I've got a collection_select
who represents the current category you are on: 因此,我有一个collection_select
代表您当前所在的类别:
<%= collection_select(:category, :slug, current_user.categories, :slug, :name) %>
This select represents which category the user currently is on, and the user should be able to switch between the categories. 该选择表示用户当前所在的类别,并且用户应该能够在类别之间切换。
The categories have post like so: 类别的帖子如下:
resources :categories do
resources :posts
end
I want the collection_select
to automatically select the current category based on url parameters, which works so far: 我希望collection_select
根据url参数自动选择当前类别,到目前为止,它仍然有效:
http://myproject.com/categories/action
The selected value is the same as the current url parameter. 所选值与当前url参数相同。 But it does not work when the user navigates to a post : 但是当用户导航到帖子时,它不起作用:
http://myproject.com/categories/action/my-new-post
This resets the collection_select
, I figured it has something to do with nestled resources, what should i do? 这重置了collection_select
,我认为它与嵌套资源有关,我该怎么办?
Just to clarify: i have a very simple javascript to reload the page when user selects a category. 只是要澄清一下:当用户选择类别时,我有一个非常简单的JavaScript重新加载页面。
It sounds like you want to set an initial value for your collection select. 听起来您想为集合选择设置一个初始值。 Something like: 就像是:
<%= collection_select(:category, :slug, current_user.categories, :slug, :name {:selected => params[YOUR_PARAM_NAME_HERE]}) %>
Alternatively, if you can do something like this 或者,如果您可以做这样的事情
<%= collection_select(:category, :slug, current_user.categories, :slug, :name {:selected => @category.slug} %>
Is that about right? 那是对的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.