form_for and scopes, rails 3

前端 未结 7 2095
执念已碎
执念已碎 2020-12-19 05:37

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
         


        
7条回答
  •  太阳男子
    2020-12-19 06:02

    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

提交回复
热议问题