开发者

form_remote_for using put to different action

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 fa开发者_StackOverflowlse;">

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜