我想调用一个javascript函数,该函数将以用户对确认框的响应为条件.
例如,我有以下锚点:
<%= link_to 'Sign Out', destroy_session_path, confirm: 'Are you sure that you would like to sign out?', method: :delete %>
Run Code Online (Sandbox Code Playgroud)
我应该放弃不引人注目的javascript并将我自己的jQuery回调绑定到我的锚点吗?
或者是否有一些神奇的方法将回调函数传递给Rails url帮助器?我没有在文档中看到任何关于回调的讨论.
unobtrusive-javascript view-helpers link-to link-to-remote ruby-on-rails-3.2
我试图找出新的link_to,Rails 3但我仍然没有得到它在Rails 2我做:
<%= link_to_remote "My Link",:url=>{:action=>:myaction},:with=>"'data='+$('#someField').attr('value')" %>
Run Code Online (Sandbox Code Playgroud)
但是使用Rails 3中的新语法应该怎么做?
我正在尝试类似的东西
<%=link_to "My Link",{:action=>"myaction"},:with=>"'data='+$('#someField').attr('value');",:remote=>true%>
Run Code Online (Sandbox Code Playgroud)
但是我没有在控制器的动作中获得数量参数
所以我有一个包含多个消息的页面,每个消息都有一个链接,用于更改(精炼)该消息的RATING.当用户单击此链接时,我想要一个AJAX调用,它会更新该消息的数据库中的相应列值.单击此链接时,不会发生任何明显的情况.应该没有页面刷新或重新加载.
我一直在尝试使用link_to remote:true,但我似乎无法让它工作.在线文档在这个问题上相当不清楚,并且随着Rails 2和3之间的变化,一些事情如:不再支持.
到目前为止,我已经复制了我所拥有的内容,但我知道它甚至不会接近解决方案.在我需要传递到数据库的参数方面,我需要profile_id,message_id和new_rating.
提前致谢!
show.html.haml
.status-bar
= link_to "", { action: :refine_result }, remote: true
Run Code Online (Sandbox Code Playgroud)
profile_controller.rb
...
def refine_result
@refinement = ResultRefinement.new
@refinement.profile_id = params[:profile_id]
@refinement.message_id = params[:message_id]
@refinement.save
respond_to do |format|
format.html { render nothing: true }
format.js { render nothing: true }
end
end
Run Code Online (Sandbox Code Playgroud)
result_refinement.rb
class ResultRefinement < ActiveRecord::Base
attr_accessible :profile_id, :message_id, :new_rating, :deleted
belongs_to :profile
end
Run Code Online (Sandbox Code Playgroud) 如果我有以下 JavaScript 代码
var myVar = 0;
function setNewValforVar(newValue){
myVar = newValue;
}
Run Code Online (Sandbox Code Playgroud)
我正在调用setNewValforVar功能n时间
因此,当我单击链接时,它会将myVar值发送到远程链接中的控制器操作,例如
<%=link_to "My Link",{:action=>"myAction"},:data=>''sval='+myVar',:remote=>true%>
Run Code Online (Sandbox Code Playgroud)
我 Rails 2.3.x 我会这样做
<%=link_to_remote "My Link",:url=>{:action=>"myAction"},:with=>"'sval='+myVar"%>
Run Code Online (Sandbox Code Playgroud)
我在控制器的操作中得到了这个
params["myVar"]
Run Code Online (Sandbox Code Playgroud)
我如何使用link_to助手在 Rails 3 上执行此操作?
我正在使用以下代码在rails 3中设置AJAX操作.代码的AJAX部分似乎有效,但它没有请求正确的文件,我的respond_to为常规HTML提供服务.
路由信息:
resources :zones do
resources :records
end
Run Code Online (Sandbox Code Playgroud)
控制器:
def new
@zone = Zone.new
respond_to do |format|
format.html
format.js
end
end
Run Code Online (Sandbox Code Playgroud)
链接视图(haml):
= link_to 'Add a zone →', new_zone_path, :remote=>true
Run Code Online (Sandbox Code Playgroud)
从link_to生成的HTML(还注意到html实体的失败呈现......但这是另一个问题):
<a href="/zones/new" data-remote="true">Add a zone &#8594;</a>
Run Code Online (Sandbox Code Playgroud)
对于踢,视图/区域的目录列表.我不确定我这样做是否正确,所以我有new.js.rjs和new.rjs.它们都具有相同的内容,但从未被动作拾取.
| `~zones/
| |-_form.html.haml
| |-_record.html.haml
| |-edit.html.haml
| |-index.html.haml
| |-new.html.haml
| |-new.js.rjs
| |-new.rjs
| `-show.html.haml
Run Code Online (Sandbox Code Playgroud)
最后,从我点击链接时的服务器日志:
Started GET "/zones/new" for 127.0.0.1 at Wed Dec 29 00:04:03 -0700 2010
Processing by ZonesController#new as */*
User Load (0.4ms) SELECT "users".* …Run Code Online (Sandbox Code Playgroud) 是否有任何捷径:
link_to 'title', path, 'data-type' => :html
Run Code Online (Sandbox Code Playgroud)
我认为:type=>:html可以工作,就像:remote=>true转换data-remote='true',但不支持.