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/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/befb8748915a530c9f2e920b494f844c%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Walter Lee Davis at Feb 29, 2016 at 2:45 pm

    On Feb 28, 2016, at 10:18 PM, Le Hung wrote:

    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
    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
    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/befb8748915a530c9f2e920b494f844c%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3B618ED8-0DB9-4461-B03E-F3EF70AED383%40wdstudio.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 29, '16 at 3:18a
activeFeb 29, '16 at 2:45p
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Le Hung: 1 post Walter Lee Davis: 1 post

People

Translate

site design / logo © 2021 Grokbase