jeudi 16 juillet 2015

Rails 3: Render a profile snippet side feed using javascript to query a User model?

I'm new to Ruby on Rails/ Javascript- so please, be gentle. (Using Rails 3, Jurby, Ruby 1.8.6)

I posted a similar question about rendering user information to a current page, in the form of a side feed. My new problem is now getting that information into the feed that appears on my page, once I click a username link.

My webapp allows users to generate posts that are stamped with the poster username and time.(see screengrab below)

web app screengrab

My problem is the right-hand side feed appears, but it's empty. My earlier attempts to fill this with user information resulted in the side feed rendering the same user information, regardless of who's username link I clicked.

Here is the code for the main page _signed_in.html.erb:

<div class="article_feed">
    <div class="article_snippet">
        <%= form_for(@micropost) do |f| %>
            <%= render 'shared/error_messages', object: f.object %>
            <div class="field">
                <%= f.text_area :content, placeholder: "Got anything to say...?" %>
            </div>
                <%= f.submit "Post", class: "btn btn-large btn-primary" %>
        <% end %>   
        Communicator.. (<%= Micropost.count %>)
    </div>

    <% @microposts.each do |mp| %>
        <div class="article_snippet">               
            <%= render mp %>
        </div>
    <% end %>
    <%= will_paginate @microposts%>     
</div>  

<div class="comments_feed">
    <div class ="comments_snippet" id="user" style="display: none;">
        <%= @user %>
    </div>
</div>

Here is my micropost partial _micropost.html.erb, it uses javascript to render the side feed that is to contain a snippet of the linked users profile. This should appear when the blue username link it clicked.

<script type="text/javascript">
    function show() {
        document.getElementById("user").style.display = "block";
    }
</script>

<li>
    <span class = "content">
        <%= micropost.content %>
    </span>

    <span class = "timestamp">
        posted <%= time_ago_in_words(micropost.created_at) %> ago
         by <%= link_to User.find(micropost.user_id).username, 
           "javascript:void(0)", onclick: "show()" %>       
    </span>
</li>

...and my controller for this page pages_controller.rb

def home
    @micropost = current_user.microposts.build if signed_in?
    @microposts = Micropost.paginate(page: params[:page], 
      :per_page => 4).order('id DESC') if signed_in?  
    #@user = User.find(parans[:id]) 
    #gives the error of not having a user_id
end

Now, back to my question. I've thought that maybe I need my javascript code in the partial to query the user model. Would mean that I need to pass the user_id to the javascript? The problem I thought might relate to this is the way the feed is generated, using a "do block", meaning the javascript would only ever get the user_id of the last user. This was what caused my earlier problem of getting the same user appear in the side feed, regardless of which user I clicked.

Any help with this new problem would be fantastic!

Aucun commentaire:

Enregistrer un commentaire