gpt4 book ai didi

ruby-on-rails - rails 中的 link_to_remote 问题

转载 作者:数据小太阳 更新时间:2023-10-29 07:31:22 24 4
gpt4 key购买 nike

我在 Rails 中使用 link_to_remote 时遇到了一致性问题。

我有 2 个 link_to_remote 用例,它们生成不同的 ajax。我不明白为什么,这让我发疯。

这是用例一...

<%= link_to_remote "section-", :update => "sections", :url => {:action => :destroy, :controller => "sections", :id => @section.id } %>

这会生成适当的 ajax(如下所示)并按我预期的方式工作。请注意,它从调用中获取 :action 参数并将其正确插入到 ajax 中。

<a href="#" onclick="if { new Ajax.Updater('sections', '/sections/destroy/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('f5e50e62fafd118e4588b33c9571ea6eef864176')}); }; return false;">section-</a>

我还有另一个使用 link_to_remote 的实例,但它生成了不正确的 ajax。用例几乎相同,只是 Controller 不同。无论哪种方式,我都不希望这会导致不同的 ajax。

电话...

<%= link_to_remote "question-", :update =>"questions-1", :url => {:action => :destroy, :controller => "questions", :id => @question.id} %>

生成的 ajax...

<a href="#" onclick="if { new Ajax.Updater('questions-1', '/questions/1', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('f5e50e62fafd118e4588b33c9571ea6eef864176')}); }; return false;">question-</a>

这里明显的区别在于 Ajax.Updater 的第二个参数。该路径中缺少 :action 参数。为什么?这导致我的代码损坏,但我不明白为什么会这样。 link_to_remote 调用几乎相同。

请指出正确的方向。谢谢。

下面是我的routes.rb文件...


ActionController::Routing::Routes.draw do |map|
map.resources :questions, :has_one => :section, :collection => { :sort => :post }
map.resources :sections, :has_many => :questions, :has_one => :form, :collection => { :sort => :post }
map.resources :forms, :has_many => :sections

# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
map.root :controller => "forms"

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

最佳答案

如果将其复制到您的 View 中会得到什么?

<%= url_for :action => :destroy, :controller => :questions, :id => @question.id %>

我怀疑问题不在于 link_to_remote,而在于 routing .一旦 url_for 返回您期望的 URL,然后再次尝试 link_to_remote。

关于ruby-on-rails - rails 中的 link_to_remote 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/694593/

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