我有一个div标签,我只想在函数返回true时更改,否则我希望它保持相同的内容。我使用render: nothing => true
但删除了所有内容。
<%= javascript_include_tag :defaults %>
<div id="x">Hi</div>
<% form_remote_tag :url => {:action => 'get'},
:update => 'x' do %>
<%= text_field_tag 'url' %>
<%= submit_tag "Submit" %>
<% end %>
def getImage
@img = params[:url]
if !verify @img
render :nothing => true
end
end
private
def verify url
return true if url =~ /https?:\/\/.*\.(jpg|png|gif|bmp)$/
return false
end
我想让我的rails代码执行此操作
var lastrequest = "";
function ajaxObj() { //Returns the ajax object based on browser
if ( window.XMLHttpRequest ) {
return new XMLHttpRequest(); //Firefox
} else if ( window.ActiveXObject ) {
return new ActiveXObject("Microsoft.XMLHTTP"); //IE
}
}
window.onload = function() {
document.getElementById("Submit").onclick = function() {
var request = ajaxObj();
request.onreadystatechange = function() {
if ( request.readyState == 4 ) {
document.getElementById("imgdisplay").innerHTML = request.responseText;
document.getElementById("imgurl").value = "";
}
}
if ( lastrequest != document.getElementById("imgurl").value ) { // Makes sure it doesn't request the same image twice
var parameters = "url=" + document.getElementById("imgurl").value;
request.open("POST", "imageget.php", true); // Sends the ajax request
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", parameters.length);
request.setRequestHeader("Connection", "close");
request.send(parameters);
lastrequest = document.getElementById("imgurl").value;
}
}
}
答案 0 :(得分:0)
使用状态代码或无法控制发生的情况。 http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645
答案 1 :(得分:0)
您还可以使用控制器中的代码删除:update => 'x'
部分:
def getImage
@img = params[:url]
if !verify @img
render :nothing => true
else
render :update do |page|
page['x'].replace_html :partial => <some partial here>
end
end
end
或者,有点简短:
def getImage
@img = params[:url]
render :update do |page|
page['x'].replace_html(:partial => <some partial here>) if verify(@img)
end
end
迎接