jquery logic

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

jquery logic

fugee ohu
This is my jquery
$('.new_comment_button').after("<%= j render('form') %>");
I need 
$('.new_comment_button') to be contructed from the values of variables because i can't give every post the same id I don't think using class instead of id in my view would make a  difference How can i do this?

--
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/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: jquery logic

Walter Lee Davis
If you are doing this in a RJS template, then you will have the instance variables available to you, whatever you set at the controller that is rendering this. Just render the form using the @comment you created in the controller.

I just posted an answer to a different question you raised, where I show you how to get the unique IDs on the DOM elements holding members of a collection. You can use that technique with RJS or with a single JS file that does not have any ERB replacement in it.

Walter

> On Jun 15, 2017, at 4:23 PM, fugee ohu <[hidden email]> wrote:
>
> This is my jquery
> $('.new_comment_button').after("<%= j render('form') %>");
> I need
> $('.new_comment_button') to be contructed from the values of variables because i can't give every post the same id I don't think using class instead of id in my view would make a  difference How can i do this?
>
> --
> 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/941f357b-b82a-4e27-82b4-1effb5303d12%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/FAB47945-83FB-4249-9B83-88258AFDEFA0%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: jquery logic

fugee ohu


On Thursday, June 15, 2017 at 5:11:29 PM UTC-4, Walter Lee Davis wrote:
If you are doing this in a RJS template, then you will have the instance variables available to you, whatever you set at the controller that is rendering this. Just render the form using the @comment you created in the controller.

I just posted an answer to a different question you raised, where I show you how to get the unique IDs on the DOM elements holding members of a collection. You can use that technique with RJS or with a single JS file that does not have any ERB replacement in it.

Walter

> On Jun 15, 2017, at 4:23 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="1yryVgkbDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> This is my jquery
> $('.new_comment_button').after("<%= j render('form') %>");
> I need
> $('.new_comment_button') to be contructed from the values of variables because i can't give every post the same id I don't think using class instead of id in my view would make a  difference How can i do this?
>
> --
> 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="1yryVgkbDQAJ" 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="1yryVgkbDQAJ" 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/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%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.


You said: "Just render the form using the @comment you created in the controller. "
That's not gonna change $('.new_comment_button') in new.js.erb

--
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/31d88b61-38f0-4340-97d0-ec6928db1aa3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: jquery logic

fugee ohu
In reply to this post by Walter Lee Davis


On Thursday, June 15, 2017 at 5:11:29 PM UTC-4, Walter Lee Davis wrote:
If you are doing this in a RJS template, then you will have the instance variables available to you, whatever you set at the controller that is rendering this. Just render the form using the @comment you created in the controller.

I just posted an answer to a different question you raised, where I show you how to get the unique IDs on the DOM elements holding members of a collection. You can use that technique with RJS or with a single JS file that does not have any ERB replacement in it.

Walter

> On Jun 15, 2017, at 4:23 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="1yryVgkbDQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
> This is my jquery
> $('.new_comment_button').after("<%= j render('form') %>");
> I need
> $('.new_comment_button') to be contructed from the values of variables because i can't give every post the same id I don't think using class instead of id in my view would make a  difference How can i do this?
>
> --
> 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="1yryVgkbDQAJ" 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="1yryVgkbDQAJ" 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/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%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.


Can I have the link And once I get the DOM id's how do i plug them into my js.erb file to change $('.new_comment_button') to let's say  $('.new_comment_button2') 

--
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/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: jquery logic

Walter Lee Davis

> On Jun 15, 2017, at 6:12 PM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Thursday, June 15, 2017 at 5:11:29 PM UTC-4, Walter Lee Davis wrote:
> If you are doing this in a RJS template, then you will have the instance variables available to you, whatever you set at the controller that is rendering this. Just render the form using the @comment you created in the controller.
>
> I just posted an answer to a different question you raised, where I show you how to get the unique IDs on the DOM elements holding members of a collection. You can use that technique with RJS or with a single JS file that does not have any ERB replacement in it.
>
> Walter
>
> > On Jun 15, 2017, at 4:23 PM, fugee ohu <[hidden email]> wrote:
> >
> > This is my jquery
> > $('.new_comment_button').after("<%= j render('form') %>");
> > I need
> > $('.new_comment_button') to be contructed from the values of variables because i can't give every post the same id I don't think using class instead of id in my view would make a  difference How can i do this?
> >
> > --
> > 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/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
>
> Can I have the link And once I get the DOM id's how do i plug them into my js.erb file to change $('.new_comment_button') to let's say  $('.new_comment_button2')
>

There are a number of different ways to solve this problem. The way I imagine your app, you want to be able to comment on an item, but without having a bunch of forms rendered in the page (one for each item in a collection). Right so far? So the static (non-RJS) way to do this with jQuery is to use the DOM and JS to send the correctly unique variable to your comments controller to let it know which item you are commenting on.

# items_controller.rb

  def index
    @items = Item.all.page(params[:page])
  end

# views/items/index.html.erb
<ul id="items">
  <%= render @items %>
</ul>

# views/items/_item.html.erb
<%= content_tag_for item, :li do %>
  <%= content_tag :hi, item.headline %>
  <%= simple_format item.description %>
<%- end -%>

# assets/javascripts/item_comments.js
$(document).on('turbolinks:load', function(){
  $('#items li').each(function(){
    var elm = $(this);
    elm.append('<a href="/comments/new?item_id=' + elm.attr('id').split('_')[1] + '">Comment</a>');
  });
});

That gets you a link to a CommentsController, which in turn will need to decode the (bare) attribute item_id out of its querystring, and figure out which item you are commenting on.

The #content_tag_for method, which you no doubt read the documentation for, will always create a unique id on the content tag it creates, using the instance variable you pass to it to figure out what that should be. For my contrived example, that will be 'item_123' or 'item_456' or similar. Model name, singular, lower-case, followed by an underscore, followed by the ID of the instance.

It's trivial in JavaScript (using jQuery or not) to read that attribute, split it by the underscore character, and take the number off the end. So your JavaScript stays static, uses the DOM as a data source, and you don't have to write a different link for each of the items in your list.

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/c5c586c2-70f7-452c-86af-b2e247482464%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/94FAD8F5-E12D-4785-94E1-58DA00AA8E08%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: jquery logic

fugee ohu


On Thursday, June 15, 2017 at 6:33:02 PM UTC-4, Walter Lee Davis wrote:

> On Jun 15, 2017, at 6:12 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DokBp3wfDQAJ" 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:11:29 PM UTC-4, Walter Lee Davis wrote:
> If you are doing this in a RJS template, then you will have the instance variables available to you, whatever you set at the controller that is rendering this. Just render the form using the @comment you created in the controller.
>
> I just posted an answer to a different question you raised, where I show you how to get the unique IDs on the DOM elements holding members of a collection. You can use that technique with RJS or with a single JS file that does not have any ERB replacement in it.
>
> Walter
>
> > On Jun 15, 2017, at 4:23 PM, fugee ohu <[hidden email]> wrote:
> >
> > This is my jquery
> > $('.new_comment_button').after("<%= j render('form') %>");
> > I need
> > $('.new_comment_button') to be contructed from the values of variables because i can't give every post the same id I don't think using class instead of id in my view would make a  difference How can i do this?
> >
> > --
> > 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/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%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.
>
>
> Can I have the link And once I get the DOM id's how do i plug them into my js.erb file to change $('.new_comment_button') to let's say  $('.new_comment_button2')
>

There are a number of different ways to solve this problem. The way I imagine your app, you want to be able to comment on an item, but without having a bunch of forms rendered in the page (one for each item in a collection). Right so far? So the static (non-RJS) way to do this with jQuery is to use the DOM and JS to send the correctly unique variable to your comments controller to let it know which item you are commenting on.

# items_controller.rb

  def index
    @items = Item.all.page(params[:page])
  end

# views/items/index.html.erb
<ul id="items">
  <%= render @items %>
</ul>

# views/items/_item.html.erb
<%= content_tag_for item, :li do %>
  <%= content_tag :hi, item.headline %>
  <%= simple_format item.description %>
<%- end -%>

# assets/javascripts/item_comments.js
$(document).on('turbolinks:load', function(){
  $('#items li').each(function(){
    var elm = $(this);
    elm.append('<a href="/comments/new?item_id=' + elm.attr('id').split('_')[1] + '">Comment</a>');
  });
});

That gets you a link to a CommentsController, which in turn will need to decode the (bare) attribute item_id out of its querystring, and figure out which item you are commenting on.

The #content_tag_for method, which you no doubt read the documentation for, will always create a unique id on the content tag it creates, using the instance variable you pass to it to figure out what that should be. For my contrived example, that will be 'item_123' or 'item_456' or similar. Model name, singular, lower-case, followed by an underscore, followed by the ID of the instance.

It's trivial in JavaScript (using jQuery or not) to read that attribute, split it by the underscore character, and take the number off the end. So your JavaScript stays static, uses the DOM as a data source, and you don't have to write a different link for each of the items in your list.

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="DokBp3wfDQAJ" 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="DokBp3wfDQAJ" 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/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%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 Is this what everyone else is doing ? 

--
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/eddc55a6-4f53-4194-9169-3c348d2a425a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: jquery logic

fugee ohu
In reply to this post by Walter Lee Davis


On Thursday, June 15, 2017 at 6:33:02 PM UTC-4, Walter Lee Davis wrote:

> On Jun 15, 2017, at 6:12 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DokBp3wfDQAJ" 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:11:29 PM UTC-4, Walter Lee Davis wrote:
> If you are doing this in a RJS template, then you will have the instance variables available to you, whatever you set at the controller that is rendering this. Just render the form using the @comment you created in the controller.
>
> I just posted an answer to a different question you raised, where I show you how to get the unique IDs on the DOM elements holding members of a collection. You can use that technique with RJS or with a single JS file that does not have any ERB replacement in it.
>
> Walter
>
> > On Jun 15, 2017, at 4:23 PM, fugee ohu <[hidden email]> wrote:
> >
> > This is my jquery
> > $('.new_comment_button').after("<%= j render('form') %>");
> > I need
> > $('.new_comment_button') to be contructed from the values of variables because i can't give every post the same id I don't think using class instead of id in my view would make a  difference How can i do this?
> >
> > --
> > 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/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%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.
>
>
> Can I have the link And once I get the DOM id's how do i plug them into my js.erb file to change $('.new_comment_button') to let's say  $('.new_comment_button2')
>

There are a number of different ways to solve this problem. The way I imagine your app, you want to be able to comment on an item, but without having a bunch of forms rendered in the page (one for each item in a collection). Right so far? So the static (non-RJS) way to do this with jQuery is to use the DOM and JS to send the correctly unique variable to your comments controller to let it know which item you are commenting on.

# items_controller.rb

  def index
    @items = Item.all.page(params[:page])
  end

# views/items/index.html.erb
<ul id="items">
  <%= render @items %>
</ul>

# views/items/_item.html.erb
<%= content_tag_for item, :li do %>
  <%= content_tag :hi, item.headline %>
  <%= simple_format item.description %>
<%- end -%>

# assets/javascripts/item_comments.js
$(document).on('turbolinks:load', function(){
  $('#items li').each(function(){
    var elm = $(this);
    elm.append('<a href="/comments/new?item_id=' + elm.attr('id').split('_')[1] + '">Comment</a>');
  });
});

That gets you a link to a CommentsController, which in turn will need to decode the (bare) attribute item_id out of its querystring, and figure out which item you are commenting on.

The #content_tag_for method, which you no doubt read the documentation for, will always create a unique id on the content tag it creates, using the instance variable you pass to it to figure out what that should be. For my contrived example, that will be 'item_123' or 'item_456' or similar. Model name, singular, lower-case, followed by an underscore, followed by the ID of the instance.

It's trivial in JavaScript (using jQuery or not) to read that attribute, split it by the underscore character, and take the number off the end. So your JavaScript stays static, uses the DOM as a data source, and you don't have to write a different link for each of the items in your list.

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="DokBp3wfDQAJ" 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="DokBp3wfDQAJ" 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/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%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.


Not rendering the new comment links Only the posts render on the page but the divs render fine <div class="post" id="post_14"> and so on


 <% @user.posts.each do |commentable| %>
 <%= commentable.content %>
 <% unless commentable.attachment.blank? %>
  <%= image_tag(commentable.attachment, height: 250) %><br>
 <% end %>
 <%= div_for(commentable) do %> 
 <% end %> 
<% end %>

$(document).on('turbolinks:load', function(){ 
  $('#commentable').each(function(){ 
    var elm = $(this); 
    elm.append('<a href="/comments/new?commentable_id=' + elm.attr('id').split('_')[1] + '">Comment</a>'); 

--
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/8b4e836c-95d9-4866-8649-a1e3217fb516%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: jquery logic

Jason Fleetwood-Boldt
In reply to this post by fugee ohu
Generally very large and apps built after 2013 have moved on to frameworks like Backbone, React, Ember, etc. 

For a simple or prototype app, RJS is totally appropriate. And as a beginning developer it's totally a great idea to learn how jQuery and Turbolinks works. 

On a larger app, coding like that you will wind up with "jQuery soup" which in my experience never really works out to build a maintainable codebase. 

Rails doesn't really have a robust "javascript solution" other than RJS, taking the agnostic stance that it is a back-end technology. Having said that, the latest version of Rails had a strongly-worded stake in the sand statement about Rails & the future of Javascript, in which a package manager (Yarn) was made the default. 

But even with Rails moving in that direction, it still means if you want to build a large, robust front-end app you'll probably be picking a JS framework or writing a bare-bones one yourself (or using a bare-bones one, like Backbone, which is what I prefer.)

What you are doing with RJS/UJS is the way to go for your purposes for a small app. 

-Jason




On Jun 15, 2017, at 10:45 PM, fugee ohu <[hidden email]> wrote:

Thanks Is this what everyone else is doing ? 

If you'd like to reply by encrypted email you can find my public key on jasonfleetwoodboldt.com (more about setting GPG: https://gpgtools.org

--
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/9D0C6D0C-FB8E-48A2-BCC4-BE8D99CCFBB9%40datatravels.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: jquery logic

fugee ohu
In reply to this post by Walter Lee Davis


On Thursday, June 15, 2017 at 6:33:02 PM UTC-4, Walter Lee Davis wrote:

> On Jun 15, 2017, at 6:12 PM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="DokBp3wfDQAJ" 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:11:29 PM UTC-4, Walter Lee Davis wrote:
> If you are doing this in a RJS template, then you will have the instance variables available to you, whatever you set at the controller that is rendering this. Just render the form using the @comment you created in the controller.
>
> I just posted an answer to a different question you raised, where I show you how to get the unique IDs on the DOM elements holding members of a collection. You can use that technique with RJS or with a single JS file that does not have any ERB replacement in it.
>
> Walter
>
> > On Jun 15, 2017, at 4:23 PM, fugee ohu <[hidden email]> wrote:
> >
> > This is my jquery
> > $('.new_comment_button').after("<%= j render('form') %>");
> > I need
> > $('.new_comment_button') to be contructed from the values of variables because i can't give every post the same id I don't think using class instead of id in my view would make a  difference How can i do this?
> >
> > --
> > 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/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/941f357b-b82a-4e27-82b4-1effb5303d12%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.
>
>
> Can I have the link And once I get the DOM id's how do i plug them into my js.erb file to change $('.new_comment_button') to let's say  $('.new_comment_button2')
>

There are a number of different ways to solve this problem. The way I imagine your app, you want to be able to comment on an item, but without having a bunch of forms rendered in the page (one for each item in a collection). Right so far? So the static (non-RJS) way to do this with jQuery is to use the DOM and JS to send the correctly unique variable to your comments controller to let it know which item you are commenting on.

# items_controller.rb

  def index
    @items = Item.all.page(params[:page])
  end

# views/items/index.html.erb
<ul id="items">
  <%= render @items %>
</ul>

# views/items/_item.html.erb
<%= content_tag_for item, :li do %>
  <%= content_tag :hi, item.headline %>
  <%= simple_format item.description %>
<%- end -%>

# assets/javascripts/item_comments.js
$(document).on('turbolinks:load', function(){
  $('#items li').each(function(){
    var elm = $(this);
    elm.append('<a href="/comments/new?item_id=' + elm.attr('id').split('_')[1] + '">Comment</a>');
  });
});

That gets you a link to a CommentsController, which in turn will need to decode the (bare) attribute item_id out of its querystring, and figure out which item you are commenting on.

The #content_tag_for method, which you no doubt read the documentation for, will always create a unique id on the content tag it creates, using the instance variable you pass to it to figure out what that should be. For my contrived example, that will be 'item_123' or 'item_456' or similar. Model name, singular, lower-case, followed by an underscore, followed by the ID of the instance.

It's trivial in JavaScript (using jQuery or not) to read that attribute, split it by the underscore character, and take the number off the end. So your JavaScript stays static, uses the DOM as a data source, and you don't have to write a different link for each of the items in your list.

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="DokBp3wfDQAJ" 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="DokBp3wfDQAJ" 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/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/c5c586c2-70f7-452c-86af-b2e247482464%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.


This is confusing In your example you're using items i'm using posts but now I'm questioning why I'm using polymorphic association commentable in the first place I forget To follow your example I have to yield to the posts/index action and view within the users page in my case localhost://page/:id it's like the users wall where the posts are shown I dunno how to do that yet but is that what I should do, then I could match the posts index to items index in your example Thanks in advance and thanks for showing me this example 

--
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/020483ac-45e3-4cbb-999c-610a42d6c67a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.