I have been working with Rails for a little while now. I got some stuff figured out, but I am struggling with this. I have an array of blog posts, rendered with just their title. In the controller the object looks simply like:
@articles
On my main page I render it through a partial and display just the title.
<%= render 'shared/article_view', :collection => @articles %>
This gives me a list of articles on the main page. When I click on the title I would like to open it up in a separate div. I have this working with a form. However, I'm not sure how to pass a parameter to the javascript that is getting called. I'm assuming I could pass the id to my view_article.js.erb file and could from there call
@article = Article.find_by_id(id)
and then
<p><%= @article.text %></p>
However, I have no idea how to pass any sort of parameter to this file. It may not even work and I could be entirely wrong in doing this. As I said, still learning. Anyway, thanks for the help!
data-idattr on your title element that contains your article id. In JS you would bind a click event to the title els, extract the ids, and make an ajax request to your articles endpoint and return json, which you would render in your div. Let me know if I'm on the right track...data-idis a custom HTML attribute you can set on any element. More info here. Essentially you'd bind the event to elements with classarticleand then infer the id from the attribute using jQuery.