Ответ 1
respond_to do |format|
format.js # actually means: if the client ask for js -> return file.js
end
js
здесь задает тип mime, который метод контроллера отправит обратно как ответ;
Default mails-типы Rails.
Если вы также попробуете format.yaml
:
respond_to do |format|
format.js
format.yaml
end
это означает, что ваш контроллер вернет yml
или js
в зависимости от того, что запрашивает клиентская сторона;
{}
в терминах ruby - block;
Если вы не укажете, какие рельсы будут пытаться отобразить файл по умолчанию из app/views/[имя_протоллера]/[имя метода контроллера]. [Html/js/...]
# app/controllers/some_controller.rb
def hello
respond_to do |format|
format.js
end
end
будет искать /app/views/some/hello.js.erb
;//по крайней мере в Rails v. 2.3.
Если вы укажете блок:
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