*.js.erb files

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

*.js.erb files

fugee ohu
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

Walter Lee Davis
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 <[hidden email]> 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 [hidden email].
> To post to this group, send email to [hidden email].
> 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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/709D6D9F-6095-4BF7-BE79-E5591E42EF0F%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu


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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



I'm setting up the users page, the page that other users visit to follow or like them so the page has posts each with a comment link like this 
<%= link_to 'Comment', new_comment_path('comment[commentable_type]': 'Post', 'comment[commentable_id]': commentable.id), remote: true %> 
Now I just want the comment form to render so what should I name the <div> and what should I pass to render please Thanks

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bb04e9c7-3b73-4034-bbfb-cb9fe55af8b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu
In reply to this post by Walter Lee Davis


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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


How do i render the form into an element on the page when a user clicks the comment link?

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ca6bfd2d-5121-44a1-b550-bb66de66210b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu
In reply to this post by Walter Lee Davis


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 <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="2bUIvVnYDAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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?

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

Walter Lee Davis

> On Jun 15, 2017, at 1:00 AM, fugee ohu <[hidden email]> 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 <[hidden email]> 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 [hidden email].
> > To post to this group, send email to [hidden email].
> > 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 [hidden email].
> To post to this group, send email to [hidden email].
> 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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu


On Thursday, June 15, 2017 at 5:03:37 PM UTC-4, Walter Lee Davis wrote:

> On Jun 15, 2017, at 1:00 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="gOC3YZsaDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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 <[hidden email]> 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 [hidden email].
> > To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com.
> > For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="gOC3YZsaDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="gOC3YZsaDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


undefined method `model_name' for :li:Symbol Extracted source (around line #1): <%= content_tag_for post, :li do %> 

 

/posts/_post.html.erb
<%= content_tag_for post, :li do %> 
  <%= content_tag :hi, post.id %> 
   <%= simple_format post.content %> 
<%- end -%>


/posts/_index.html.erb
<ul id="posts">
<%= render @posts %> </ul>

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/deaa9d49-72a4-4818-bd18-6fd79cfc5648%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu
In reply to this post by Walter Lee Davis


On Thursday, June 15, 2017 at 5:03:37 PM UTC-4, Walter Lee Davis wrote:

> On Jun 15, 2017, at 1:00 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="gOC3YZsaDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> 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 <[hidden email]> 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 [hidden email].
> > To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com.
> > For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="gOC3YZsaDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="gOC3YZsaDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


Your example is with a js file in assets Can we do this with a js.erb in views instead? Same code? What about the file naming convention please Thanks

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/95c775c1-29fb-4ee3-94b0-131b6ab38456%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

Walter Lee Davis

> On Jun 16, 2017, at 11:56 PM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Thursday, June 15, 2017 at 5:03:37 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 15, 2017, at 1:00 AM, fugee ohu <[hidden email]> 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 <[hidden email]> 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 [hidden email].
> > > To post to this group, send email to [hidden email].
> > > 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 [hidden email].
> > To post to this group, send email to [hidden email].
> > 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.
>
>
> Your example is with a js file in assets Can we do this with a js.erb in views instead? Same code? What about the file naming convention please Thanks


Actually, it occurred to me after I wrote this that you could do it without any JavaScript at all. In fact, that's what I usually tell people to do first, so I'm surprised I led with the JS example. The thing is, you build the JS version second, as a layer over the HTML version. That way you have a fallback.

In the _item.html.erb partial, you would change the template to include the link helper directly:

<%= content_tag_for item, :li do %>
  <h1><%= item.title %></h1>
  <%= simple_format item.description %>
  <%= link_to 'Comment', new_comment_path(item_id: item.id) %>
<%- end -%>

Now that link doesn't have to be "magic'd" into the page with JS at all. It is there and it works. Try it out, see how it works. Then layer JS on top of it (if you want to put the comment form into a modal dialog, for example).

Also, rather than continuing with your current project, I suggest you build a "stunt app" just to work out this linking issue. Just cd up a few levels in your drive, and `rails new stunt-app` and `rails scaffold Item name description:text` and `rails scaffold Comment item:references comment:text` That's enough to work with here, and by using scaffold, you will start with a known-working framework without any assumptions. If you're using Rails 5, don't forget to add the optional: true bit to your belongs_to declaration on Comment. Otherwise you will find that you cannot save an Item without comments.

Now to the naming conventions.

If you use the <%= render @items %> trick, then you have to have a file named views/items/_item.html.erb. Inside that partial, the local variable item will be loaded by the Rails framework if you have passed a collection named @items in your render call. All of that is the optimized "magic" way of doing this. It is a shortcut, and it can be expanded to:

<%= render partial: 'items/item', collection: @items %>

If you name the partial something other than the singular of the collection, then you have to use the long form. If your partial is in a different object's view hierarchy than the one that is rendering it, then you also need to put that part in the partial name (how I used items/item above).

So the same thing could be rendered as:

<%= render partial: 'foos/foo', collection: @items %>

and the partial would be changed to name the instance variable inside it 'foo'.

If you really get into the weeds, then you need to have an iterator in the parent page, and really spell it out for Rails:

<%- @items.each do |item| -%>
  <%= render partial: 'items/item', locals: { item: item } %>
<%- end -%>

The shortcuts are there to make your production work faster, and if you follow the conventions, then they just work in a way that may confuse you at first. I certainly was confused the first time I did this, particularly when I tried to use a non-conventional setup. It took me a long time for the penny to drop that the name of the instance variable inside the partial was NOT based on the name of the object being rendered, but rather the partial's filename.

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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/89D7EFEB-6EAF-4319-AF5D-66BFE5BEA6D2%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu


On Saturday, June 17, 2017 at 10:12:46 AM UTC-4, Walter Lee Davis wrote:

> On Jun 16, 2017, at 11:56 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="3CdxBFmhDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Thursday, June 15, 2017 at 5:03:37 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 15, 2017, at 1:00 AM, fugee ohu <[hidden email]> 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 <[hidden email]> 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 [hidden email].
> > > To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com.
> > > For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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-ta...@googlegroups.com.
> > To post to this group, send email to [hidden email].
> > To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com.
> > For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.
>
>
> Your example is with a js file in assets Can we do this with a js.erb in views instead? Same code? What about the file naming convention please Thanks


Actually, it occurred to me after I wrote this that you could do it without any JavaScript at all. In fact, that's what I usually tell people to do first, so I'm surprised I led with the JS example. The thing is, you build the JS version second, as a layer over the HTML version. That way you have a fallback.

In the _item.html.erb partial, you would change the template to include the link helper directly:

<%= content_tag_for item, :li do %>
  <h1><%= item.title %></h1>
  <%= simple_format item.description %>
  <%= link_to 'Comment', new_comment_path(item_id: <a href="http://item.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fitem.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGW6q1APsIqjQ4qpLugXxTUbCuyKA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fitem.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGW6q1APsIqjQ4qpLugXxTUbCuyKA&#39;;return true;">item.id) %>
<%- end -%>

Now that link doesn't have to be "magic'd" into the page with JS at all. It is there and it works. Try it out, see how it works. Then layer JS on top of it (if you want to put the comment form into a modal dialog, for example).

Also, rather than continuing with your current project, I suggest you build a "stunt app" just to work out this linking issue. Just cd up a few levels in your drive, and `rails new stunt-app` and `rails scaffold Item name description:text` and `rails scaffold Comment item:references comment:text` That's enough to work with here, and by using scaffold, you will start with a known-working framework without any assumptions. If you're using Rails 5, don't forget to add the optional: true bit to your belongs_to declaration on Comment. Otherwise you will find that you cannot save an Item without comments.

Now to the naming conventions.

If you use the <%= render @items %> trick, then you have to have a file named views/items/_item.html.erb. Inside that partial, the local variable item will be loaded by the Rails framework if you have passed a collection named @items in your render call. All of that is the optimized "magic" way of doing this. It is a shortcut, and it can be expanded to:

<%= render partial: 'items/item', collection: @items %>

If you name the partial something other than the singular of the collection, then you have to use the long form. If your partial is in a different object's view hierarchy than the one that is rendering it, then you also need to put that part in the partial name (how I used items/item above).

So the same thing could be rendered as:

<%= render partial: 'foos/foo', collection: @items %>

and the partial would be changed to name the instance variable inside it 'foo'.

If you really get into the weeds, then you need to have an iterator in the parent page, and really spell it out for Rails:

<%- @items.each do |item| -%>
  <%= render partial: 'items/item', locals: { item: item } %>
<%- end -%>

The shortcuts are there to make your production work faster, and if you follow the conventions, then they just work in a way that may confuse you at first. I certainly was confused the first time I did this, particularly when I tried to use a non-conventional setup. It took me a long time for the penny to drop that the name of the instance variable inside the partial was NOT based on the name of the object being rendered, but rather the partial's filename.

Walter


What about the js to render the comment form into the div of the right post id? 

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/21f1179b-0caa-40f3-971c-f66d119c9e82%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

Walter Lee Davis

> On Jun 17, 2017, at 11:20 AM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Saturday, June 17, 2017 at 10:12:46 AM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 16, 2017, at 11:56 PM, fugee ohu <[hidden email]> wrote:
> >
> >
> >
> > On Thursday, June 15, 2017 at 5:03:37 PM UTC-4, Walter Lee Davis wrote:
> >
> > > On Jun 15, 2017, at 1:00 AM, fugee ohu <[hidden email]> 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 <[hidden email]> 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 [hidden email].
> > > > To post to this group, send email to [hidden email].
> > > > 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 [hidden email].
> > > To post to this group, send email to [hidden email].
> > > 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.
> >
> >
> > Your example is with a js file in assets Can we do this with a js.erb in views instead? Same code? What about the file naming convention please Thanks
>
>
> Actually, it occurred to me after I wrote this that you could do it without any JavaScript at all. In fact, that's what I usually tell people to do first, so I'm surprised I led with the JS example. The thing is, you build the JS version second, as a layer over the HTML version. That way you have a fallback.
>
> In the _item.html.erb partial, you would change the template to include the link helper directly:
>
> <%= content_tag_for item, :li do %>
>   <h1><%= item.title %></h1>
>   <%= simple_format item.description %>
>   <%= link_to 'Comment', new_comment_path(item_id: item.id) %>
> <%- end -%>
>
> Now that link doesn't have to be "magic'd" into the page with JS at all. It is there and it works. Try it out, see how it works. Then layer JS on top of it (if you want to put the comment form into a modal dialog, for example).
>
> Also, rather than continuing with your current project, I suggest you build a "stunt app" just to work out this linking issue. Just cd up a few levels in your drive, and `rails new stunt-app` and `rails scaffold Item name description:text` and `rails scaffold Comment item:references comment:text` That's enough to work with here, and by using scaffold, you will start with a known-working framework without any assumptions. If you're using Rails 5, don't forget to add the optional: true bit to your belongs_to declaration on Comment. Otherwise you will find that you cannot save an Item without comments.
>
> Now to the naming conventions.
>
> If you use the <%= render @items %> trick, then you have to have a file named views/items/_item.html.erb. Inside that partial, the local variable item will be loaded by the Rails framework if you have passed a collection named @items in your render call. All of that is the optimized "magic" way of doing this. It is a shortcut, and it can be expanded to:
>
> <%= render partial: 'items/item', collection: @items %>
>
> If you name the partial something other than the singular of the collection, then you have to use the long form. If your partial is in a different object's view hierarchy than the one that is rendering it, then you also need to put that part in the partial name (how I used items/item above).
>
> So the same thing could be rendered as:
>
> <%= render partial: 'foos/foo', collection: @items %>
>
> and the partial would be changed to name the instance variable inside it 'foo'.
>
> If you really get into the weeds, then you need to have an iterator in the parent page, and really spell it out for Rails:
>
> <%- @items.each do |item| -%>
>   <%= render partial: 'items/item', locals: { item: item } %>
> <%- end -%>
>
> The shortcuts are there to make your production work faster, and if you follow the conventions, then they just work in a way that may confuse you at first. I certainly was confused the first time I did this, particularly when I tried to use a non-conventional setup. It took me a long time for the penny to drop that the name of the instance variable inside the partial was NOT based on the name of the object being rendered, but rather the partial's filename.
>
> Walter
>
>
> What about the js to render the comment form into the div of the right post id?

Walk first, then run. This code will link to a form, which will load in another page view. Once you have that working, and it can submit, and it can save a comment, then optimize the flow by injecting the form into the page. You will modify the working code, by adding remote: true to the link to the new comment form, and you will add a new.js.erb file to the comments directory, which will inject the form into the parent page. But you cannot do that step before you get the normal new page view form of this working entirely.

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 [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/21f1179b-0caa-40f3-971c-f66d119c9e82%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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/24FA0DD9-C604-4C88-93D1-3385AAEA0924%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu


On Saturday, June 17, 2017 at 11:25:24 AM UTC-4, Walter Lee Davis wrote:

> On Jun 17, 2017, at 11:20 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="iYyvxE-lDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Saturday, June 17, 2017 at 10:12:46 AM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 16, 2017, at 11:56 PM, fugee ohu <[hidden email]> wrote:
> >
> >
> >
> > On Thursday, June 15, 2017 at 5:03:37 PM UTC-4, Walter Lee Davis wrote:
> >
> > > On Jun 15, 2017, at 1:00 AM, fugee ohu <[hidden email]> 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 <[hidden email]> 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 [hidden email].
> > > > To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/2a6923a4-e128-4b1f-be14-6eed119e66df%40googlegroups.com.
> > > > For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">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-ta...@googlegroups.com.
> > > To post to this group, send email to [hidden email].
> > > To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/84d6c62f-aa8f-429b-b774-29ccacdd3581%40googlegroups.com.
> > > For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.
> >
> >
> > Your example is with a js file in assets Can we do this with a js.erb in views instead? Same code? What about the file naming convention please Thanks
>
>
> Actually, it occurred to me after I wrote this that you could do it without any JavaScript at all. In fact, that's what I usually tell people to do first, so I'm surprised I led with the JS example. The thing is, you build the JS version second, as a layer over the HTML version. That way you have a fallback.
>
> In the _item.html.erb partial, you would change the template to include the link helper directly:
>
> <%= content_tag_for item, :li do %>
>   <h1><%= item.title %></h1>
>   <%= simple_format item.description %>
>   <%= link_to 'Comment', new_comment_path(item_id: <a href="http://item.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fitem.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGW6q1APsIqjQ4qpLugXxTUbCuyKA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fitem.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGW6q1APsIqjQ4qpLugXxTUbCuyKA&#39;;return true;">item.id) %>
> <%- end -%>
>
> Now that link doesn't have to be "magic'd" into the page with JS at all. It is there and it works. Try it out, see how it works. Then layer JS on top of it (if you want to put the comment form into a modal dialog, for example).
>
> Also, rather than continuing with your current project, I suggest you build a "stunt app" just to work out this linking issue. Just cd up a few levels in your drive, and `rails new stunt-app` and `rails scaffold Item name description:text` and `rails scaffold Comment item:references comment:text` That's enough to work with here, and by using scaffold, you will start with a known-working framework without any assumptions. If you're using Rails 5, don't forget to add the optional: true bit to your belongs_to declaration on Comment. Otherwise you will find that you cannot save an Item without comments.
>
> Now to the naming conventions.
>
> If you use the <%= render @items %> trick, then you have to have a file named views/items/_item.html.erb. Inside that partial, the local variable item will be loaded by the Rails framework if you have passed a collection named @items in your render call. All of that is the optimized "magic" way of doing this. It is a shortcut, and it can be expanded to:
>
> <%= render partial: 'items/item', collection: @items %>
>
> If you name the partial something other than the singular of the collection, then you have to use the long form. If your partial is in a different object's view hierarchy than the one that is rendering it, then you also need to put that part in the partial name (how I used items/item above).
>
> So the same thing could be rendered as:
>
> <%= render partial: 'foos/foo', collection: @items %>
>
> and the partial would be changed to name the instance variable inside it 'foo'.
>
> If you really get into the weeds, then you need to have an iterator in the parent page, and really spell it out for Rails:
>
> <%- @items.each do |item| -%>
>   <%= render partial: 'items/item', locals: { item: item } %>
> <%- end -%>
>
> The shortcuts are there to make your production work faster, and if you follow the conventions, then they just work in a way that may confuse you at first. I certainly was confused the first time I did this, particularly when I tried to use a non-conventional setup. It took me a long time for the penny to drop that the name of the instance variable inside the partial was NOT based on the name of the object being rendered, but rather the partial's filename.
>
> Walter
>
>
> What about the js to render the comment form into the div of the right post id?

Walk first, then run. This code will link to a form, which will load in another page view. Once you have that working, and it can submit, and it can save a comment, then optimize the flow by injecting the form into the page. You will modify the working code, by adding remote: true to the link to the new comment form, and you will add a new.js.erb file to the comments directory, which will inject the form into the parent page. But you cannot do that step before you get the normal new page view form of this working entirely.

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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="iYyvxE-lDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="iYyvxE-lDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/21f1179b-0caa-40f3-971c-f66d119c9e82%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/21f1179b-0caa-40f3-971c-f66d119c9e82%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/21f1179b-0caa-40f3-971c-f66d119c9e82%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/21f1179b-0caa-40f3-971c-f66d119c9e82%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


I previously had it working and when you clicked a comment link a new page with a form would load

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d4264c9b-c7b8-4aa2-9706-3aa8c7ed4b6a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

Walter Lee Davis

> On Jun 17, 2017, at 11:31 AM, fugee ohu <[hidden email]> wrote:
>
> I previously had it working and when you clicked a comment link a new page with a form would load
>
>

If you have the form appearing on a new page now, and the form works and can submit and save a comment, then you can change the link to the comment form to include remote: true. Then create a new.js.erb file in the same views folder as your current new.html.erb file.

Use Gist or another pastbin to show me what your _form.html.erb looks like, and what your index.html.erb (specifically your content_tag_for helper) looks like at the moment. That will determine what we put into the new.js.erb file.

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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/D3A79444-5096-4DB8-A7ED-47EA5B418E4D%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu


On Saturday, June 17, 2017 at 9:32:55 PM UTC-4, Walter Lee Davis wrote:

> On Jun 17, 2017, at 11:31 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="SoojmXbGDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> I previously had it working and when you clicked a comment link a new page with a form would load
>
>

If you have the form appearing on a new page now, and the form works and can submit and save a comment, then you can change the link to the comment form to include remote: true. Then create a new.js.erb file in the same views folder as your current new.html.erb file.

Use Gist or another pastbin to show me what your _form.html.erb looks like, and what your index.html.erb (specifically your content_tag_for helper) looks like at the moment. That will determine what we put into the new.js.erb file.

Walter

I have <%= render post.comments %> in  _post.html.erb This doesn't use the view app/views/comments/_comment.html.erb I don't know what view it calls, it doesn't seem to call any partial view at all ... ?

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ad17469d-6a02-403a-890f-4dbc63290c4c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu
In reply to this post by Walter Lee Davis


On Saturday, June 17, 2017 at 9:32:55 PM UTC-4, Walter Lee Davis wrote:

> On Jun 17, 2017, at 11:31 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="SoojmXbGDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> I previously had it working and when you clicked a comment link a new page with a form would load
>
>

If you have the form appearing on a new page now, and the form works and can submit and save a comment, then you can change the link to the comment form to include remote: true. Then create a new.js.erb file in the same views folder as your current new.html.erb file.

Use Gist or another pastbin to show me what your _form.html.erb looks like, and what your index.html.erb (specifically your content_tag_for helper) looks like at the moment. That will determine what we put into the new.js.erb file.

Walter

 I can't figure out what variables from _post.html.erb will be available to _comment.html.erb Also, do I need the index action in the comments_controller.rb and an index view Maybe I need it to chain the two partials together Here they are https://gist.github.com/mices/d9aa8b344c214c4a422ffc8a538eb566

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

Walter Lee Davis

> On Jun 23, 2017, at 4:06 AM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Saturday, June 17, 2017 at 9:32:55 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 17, 2017, at 11:31 AM, fugee ohu <[hidden email]> wrote:
> >
> > I previously had it working and when you clicked a comment link a new page with a form would load
> >
> >
>
> If you have the form appearing on a new page now, and the form works and can submit and save a comment, then you can change the link to the comment form to include remote: true. Then create a new.js.erb file in the same views folder as your current new.html.erb file.
>
> Use Gist or another pastbin to show me what your _form.html.erb looks like, and what your index.html.erb (specifically your content_tag_for helper) looks like at the moment. That will determine what we put into the new.js.erb file.
>
> Walter
>
>  I can't figure out what variables from _post.html.erb will be available to _comment.html.erb Also, do I need the index action in the comments_controller.rb and an index view Maybe I need it to chain the two partials together Here they are https://gist.github.com/mices/d9aa8b344c214c4a422ffc8a538eb566

There's a few fundamental flaws here. You have the link to create a new comment inside the comment partial, not the post partial. The post needs to be the holder of this link, because there is only supposed to be one link per post that reads "Add a comment". Also, your link_to is missing the first argument -- there's no link text being supplied -- so I'm surprised if this template even compiles and displays.

You're also missing the surrounding HTML that I suggested to you when you asked how to get this into the "Ajax" format. Without those surrounding content_tag_for wrappers, the return from Rails has no idea where to insert the form or the updated comments. What I believe you need to end up with is something that looks like this (in _post.html.erb):

content_tag_for :div, post, 'comments' do

  content_tag_for :div, post, 'comment_form_holder' do
    link_to 'Add a Comment', new_comment_path(commentable_id: post.id, commentable_type: 'Post'), remote: true
  end

  render 'comments/comment', collection: post.comments

end

So what you should get is a structure like this:

<div id="post_comments_123">
  <div id="post_comment_form_holder_123">
    <a href="/comments/new?commentable_id=123&commentable_type=Post" data-remote="true">Add a Comment</a>
  </div>
  <div id="post_comment_345">
    Hello, this is a comment
  </div>
</div>

When someone clicks the Add a Comment link, because it is set to remote: true, the comments/new.js.erb template will render (by default), and as we discussed earlier, that template is set to update the appropriate container (post_comment_form_holder_123) with the new comment form contents. So the action is you click the link, and the link disappears, replaced by the form to add a comment.

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 [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/DBC87105-4F49-4650-9187-7BF7FA957CDE%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu


On Friday, June 23, 2017 at 8:50:56 AM UTC-4, Walter Lee Davis wrote:

> On Jun 23, 2017, at 4:06 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="32uyBfHpDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Saturday, June 17, 2017 at 9:32:55 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 17, 2017, at 11:31 AM, fugee ohu <[hidden email]> wrote:
> >
> > I previously had it working and when you clicked a comment link a new page with a form would load
> >
> >
>
> If you have the form appearing on a new page now, and the form works and can submit and save a comment, then you can change the link to the comment form to include remote: true. Then create a new.js.erb file in the same views folder as your current new.html.erb file.
>
> Use Gist or another pastbin to show me what your _form.html.erb looks like, and what your index.html.erb (specifically your content_tag_for helper) looks like at the moment. That will determine what we put into the new.js.erb file.
>
> Walter
>
>  I can't figure out what variables from _post.html.erb will be available to _comment.html.erb Also, do I need the index action in the comments_controller.rb and an index view Maybe I need it to chain the two partials together Here they are <a href="https://gist.github.com/mices/d9aa8b344c214c4a422ffc8a538eb566" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fmices%2Fd9aa8b344c214c4a422ffc8a538eb566\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFQ5QaPMNfbTP7OSia4JTImAVzmIA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fmices%2Fd9aa8b344c214c4a422ffc8a538eb566\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFQ5QaPMNfbTP7OSia4JTImAVzmIA&#39;;return true;">https://gist.github.com/mices/d9aa8b344c214c4a422ffc8a538eb566

There's a few fundamental flaws here. You have the link to create a new comment inside the comment partial, not the post partial. The post needs to be the holder of this link, because there is only supposed to be one link per post that reads "Add a comment". Also, your link_to is missing the first argument -- there's no link text being supplied -- so I'm surprised if this template even compiles and displays.

You're also missing the surrounding HTML that I suggested to you when you asked how to get this into the "Ajax" format. Without those surrounding content_tag_for wrappers, the return from Rails has no idea where to insert the form or the updated comments. What I believe you need to end up with is something that looks like this (in _post.html.erb):

content_tag_for :div, post, 'comments' do

  content_tag_for :div, post, 'comment_form_holder' do
    link_to 'Add a Comment', new_comment_path(commentable_id: <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">post.id, commentable_type: 'Post'), remote: true
  end

  render 'comments/comment', collection: post.comments

end

So what you should get is a structure like this:

<div id="post_comments_123">
  <div id="post_comment_form_holder_123">
    <a href="/comments/new?commentable_id=123&commentable_type=Post" data-remote="true">Add a Comment</a>
  </div>
  <div id="post_comment_345">
    Hello, this is a comment
  </div>
</div>

When someone clicks the Add a Comment link, because it is set to remote: true, the comments/new.js.erb template will render (by default), and as we discussed earlier, that template is set to update the appropriate container (post_comment_form_holder_123) with the new comment form contents. So the action is you click the link, and the link disappears, replaced by the form to add a comment.

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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="32uyBfHpDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="32uyBfHpDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


There's only one list of comments per post so why can't the link for a new comment be in the comments partial In any resource the index view has a link to new, from the folder for that resource ... i dunno, are you sure it has to be in the post folder/containers?

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c2506dc3-9c6c-484f-8271-96621b43801c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

Walter Lee Davis

> On Jun 23, 2017, at 5:29 PM, fugee ohu <[hidden email]> wrote:
>
> There's only one list of comments per post so why can't the link for a new comment be in the comments partial In any resource the index view has a link to new, from the folder for that resource ... i dunno, are you sure it has to be in the post folder/containers?


The link is in the _comment partial (singular). There is no _comments (plural) partial. If there was, then you could put the link there. But here's the thing. A post has_many comments. Until the first one is put in, you will see zero comments, and thus (if you put the link in the _comment (singular) partial, there won't be any link, because the _comment partial will be rendered zero times.

But further, if you put it where you did, you have some more problems. You will have a new link below each comment. Unless you are planning to allow comments on comments (some sort of threaded nesting structure), then this will look awful and make no sense. So if you are somehow able to render the first comment, the second one will get its own Add Comment link. Beyond that, in order to inject the form into the page the way you want to, you need to insert the form into some identifiable DOM object, which you didn't provide in your sample templates.

Please try what I suggest -- I've been doing this for a large number of years, sometimes badly and without understanding, and I'd like to see the penny drop for you. I promise it can make sense once you stop pushing so hard to go a different way.

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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CB3EC818-8BFB-4BB5-8EAE-56F25909A31E%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

Walter Lee Davis

> On Jun 23, 2017, at 5:58 PM, Walter Lee Davis <[hidden email]> wrote:
>
>
>> On Jun 23, 2017, at 5:29 PM, fugee ohu <[hidden email]> wrote:
>>
>> There's only one list of comments per post so why can't the link for a new comment be in the comments partial In any resource the index view has a link to new, from the folder for that resource ... i dunno, are you sure it has to be in the post folder/containers?
>
>
> The link is in the _comment partial (singular). There is no _comments (plural) partial. If there was, then you could put the link there. But here's the thing. A post has_many comments. Until the first one is put in, you will see zero comments, and thus (if you put the link in the _comment (singular) partial, there won't be any link, because the _comment partial will be rendered zero times.
>
> But further, if you put it where you did, you have some more problems. You will have a new link below each comment. Unless you are planning to allow comments on comments (some sort of threaded nesting structure), then this will look awful and make no sense. So if you are somehow able to render the first comment, the second one will get its own Add Comment link. Beyond that, in order to inject the form into the page the way you want to, you need to insert the form into some identifiable DOM object, which you didn't provide in your sample templates.
>
> Please try what I suggest -- I've been doing this for a large number of years, sometimes badly and without understanding, and I'd like to see the penny drop for you. I promise it can make sense once you stop pushing so hard to go a different way.
>
> Walter
>

You may want to read this step-by-step I wrote for a lunch-n-learn at Penn: https://gist.github.com/walterdavis/3a960dfdc529a5adf0dd6cff9e644fb0 

It covers starting with a fully inline layout, and moving into partials. Funny enough, it features articles and comments.

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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/C7B1D4BF-6529-4043-8486-62441C84B5B3%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: *.js.erb files

fugee ohu
In reply to this post by Walter Lee Davis


On Friday, June 23, 2017 at 8:50:56 AM UTC-4, Walter Lee Davis wrote:

> On Jun 23, 2017, at 4:06 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="32uyBfHpDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Saturday, June 17, 2017 at 9:32:55 PM UTC-4, Walter Lee Davis wrote:
>
> > On Jun 17, 2017, at 11:31 AM, fugee ohu <[hidden email]> wrote:
> >
> > I previously had it working and when you clicked a comment link a new page with a form would load
> >
> >
>
> If you have the form appearing on a new page now, and the form works and can submit and save a comment, then you can change the link to the comment form to include remote: true. Then create a new.js.erb file in the same views folder as your current new.html.erb file.
>
> Use Gist or another pastbin to show me what your _form.html.erb looks like, and what your index.html.erb (specifically your content_tag_for helper) looks like at the moment. That will determine what we put into the new.js.erb file.
>
> Walter
>
>  I can't figure out what variables from _post.html.erb will be available to _comment.html.erb Also, do I need the index action in the comments_controller.rb and an index view Maybe I need it to chain the two partials together Here they are <a href="https://gist.github.com/mices/d9aa8b344c214c4a422ffc8a538eb566" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fmices%2Fd9aa8b344c214c4a422ffc8a538eb566\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFQ5QaPMNfbTP7OSia4JTImAVzmIA&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgist.github.com%2Fmices%2Fd9aa8b344c214c4a422ffc8a538eb566\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFQ5QaPMNfbTP7OSia4JTImAVzmIA&#39;;return true;">https://gist.github.com/mices/d9aa8b344c214c4a422ffc8a538eb566

There's a few fundamental flaws here. You have the link to create a new comment inside the comment partial, not the post partial. The post needs to be the holder of this link, because there is only supposed to be one link per post that reads "Add a comment". Also, your link_to is missing the first argument -- there's no link text being supplied -- so I'm surprised if this template even compiles and displays.

You're also missing the surrounding HTML that I suggested to you when you asked how to get this into the "Ajax" format. Without those surrounding content_tag_for wrappers, the return from Rails has no idea where to insert the form or the updated comments. What I believe you need to end up with is something that looks like this (in _post.html.erb):

content_tag_for :div, post, 'comments' do

  content_tag_for :div, post, 'comment_form_holder' do
    link_to 'Add a Comment', new_comment_path(commentable_id: <a href="http://post.id" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fpost.id\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNENbMwCHR_oS2BIpfisFlDNvniqcw&#39;;return true;">post.id, commentable_type: 'Post'), remote: true
  end

  render 'comments/comment', collection: post.comments

end

So what you should get is a structure like this:

<div id="post_comments_123">
  <div id="post_comment_form_holder_123">
    <a href="/comments/new?commentable_id=123&commentable_type=Post" data-remote="true">Add a Comment</a>
  </div>
  <div id="post_comment_345">
    Hello, this is a comment
  </div>
</div>

When someone clicks the Add a Comment link, because it is set to remote: true, the comments/new.js.erb template will render (by default), and as we discussed earlier, that template is set to update the appropriate container (post_comment_form_holder_123) with the new comment form contents. So the action is you click the link, and the link disappears, replaced by the form to add a comment.

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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="32uyBfHpDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="32uyBfHpDgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/54955443-ff5b-42cd-9931-f70ba9c073f3%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


Thanks Walter Just to add, the additional options in div_for or content_tag_for are pre-pended "comment_post_345" , "comment_form_holder_post_123"

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/428adae9-a178-45dc-b784-78942c681a84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...