Ruby-on-rails rails response_to format.js API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13545148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails respond_to format.js API
提问by thiagoh
I am an experienced JAVA and C++ developer and I am trying to understand how rails works.
我是一位经验丰富的 JAVA 和 C++ 开发人员,我正在尝试了解 Rails 的工作原理。
I got this code below:
我在下面得到了这个代码:
respond_to do |format|
if @line_item.save
format.html { redirect_to store_url }
format.js { render :json => @line_item, :mime_type => Mime::Type.lookup('application/json'),
:callback => 'javascriptFunction' }
and I've been searching the api that defines what I can pass inside the format.js {}but I could not find..
我一直在搜索定义我可以在里面传递的内容的api,format.js {}但我找不到..
first of all: what kind of statement is format.js, is that a variable?
首先:什么样的语句是format.js,这是一个变量?
and most important: what attributes can I pass into format.js {} ? can you pass the direct link? I've searched over the http://api.rubyonrails.org/
最重要的是:我可以将哪些属性传递给 format.js {} ?你能通过直接链接吗?我搜索了http://api.rubyonrails.org/
回答by ted
respond_to do |format|
format.js # actually means: if the client ask for js -> return file.js
end
jshere specifies a mime-type that the controller method would send back as a response;
Default Rails mime-types.
If you try also with format.yaml:
js此处指定控制器方法将作为响应发回的 MIME 类型;
默认 Rails mime-types。
如果您也尝试使用format.yaml:
respond_to do |format|
format.js
format.yaml
end
that will mean that your controller will return ymlor jsdepending on what the client-side is asking;
这将意味着您的控制器将返回yml或js取决于客户端的要求;
{}in terms of ruby is a block;
If you don't specify any rails will try to render a default file from app/views/[contoller name]/[controller method name].[html/js/...]
{}就 ruby 而言,是一个块;如果你没有指定任何 rails 将尝试从 app/views/[contoller name]/[controller method name].[html/js/...] 渲染一个默认文件
# app/controllers/some_controller.rb
def hello
respond_to do |format|
format.js
end
end
will look for /app/views/some/hello.js.erb; // at least in Rails v. 2.3.
会寻找/app/views/some/hello.js.erb;// 至少在 Rails v. 2.3 中。
If you do specify block:
如果您指定块:
respond_to do |format|
# that will mean to send a javascript code to client-side;
format.js { render
# raw javascript to be executed on client-side
"alert('Hello Rails');",
# send HTTP response code on header
:status => 404, # page not found
# load /app/views/your-controller/different_action.js.erb
:action => "different_action",
# send json file with @line_item variable as json
:json => @line_item,
:file => filename,
:text => "OK",
# the :location option to set the HTTP Location header
:location => path_to_controller_method_url(argument)
}
end
回答by Magne
I believe this was the url you were looking for:
我相信这是您要查找的网址:
https://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to
https://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to
This might also be helpful to some, to see that you can actually render js directly within the format.js method, if you for example only have a small one line js statement you want to return, and you don'twant to defer to a RJS file like controller_action_name.js.erb:
这也可能是有帮助的一些,一看就知道你其实可以直接在format.js方法渲染的js,比如你只需要你想返回一个小行的js语句,你不希望推迟到一个 RJS 文件,如controller_action_name.js.erb:
respond_to do |format|
format.html { redirect_to new_admin_session_path }
format.js { render :js => "window.location='#{ new_admin_session_path }'" }
end

