I have a small concern about using “remote: true” in erb form.
I am building simple twitter site. Let’s say user has “tweet” from on
the main page and a list of tweets below.
here is what I do:
<%= form_tag("/tweets", method: “post”, class: “new-tweet”) do %>
<%= label_tag(nil, “New tweet”) %>
<%= text_field_tag(:text,nil,remote:true, placeholder: “Enter your
tweet message here. Limit 140 symbols”, maxlength: 140) %>
<%= submit_tag(“Tweet”) %>
<% end %>
<% if @tweets %>
<% @tweets.each do |tweet| %>
<%= tweet.text %>
<% end %>
<% end %>
route /tweets leads to another controller which actually adds a tweet to
the users collection which is displayed on the main page. My question
is: Does it make sense to use “remote: true” in the form here in order
to not reload all page.
If my question does not make sense, here is a deal. How does
“remote:true” actually works and what benefits does it give?
Essentially, remote: true will handle submitting your form via ajax for
you, but it’s on you to handle the response (since that’s going to be
very
application-specific). In your case, if the tweets are displayed in a
list, maybe the action responds to ajax requests by rendering just a
tweet
template (versus the entire tweets collection), and your JS takes that
HTML
snippet and inserts it into the list.
Yup, it’s on you, but it’s surprisingly simple since you’re only working
on
the response side. In just a few lines of code, I’ve done some cool
stuff
without a page refresh or client side framework.