Routing error with sending / sending requests (passenger headers)

I ran into some strange problem even after a bunch of studies can't get closer. I have several forms that upload files through Carrierwave. When I load information, part of the route is disconnected (I think).

For example, I have a multi-part form representing:

https: / domain / programs / 223 / add_file as POST

but on presentation I get an error

No route matches [POST] "/ 223 / add_file"

although there’s a full route in my address bar. And if you send the full route as a GET request, it works fine. When I start rake routes, the route shows up very well.

Here is a subset of my route:

resources :programs do match "add_file" => "programs#add_file" 

If that matters, I am running Rails 3.2.2 using Passenger on Apache. The problem only occurs on this production server, never in development.

Any ideas? I am stuck with this as it affects multiple routes, and I tried to define a custom route only for this form without any luck.

Update: When I remove multi-part => true or file_field_tag ​​from the form, it fixes the problem. This is still a problem, but it seems to be less routing oriented than a file upload form.

+4
source share
3 answers

Create passenger_extension.rb in the lib folder using this code:

Passenger 3

 module PhusionPassenger module Utils protected NULL = "\0".freeze def split_by_null_into_hash(data) args = data.split(NULL, -1) args.pop headers_hash = Hash.new args.each_slice(2).to_a.each do |pair| headers_hash[pair.first] = pair.last unless headers_hash.keys.include? pair.first end return headers_hash end end end 

Passenger 5

 module PhusionPassenger module Utils # Utility functions that can potentially be accelerated by native_support functions. module NativeSupportUtils extend self NULL = "\0".freeze class ProcessTimes < Struct.new(:utime, :stime) end def split_by_null_into_hash(data) args = data.split(NULL, -1) args.pop headers_hash = Hash.new args.each_slice(2).to_a.each do |pair| headers_hash[pair.first] = pair.last unless headers_hash.keys.include? pair.first end return headers_hash end def process_times times = Process.times return ProcessTimes.new((times.utime * 1_000_000).to_i, (times.stime * 1_000_000).to_i) end end end # module Utils end # module PhusionPassenger 

And then in 'config / application.rb' do:

 class Application < Rails::Application ... config.autoload_paths += %W(#{config.root}/lib) require 'passenger_extension' end 

Then restart the web server.

NOTE: I am not sure that this does not violate any other functions, so use it at your own risk, and please let me know if you find any harm from this approach.

+5
source

One problem is that you do not indicate whether a route is defined in the collection or member. Which one is the right route?

 programs/:id/add_file programs/add_file 

You should build your routes as follows:

 resources :programs do post 'add_file', :on => :member end 

or

 resources :programs do member do post 'add_file' end end 

The above messages will be sent to programs/:id/add_file and sent to ProgramsController.add_file with params[:id] as the program identifier.

If you want this in a collection, you can do:

 resources :programs do post 'add_file', :on => :collection end 

or

 resources :programs do collection do post 'add_file' end end 

This will take the post requests to programs/add_file and send them to ProgramsController.add_file , but there will be no params[:id] .

In general, you should always indicate whether the routes are in the collection or member, and you must indicate which verb the route should take (i.e. use "get" or "post", etc. instead of "match").

Try the above and see if this solves your problem, if you do not tell me, and I will look again.

0
source

I think you may need to add

 :via => [:post] 

to your route specification. It seems strange that he will work on development, not production, but, as I understand it, routing routing, the connector you added will only respond to receive.

Try changing your match to

 match "add_file" => "programs#add_file", :via => [:post] 

In addition, based on the answer just presented to Andrew, you are probably better off using a member qualifier to be explicit that the operation is performed in a specific Program with a specific identifier, rather than a collection. It also needs to save some code in the add_file method, which probably does its best to get the parameter identifier from the URL.

0
source

All Articles