0
votes

I'm getting the following error when I call an Update method.

ActionController::UnknownFormat (ActionController::UnknownFormat):
  app/controllers/devise/custom/registrations_controller.rb:21:in `update_avatar'

Here's the offending controller:

class Devise::Custom::RegistrationsController < Devise::RegistrationsController

  respond_to :html, :js

  def update_avatar
    @user = current_user

    if @user.update(user_params)
      respond_to do |format|
        format.js { render 'update_avatar'}
        flash[:notice] = "Updated user"
      end
    else
      render 'edit'
    end
  end

  protected
    def user_params
     params.require(:user).permit(:avatar)
    end
end

What's odd is I use this approach on other controllers without a hitch and yet here it falls over on the respond_to line.

Can anyone shed any light? Thanks!

Log

Processing by Devise::Custom::RegistrationsController#update_avatar as Parameters: {"utf8"=>"✓", "remotipart_submitted"=>"true", "authenticity_token"=>"reTaOwTtvbI+IPYq1nvLWl0blVOmaSu/o5VpfGziguo=", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, /; q=0.01", "user"=>{"avatar"=>#, @original_filename="translate.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"translate.jpg\"\r\nContent-Type: image/jpeg\r\n">}}

4
Can you look in your log and add the first couple of lines of output from processing the action? It should look something like this example from my log Processing LessonsController#online_xml [GET]\n Parameters: {"id"=>"57978"}Max Williams
Thanks, I have this installed already. It's uploading the file fine and saving to the db, just not processing the format.js to call my ajax request.steve
Maybe it's being processed as an html request for some reason? Try adding format.html { raise 'We got an HTML request'}Max Williams
Hmm, no - not doing that either. I'm thinking maybe something to do with the fact that it's a custom devise controller? Though can't see what difference it'd make to this...steve

4 Answers

1
votes

I believe your error is due to this line render 'edit'.You should be specifying a format when using respond_to.

Update your update_avatar method to like this

def update_avatar
  @user = current_user

  if @user.update(user_params)
    respond_to do |format|
      format.js { render 'update_avatar'}
      flash[:notice] = "Updated user"
    end
  else
    format.html { render 'edit' } # here
  end
end
0
votes

The part { render 'update_avatar'} in format.js { render 'update_avatar'} is not needed, as it will try to load the file called update_avatar.js by default.

Do you actually have such js file?

You could also test with something like: format.js {render "alert('Hello World');"} just to see if it's working.

0
votes

I think you should try:

format.js { render 'update_avatar',
        flash[:notice] = "Updated user"}

and btw deyan is right! format.js renders renders update_avatar.js by default you don't need to specify it.

0
votes

For me the answer was to completely rewrite the custom controller. I followed this guide: http://natashatherobot.com/devise-rails-sign-in/ and re-factored my code accordingly. It also helps to understand more about Devise before tackling things like this!

Thanks though for the all the contributions. Best place on the web to get help, hands down!