:remote =>与form_for的真正混淆 [英] :remote => true confusion with form_for
问题描述
我正在学习Rails,将不胜感激.我看过大约10个:remote =>的教程.在
,它们似乎彼此复制(同样令人困惑). form_for
上为真
I'm just learning Rails and would appreciate any help. I've looked at probably 10 tutorials for :remote => true
on form_for
and they all seem to copy each other (equally confusing).
This tutorial is one of them: http://tech.thereq.com/post/18910961502/rails-3-using-form-remote-true-with-jquery-ujs
我的Web应用程序几乎全部都放在一个页面上,而我的提交表单"位于一个弹出的灯箱中.这意味着form_for代码实际上在我的index.html.erb中.这也意味着为 index
创建了一个新的@playlist实例变量(而不是将其放在 new
中)
My web app is pretty much all on one page, and my 'submit form' is inside a pop-up lightbox. This means that the form_for code is actually within my index.html.erb. It also means a new @playlist instance variable is created for index
(instead of having this be in new
)
我的最终目标是这个:
如果@ playlist.save
返回 true
,我想使用:notice将用户重定向到 root_path
.无需使用成功消息或类似内容更新表单.我想对保存成功进行硬重定向.
if @playlist.save
returns true
, I want to REDIRECT the user to the root_path
WITH a :notice. There is no need to update the form with a success message or anything like that. I want a hard redirect on save success.
如果@ playlist.save
返回 false
,我想在create.js.erb中呈现代码,该代码会将错误附加到灯箱表单中,而无需刷新页.注释:许多教程似乎都希望将整个表单重新呈现为html,然后将其内容替换为适当的位置.我宁愿(如果可以的话)将错误发送到表单并将其插入,我认为重新呈现和替换整个表单似乎比必须的复杂.
if @playlist.save
returns false
, I want to render the code inside create.js.erb, which will append the errors into the lightbox form without refreshing the page. note A lot of the tutorials seem to want to re-render the entire form as html, then replace the contents in place. I'd rather (if I can), just send the errors to the form and insert them, I think re-rendering and replacing the entire form seems more complicated than it has to be.
截至目前,我的代码有点混乱,尤其是:
As of right now, my code is kind of muddled, especially:
-
respond_to
块.这是关于实际操作的最大困惑,我尝试了很多不同的代码,但没有任何效果 - 我不知道create.js.erb是否正确
- 我没有create.html.erb-我认为这是正确的,因为我只想重定向
- the
respond_to
block. This is the largest point of confusion about what actually goes here, I have tried lots of different code and nothing works - I have no idea if create.js.erb is correct
- I have no create.html.erb - I assume this is right because I just want to redirect
我的表格的第一行:
<%= form_for @playlist, :remote => true do |f| %>
我的整个PlaylistsController
class PlaylistsController < ApplicationController
before_filter :load_genres, :only =>[:index, :user]
before_filter :new_playlist, :only => [:index, :new, :create]
def index
@playlists = Playlist.order('created_at DESC').page(params[:page]).per(30)
end
def new
end
def create
respond_to do |format|
if @playlist.save
# ???
format.html { redirect_to root_path, :notice => "Playlist submitted" }
else
# ???
format.js { render :js => @playlist.errors, :status => :unprocessable_entity }
end
end
end
def user
@playlists = Playlist.order('created_at DESC').where(:username => params[:username]).page(params[:page]).per(30)
end
def listen
playlist = Playlist.find(params[:playlist_id])
playlist.update_attribute(:listens, playlist.listens + 1)
render :nothing => true
end
private
def load_genres
@genres = Genre.order(:name)
end
def new_playlist
@playlist = Playlist.new(:title => params[:title], :url => params[:url], :description => params[:description])
end
end
create.js.erb
jQuery(function($) {
alert('createjs called');
$('#submit-form form').on('ajax:error', function(event, xhr, status, error){
var responseObject = $.parseJSON(xhr.responseText),
errors = $('<ul />');
$.each(responseObject, function(index, value){
errors.append('<li>' + value + '</li>');
})
$(this).find('.submit-playlist-errors').html(errors);
});
});
推荐答案
如果我对您的理解正确,则您有一个表单,并且想要通过 flash
进行重定向,请注意是否已保存该表单.如果表单无效,则要显示错误.
If I understand you correctly, you have a form and you want to redirect with flash
notice if the form is saved. If the form is invalid you want do display errors.
您可以使用不同的格式(通常是html,json和js)将表单(或任何请求)发送到Rails应用程序.这是在您提供的网址中指定的.
You can send the form (or any request) to rails app in different formats (usually html, json and js). This is specified in the url you provides.
playlist_path(@playlist)
# => /playlists/1
playlist_path(@playlist, :format => "json")
# => /playlists/1.json
playlist_path(@playlist, :format => "html")
# => /playlists/1.html
# ... etc
格式告诉Rails要使用在 respond_to
块中指定的哪种响应.
The format tells rails which kind of response specified in your respond_to
block to use.
现在,如果这很清楚,您就必须决定要使用哪种格式.
Now if this is clear you have to decide which format to use.
如果您指定 js
格式,则响应将被视为 script
已执行.如果该表单有效,则可以发送用户javascript,它将用户重定向到所需的页面.如果表单无效,则可以用这种方式修改HTML,以便显示错误消息.不需要客户端代码.
If you specify js
format, the response will be treated as script
an executed. If the form is valid, you can send user javascript that will redirect them to the page you desire. If the form is invalid, you can modify HTML in the way so you can display error messages. No client-side code needed.
respond_to do |format|
if valid
format.js { render "valid" }
else
format.js { render "invalid", :status => :unprocessable_entity }
end
end
# valid.js.erb
window.location = <%= your_path %>
# invalid.js.erb
// whatever to display errors
当您使用 json
时,您可以返回重定向URL来重定向用户,或者如果表单无效则返回错误.请注意,此解决方案需要客户端使用Javascript来处理它.
When you use json
you can return redirect url to redirect user or errors if the form is invalid. Note that this solution requires Javascript on the client side to handle it.
# controller
respond_to do |format|
if valid
format.json { render :json => your_path }
else
format.json { render :json => @object.errors, :status => :unprocessable_entity }
end
end
# client side javascript
$("form")
.on("ajax:success", function(event, data, status, response) {
window.location = data
})
.on("ajax:error", function(event, response, error) {
// logic to handle errors
})
您还可以使用 html
格式(通过将format设置为 html
或完全不指定格式).但是,如果您使用:remote =>true
,您将不得不将其与 json
(具有客户端javascript)一样对待.如果您使用 redirect_to
作为响应,它将重定向请求,而不是您的用户.
You can also use html
format (by setting format to html
or not specifying format at all). But if you use :remote => true
you will have to treat it the same way as json
(have client side javascript). If you use redirect_to
as a response, it will redirect the request, not your user.
我不确定在重定向用户后是否显示即显消息,但是您可以通过以下方式尝试:
I am not sure about displaying the flash message after the user is redirected but you can try it this way:
respond_to do |format|
if valid
format.html {
flash[:notice] = "Playlist submitted"
render :json => your_path }
else
format.html { render :json => @object.errors, :status => :unprocessable_entity }
end
end
# client side javascript
//...
希望这会干燥您的 respond_to
块.
这篇关于:remote =>与form_for的真正混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!