0

i need to make "put" request, to "/users/upload_image" action.

Following code:

<% form_remote_for @user, :url => "/users/upload_image?id=#{@user.id}", :method => :put, :html => { :multipart => true } do |upload_form| %>
    <%= upload_form.file_field :avatar %>
<% end %>

Produces following HTML:

<form action="/users/1234567" method="post" onsubmit="$.ajax({data:$.param($(this).serializeArray()) + '&amp;authenticity_token=' + encodeURIComponent('HMkaYvfHyyYR1jGpVPkyLPfkPacqvTvtHjgDowzwzuY='), dataType:'script', type:'post', url:'/users/1234567'}); return false;">

When i set :method => :put in :html param, it will be set only in HTML, instead of JavaScript code.

  1. I need to have "put" inside the form tag and JavaScript
  2. I need to have action, like "/users/upload_image"

How to do it ?

Thanks

AntonAL
  • 16,692
  • 21
  • 80
  • 114

1 Answers1

1

If you'll look below <form> tag you'll see:

<input name="_method" type="hidden" value="put" />

That because HTTP doesn't support PUT and DELETE methods. It supports only GET and POST so Rails immitates PUT method this way

UPD

About ajax file uppload: Ruby on Rails AJAX file upload

to add /users/:id/upload_image path you should edit your routes and controller:

# routes to get /users/:id/upload_image
map.resources :user, :member => {:upload_image => :put}

# users_controller
def upload_image
  @user = User.find(params[:id])
  ...
end

Now you can use upload_image_user_path(@user) that will generate /users/@user.id/upload_image url for you

UPD 2

If you want to get /users/upload_image?user_id=XXX

# config/routes.rb
map.resources :user, :collection => {:upload_image => :put}

# app/controllers/users_controller.rb
def upload_image
  @user = User.find(params[:user_id])
  ...
end
Community
  • 1
  • 1
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • Yes. I see. Thanks. How can i customize :url ? – AntonAL Mar 16 '11 at 14:50
  • Thanks. I already have working uploading code. But this request was handled in /users/update method. Since, they are different scenarios, i need to have a separate action, called /users/upload_image". The only thing, i need now, is to change action in form_remote_for. Setting different :url does't gives any effect. – AntonAL Mar 16 '11 at 14:59
  • new update. But, actually, I didn't understand whole picture. – fl00r Mar 16 '11 at 15:04
  • I just need to implement avatar uploading in a separate controller's action. Thanks ! I works ! – AntonAL Mar 16 '11 at 15:08