form_for 和范围,rails 3

发布于 2024-09-19 13:19:35 字数 419 浏览 12 评论 0原文

由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

喜你已久 2024-09-26 13:19:35

我想出的答案相当难看,但适用于更新和创建:

<%= form_for(@article, :url => (@article.new_record? ? 
    articles_path(params[:tab]) : article_path(params[:tab], @article) do |f| %>

更新:
更好的解决方案是将 default_url_options-method 重写为如下所示:

def default_url_options(options={})
  { :tab => params[:tab] }
end

然后 <%= form_for @article do |f| %>可以使用,并且所有url都正确生成

The answer I came up with was quite ugly, but works with both update and create:

<%= form_for(@article, :url => (@article.new_record? ? 
    articles_path(params[:tab]) : article_path(params[:tab], @article) do |f| %>

Update:
A better solution would be to override the default_url_options-method to something like this:

def default_url_options(options={})
  { :tab => params[:tab] }
end

Then the <%= form_for @article do |f| %> could be used, and all urls are correctly generated

小兔几 2024-09-26 13:19:35

尝试:

<%= form_for [:tab, @article] do |f| %>
   <%= f.label :title %>
   <%= f.text_field :title %>
    etc.
<%end%>

Try:

<%= form_for [:tab, @article] do |f| %>
   <%= f.label :title %>
   <%= f.text_field :title %>
    etc.
<%end%>
橘味果▽酱 2024-09-26 13:19:35

您可以明确指定路径:

<%= form_for(@article, :url => article_path(@article, :tab => params[:tab]) %>

You could specify the path explicitly:

<%= form_for(@article, :url => article_path(@article, :tab => params[:tab]) %>
物价感观 2024-09-26 13:19:35

我对form_for和scopes的类似问题的解决方案是在helpers//_helper.rb中定义新方法,例如我的是sessions_helper。 rb 其中包含

module Implant::SessionsHelper
  def sessions_form_path(session)
    session.new_record? ? sessions_path : session_path(session)
  end
end

And in my view I made

form_for(@session, url: sessions_form_path(@session)) do |f|

Problematic paths.rb part

scope module: 'implant' do
  resources :sessions
end

... 并使用 :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 contains

module Implant::SessionsHelper
  def sessions_form_path(session)
    session.new_record? ? sessions_path : session_path(session)
  end
end

And in my view I made

form_for(@session, url: sessions_form_path(@session)) do |f|

Problematic routes.rb part

scope module: 'implant' do
  resources :sessions
end

... and to get managed with :tab param you may add it to the helper method.

晌融 2024-09-26 13:19:35

我发现这是一个非常令人恼火的问题,现在已经通过以下猴子补丁解决了这个问题。像这样的通用,它有点出价,因为您只需将整个参数包传递给 polymorphic_url,这就是 form_for 在幕后使用的猜测路线的方法。更简洁的方法是仅合并范围值。

我的解决方案:

https://gist.github.com/1848467

module ActionDispatch
  module Routing
    module PolymorphicRoutes
      def polymorphic_path(record_or_hash_or_array, options = {})
        begin
            polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path))
        rescue Exception => e
            polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path).merge(params.reject{|k,v| ["controller", "action"].include? k.to_s}))
        end
      end
    end
  end
end

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

module ActionDispatch
  module Routing
    module PolymorphicRoutes
      def polymorphic_path(record_or_hash_or_array, options = {})
        begin
            polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path))
        rescue Exception => e
            polymorphic_url(record_or_hash_or_array, options.merge(:routing_type => :path).merge(params.reject{|k,v| ["controller", "action"].include? k.to_s}))
        end
      end
    end
  end
end
魂牵梦绕锁你心扉 2024-09-26 13:19:35

在非常相似的情况下,我在路由中定义了范围,如下所示:

scope :path => ":election_id", :as => "election" do 
  resources :questions
end

现在我有像 election_questions_path(@election) 这样的帮助器

在我可以使用的表单中:

form_for [@election, @question] do |f|
  ...
end

在上面的示例中 @election是选举模型的一个实例。

将Friendly_id集成到这个解决方案中后,我得到了一些漂亮的网址。例如“http://mydomain.com/elections-2012/questions/my-question”

In a very similar situation I defined the scope in routes like below:

scope :path => ":election_id", :as => "election" do 
  resources :questions
end

Now I have helpers like election_questions_path(@election)

In the forms I can use:

form_for [@election, @question] do |f|
  ...
end

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"

君勿笑 2024-09-26 13:19:35

我不确定这可以追溯到多久以前,但它可以在 Rails 6 上运行。我使用:

<%= form_for(@article, url: [@article, { tab: params[:tab] }]) %>
   <%= f.label :title %>
   <%= f.text_field :title %>
    etc.
<% end %>

这可以运行,因为数组 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:

<%= form_for(@article, url: [@article, { tab: params[:tab] }]) %>
   <%= f.label :title %>
   <%= f.text_field :title %>
    etc.
<% end %>

This works because of the array URL generation syntax. In the new case, @article is detected as not being persisted and routes to the POST route. In the edit case, @article is detected as being persisted and routes to the PUT route with the ID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文