form_for 和范围,rails 3
由于 Rails 3 中的作用域和 form_for 助手,我遇到了问题。 路线 - 文件看起来像这样:
scope "(/:tab)" do
resources :article
end
表单看起来像这样:
<%= form_for(@article) %>
<%= f.label :title %>
<%= f.text_field :title %>
etc.
<%end%>
tab - 属性作为字符串存储在 params[:tab] 中 我的问题是这会在表单中生成错误的网址。我怎样才能让它发挥作用? 类型化的 urlarticle_path(params[:tab], @article) 工作得很好
I have a problem due to scopes and the form_for helper in rails 3.
The routes - file looks like this:
scope "(/:tab)" do
resources :article
end
The form looks something like this:
<%= form_for(@article) %>
<%= f.label :title %>
<%= f.text_field :title %>
etc.
<%end%>
The tab - attribute is stored in params[:tab], as a string
My problem is that this genereate wrong urls in the form. How could I get this to work ?
The genreated url article_path(params[:tab], @article) works perfectly fine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我想出的答案相当难看,但适用于更新和创建:
更新:
更好的解决方案是将 default_url_options-method 重写为如下所示:
然后 <%= form_for @article do |f| %>可以使用,并且所有url都正确生成
The answer I came up with was quite ugly, but works with both update and create:
Update:
A better solution would be to override the default_url_options-method to something like this:
Then the <%= form_for @article do |f| %> could be used, and all urls are correctly generated
尝试:
Try:
您可以明确指定路径:
You could specify the path explicitly:
我对form_for和scopes的类似问题的解决方案是在
helpers//_helper.rb
中定义新方法,例如我的是sessions_helper。 rb 其中包含And in my view I made
Problematic paths.rb part
... 并使用
:tab
参数进行管理,您可以将其添加到辅助方法中。My solution of similar problem with form_for and scopes is to define new method in
helpers/<model_name>/<model_name>_helper.rb
, for example mine is sessions_helper.rb which containsAnd in my view I made
Problematic routes.rb part
... and to get managed with
:tab
param you may add it to the helper method.我发现这是一个非常令人恼火的问题,现在已经通过以下猴子补丁解决了这个问题。像这样的通用,它有点出价,因为您只需将整个参数包传递给 polymorphic_url,这就是 form_for 在幕后使用的猜测路线的方法。更简洁的方法是仅合并范围值。
我的解决方案:
https://gist.github.com/1848467
I have found this to be a really irritating problem and have gotten around this for now with the following monkey patch. Generic like this, it's a little bid off as you're simply passing the entire bag of parameters to polymorphic_url which is what form_for uses under the hood to guess the route. A more concise approach would be to merge just the scope value.
My solution:
https://gist.github.com/1848467
在非常相似的情况下,我在路由中定义了范围,如下所示:
现在我有像
election_questions_path(@election)
这样的帮助器在我可以使用的表单中:
在上面的示例中
@election
是选举模型的一个实例。将Friendly_id集成到这个解决方案中后,我得到了一些漂亮的网址。例如“http://mydomain.com/elections-2012/questions/my-question”
In a very similar situation I defined the scope in routes like below:
Now I have helpers like
election_questions_path(@election)
In the forms I can use:
In above examples
@election
is an instance of the Election model.After integrating Friendly_id into this solution I got some pretty urls. For example "http://mydomain.com/elections-2012/questions/my-question"
我不确定这可以追溯到多久以前,但它可以在 Rails 6 上运行。我使用:
这可以运行,因为数组 URL 生成语法。在
new
情况下,@article
被检测为未持久保存并路由到POST
路由。在edit
情况下,@article
被检测为持续存在,并路由到带有 ID 的PUT
路由。I'm not sure how far back this goes, but it works on Rails 6. I use:
This works because of the array URL generation syntax. In the
new
case,@article
is detected as not being persisted and routes to thePOST
route. In theedit
case,@article
is detected as being persisted and routes to thePUT
route with the ID.