mercredi 8 mars 2017

Upload file to custom route rails carrier wave using form_tag

I have a post method that I wish to use to change the profile picture of the user.

routes.rb

  post "users/change_profile_picture", to: 'users#change_profile_picture'

form part of the show.html.erb

<%=form_tag(users_change_profile_picture_path, :method=>'post',remote: true,authenticity_token: true, :multipart => true) do%>
        <%= file_field_tag "profile" %>
        <%= text_field_tag "display_name" %>
         <%= submit_tag 'Submit' %>
<% end %>

If I submit a text with that form everything is working fine(show.js.erb gets executed and updates the page), but when I click submit with a file it crashes. The show.js.erb doesn't get executed anymore. All I get is a no content header in rails console.

I can upload a file using form for @user, but how do I upload one using form_tag and my method?

change_profile_picture method

def change_profile_picture
    @user = cp_params[:display_name]
      respond_to do |format|
        format.html
        format.js { render 'users/show.js'}
      end
  end

params

private
  def cp_params
      params.permit(:profile,:authenticity_token,:utf8, :display_name, :commit)
  end

Aucun commentaire:

Enregistrer un commentaire