Ruby on Rails Thursday, June 15, 2017

> On Jun 15, 2017, at 1:00 AM, fugee ohu <fugee279@gmail.com> wrote:
>
>
>
> On Wednesday, June 14, 2017 at 8:49:27 PM UTC-4, Walter Lee Davis wrote:
> There's nothing dated about it. Using RJS (Remote JavaScript) rendering gives you the ability to render just the parts of the page you want to replace. With jQuery, you would still have to render the custom HTML that you want to inject into the page, but then you would use load() or replaceWith() to substitute it into the DOM. Now you have two problems.
>
> Walter
>
> > On Jun 14, 2017, at 7:54 PM, fugee ohu <fuge...@gmail.com> wrote:
> >
> > I wanna render to an element instead of redirecting so went back to my book Agile Web Development with Rails adding ajax it says to make my link remote: true and put a respond_to js in my controller and then use a js.erb file Is that a little dated I've been doing some jquery by including *.js files in assets/javascripts Can I render my views to the element using jquery instead of js.erb I notice rails doesn't generate any js.erb files by default Should I do as the book says or find a way to do it with jquery? thanks ia
> >
> > --
> > 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-ta...@googlegroups.com.
> > To post to this group, send email to rubyonra...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
>
> As I iterate through posts I don't know which post someone's going to comment but my js.erb file can only render to a class or id name so when i click comment forms are gonna load for all posts on the page What's the solution please?

The way I usually do this is I have a partial for a list or table that renders just one of the items, which I gather up in the controller, then I use the content_tag_for() helper (removed from Rails 5, but available in a gem). So my list of items would look like this:

<ul id="items">
<%= render @items %>
</ul>

And to get that little bit to work, you just need to have a partial here: app/views/items/_item.html.erb with the following contents:

<%= content_tag_for item, :li do %>
<h1><%= item.title %></h1>
<%= simple_format item.description %>
<%- end -%>

In this technique, remember that the local variable (item) inside the partial is named for whatever the partial is named. If you decide to change the name of the partial to item_listing, then inside that partial, your local variable would be item_listing. When you follow the pattern of naming it for the singular of the collection of items you want to render, and you name your collection for the plural, then you can use that lovely terse render @[name of the collection]. But the long form is actually render(partial: 'item', collection: @items), so when you decide to rename the collection or the partial, that's the long-form you need to use in order to clarify what you mean to Rails.

Walter

>
> --
> 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/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.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/43619B9F-0CC2-4029-8C98-13E127652423%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment