form_for and scopes, rails 3

前端 未结 7 2080
执念已碎
执念已碎 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 05:57

    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
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-19 06:07

    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.

    0 讨论(0)
  • 2020-12-19 06:14

    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"

    0 讨论(0)
  • 2020-12-19 06:17

    Try:

    <%= form_for [:tab, @article] do |f| %>
       <%= f.label :title %>
       <%= f.text_field :title %>
        etc.
    <%end%>
    
    0 讨论(0)
  • 2020-12-19 06:18

    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.

    0 讨论(0)
提交回复
热议问题