'form_for' seems lost 'scope' message. #3935

Closed
jimrok opened this Issue Dec 11, 2011 · 8 comments

Projects

None yet

7 participants

@jimrok
jimrok commented Dec 11, 2011

In my rails(3.1.3),

I have defined a Group model using:

$ rails g scaffold group name:string

Then I add the following in routes.rb

scope ":network",:constraints => {:network => /[^\/]+/} do
    resources :users
    resources :groups
end

Next, I access the webpage with the url 'http://localhost:3000/abc.com.cn/groups/1/edit', the returned form's action url as following

<form accept-charset="UTF-8" action="/1/groups/1" class="es-form-group" id="edit_group_1" method="post">
.....

but, what I expect is /abc.com.cn/groups/1,as the following

<form accept-charset="UTF-8" action="/abc.com.cn/groups/1" class="es-form-group" id="edit_group_1" method="post">
.....

rails seams lost the scope information in params[:network].
I checked the source code of route_set.rb

def define_hash_access(route, name, kind, options)
    selector = hash_access_name(name, kind)

            # We use module_eval to avoid leaks
            @module.module_eval <<-END_EVAL, __FILE__, __LINE__ + 1
              remove_possible_method :#{selector}
              def #{selector}(*args)
                options = args.extract_options!
                result = #{options.inspect}
                if args.any?
                  result[:_positional_args] = args
                  result[:_positional_keys] = #{route.segment_keys.inspect}
                end
                result.merge(options)
              end
              protected :#{selector}
            END_EVAL
            helpers << selector
          end

In my case ,The result[:_positional_keys] seems not match the result[:_positional_args] in array size.
The result[:_positional_keys] has three entries,but result[:_positional_args] only has one,the debug message as the following:

:_positional_args=>[#<Group id: 1, name: "my group", created_at: "2011-12-09 08:43:19", updated_at: "2011-12-09 08:43:19">], :_positional_keys=>[:network, :id, :format]

I think this maybe a bug for rails 's route, someone can give me an advice?

PS: I'm a ruby/rails beginner…I still not understand how the rails'route works, hope the information will be helpful.

@drogus drogus was assigned Dec 11, 2011
@drogus
Member
drogus commented Dec 11, 2011

I think this may be a bug, since /network-name/groups/new renders url with network attached, I will investigate this.

@jimrok
jimrok commented Dec 12, 2011

yes, i notice the same thing. so, I looked at the souce code, and found the following in function 'define_hash_access' of route_set.rb file

if args.any?
        result[:_positional_args] = args
        result[:_positional_keys] = #{route.segment_keys.inspect}
 end

In case of '/network-name/groups/new', The 'args.any?' will be nil. so, the network name will be attached from the url_options when hash merge happend. but I am still not know why the code should write like this.

@isaacsanders
Contributor

Is this still an issue?

@jimrok
jimrok commented May 1, 2012

I think this still not fixed in rails 3.2.3

@jonduarte

I simulated your app with rails 3.2.12 and still have the same result.
I don't know if what you expect is the default behavior. For example: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing don't specify any place holder for scope params. But, if you use fixed value it work properly. You also could use:

resources :network do
  resources :groups
end
@saulius
Contributor
saulius commented Feb 17, 2013

I think by design you are supposed to do something like this in ApplicationController to make it work:

class ApplicationController < ActionController::Base
  def url_options
    return super unless params[:network]
    { network: params[:network] }.merge(super)
  end
end

Does that help?

@jaggederest
Contributor

Hi there, is this still an issue? In 3.2 or 4.0?

@steveklabnik
Member

Yup, we don't know. I'm going to close this as stale, but if someone can provide a reproduction, I'll re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment