Form Ajax in Rails?

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:

And the form:

<%= form_for :ex,url:ex_act_path,remote:true do |f|%>
<%= f.text_field :text%>
abc
<%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

It doesn’t show anything when I submit.

function loadDoc(url, cfunc) {
function myFunction(xhttp) {

In the Console of browser:

POST http://localhost:3000/ex/act 422 Unprocessable Entity
GET http://localhost:3000/ex/act 200 OK

Rails has full support for Ajax form submission baked in. Have you tried
just adding remote: true to your form_for declaration, and see what
happens? Watch in the console as you submit the form, see how the
correct controller handles the request. If you write a JS view (name it
the same as the controller method that is invoked by your form, so
create.js.erb or update.js.erb, whichever is appropriate) then you will
have access to your updated model object, and you can use it to re-paint
the page with updated data. Here’s a very nice write-up on this
approach: http://www.korenlc.com/remote-true-in-rails-forms/

Walter

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs