gpt4 book ai didi

ruby-on-rails - rails 4 : what is wrong with this method?

转载 作者:行者123 更新时间:2023-12-04 02:25:17 25 4
gpt4 key购买 nike

我正在将应用程序升级到 Rails 4,但我一生都无法弄清楚这种方法有什么问题。罪犯的更新方法:

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

点击 submit生成此错误:
ActionController::UnknownFormat in DocsController#update

并突出显示这一行:
respond_to do |format|

create 方法工作正常,如下所示:
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

有什么想法吗?我完全被困住了。

哦,我有这个私有(private)方法作为 before_action也是,所以不是这样:
private

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

编辑

我发现了这个准解释:

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.



这让我觉得这与路线有关,但如果我这样声明它们,我的路线应该是默认的,是吗?
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

编辑 2

将 JSON 添加到 Controller ,即:
format.html { redirect_to share_url(@doc.user.ftp, @doc) }
format.json { render action: 'share', status: :created, location: @doc }

给我一个无方法错误,似乎将我重定向回编辑页面:
Showing .../fin/app/views/docs/_form.html.erb where line #19 raised:
undefined method `covers?' for nil:NilClass

真的不知道这里发生了什么。

最佳答案

一个可能的原因是,如果 @doc.update_attributes(params[:doc])返回 false update 方法中没有执行格式 block 。

通常你正在渲染 edit在这种情况下采取行动。

关于ruby-on-rails - rails 4 : what is wrong with this method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17099496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com