I'm new in Rails.
I'm trying to add a form with ajax.It's just typing a text and submit to
file 'ex/act' and show the param in form.
My code:
<script>
function loadDoc(url, cfunc) {
var xhttp;
xhttp=new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
cfunc(xhttp);
}
};
xhttp.open("POST", url, true);
xhttp.send();
}
function myFunction(xhttp) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
</script>
And the form:
<%= form_for :ex,url:ex_act_path,remote:true do |f|%>
<%= f.text_field :text%>
<button onclick="loadDoc('ex/act',myFunction)">abc</button>
<%end%>
In the 'ex/act' controller:
def act
@a = get_param
end
private
def get_param
params.require(:ex).permit(:text)
end
In the Console of browser:
POST
http://localhost:3000/ex/act 422 Unprocessable Entity
GET
http://localhost:3000/ex/act 200 OK
--
Posted via
http://www.ruby-forum.com/.