Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: what is wrong with this method?

I'm upgrading an application to Rails 4 and I cannot for the life of me figure out what is wrong with this method. The offender's the update method:

def update
  respond_to do |format|
    if @doc.articles.find_index { |a| a.changed? }
      @doc.publications.destroy_all
    end
    if @doc.update_attributes(params[:doc])
      @doc.create_activity :update, owner: current_user
      if current_user.brand.editable? && params[:editing]
        format.html { redirect_to editing_url(@doc) }
      else 
        format.html { redirect_to share_url(@doc.user.ftp, @doc) }
      end
    end
  end
end

Clicking submit generates this error:

ActionController::UnknownFormat in DocsController#update

and highlights this line:

respond_to do |format|

The create method works fine, it looks like this:

def create
  @doc = Doc.new(params[:doc])
  respond_to do |format|
    if @doc.save
      @doc.create_activity :create, owner: current_user
      if current_user.brand.editable? && params[:editing]
        format.html { redirect_to doc_editing_url(@doc) }
      else 
        format.html { redirect_to share_url(@doc.user.ftp, @doc) }
      end
    else
      format.html { render action: "new" }
    end
  end
end

Any thoughts at all? I'm totally stuck.

Oh, I've got this private method as a before_action too, so it's not that:

private

def set_document
  @doc = Doc.find(params[:id])
end

EDIT

I found this quasi-explanation:

In Rails 4.0, ActionController::UnknownFormat is raised when the action doesn't handle the request format. By default, the exception is handled by responding with 406 Not Acceptable, but you can override that now. In Rails 3, 406 Not Acceptable was always returned. No overrides.

which makes me think it's something to do with routes, but my routes should be default if I've declared them like so, yes?

resources :docs, :except => [:new, :show] do
  get "adjust/:state" => "docs#adjust", :as => :adjust
  patch "editing" => "docs#editing", :as => :editing
  patch "reupdate/" => "docs#reupdate", :as => :reupdate
  get "pdf" => "docs#pdf", :as => :pdf
  collection { post :sort }
end

EDIT 2

Adding the JSON to the controller, i.e.:

format.html { redirect_to share_url(@doc.user.ftp, @doc) }
format.json { render action: 'share', status: :created, location: @doc }

gives me a no method error and seems to redirect me back to the edit page:

Showing .../fin/app/views/docs/_form.html.erb where line #19 raised:
undefined method `covers?' for nil:NilClass

Really no idea what's going on here.

like image 437
t56k Avatar asked Jun 14 '13 00:06

t56k


1 Answers

One possible reason can be that if @doc.update_attributes(params[:doc]) returns false there is no format block being executed in the update method.

Usually you are rendering the edit action in that case.

like image 104
iltempo Avatar answered Dec 06 '22 12:12

iltempo