remote:true with redirect. Does it make sence?

34 views
Skip to first unread message

Taras Matsyk

unread,
Mar 13, 2016, 4:26:51 PM3/13/16
to rubyonra...@googlegroups.com
Dear community,

I have a small concern about remote true.
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 %>
<div class="tweets">
<% if @tweets %>
<% @tweets.each do |tweet| %>
<div class="tweet"><%= tweet.text %></div>
<% end %>
<% end %>
</div>

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?

--
Posted via http://www.ruby-forum.com/.

j...@room118solutions.com

unread,
Mar 15, 2016, 12:16:00 PM3/15/16
to Ruby on Rails: Talk
The Rails guide has a great write-up on this: http://guides.rubyonrails.org/working_with_javascript_in_rails.html#form-for

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.   

Taras Matsyk

unread,
Mar 15, 2016, 2:44:10 PM3/15/16
to rubyonra...@googlegroups.com
Got it, so client side is on my shoulders :)

Thank you, dear stranger!

j...@room118solutions.com

unread,
Mar 16, 2016, 1:39:23 PM3/16/16
to Ruby on Rails: Talk
You got it!

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.

Jim

Taras Matsyk

unread,
Mar 16, 2016, 2:57:09 PM3/16/16
to rubyonra...@googlegroups.com
Nice to meet you Jim.

Thank you for time.

I do realise how Ajax works and probably did not think long enough to
realise how it is implemented :)
Reply all
Reply to author
Forward
0 new messages