0

I am posting new "tweets" and with jquery and ajax I am able to show the newly posted tweet without refreshing the page. But in my view template, I have an if statement that checks if it is the current_user and if so, displays the delete button.

But Ajax does not read the if statement and does not display the delete button. The delete button shows up if I refresh the page or if I remove the if statement. That defeats the purpose since the button shows up on other users' profiles also.

Here is my view:

<ul class="tweets>
<li>
    <span><%= tweet.content %></span><br>
    <% if current_user?(@user)%>
        <%= link_to "Delete" tweet, method: :delete, data: { confirm: "You sure?" } %>
    <%end%>
</li>
</ul>

My create.js.erb: $('.tweets').prepend('<%= j render(@tweet)%>')

4
  • 1
    do you really expect the ajax request to reread your original view?, unless you're showing the tweet partial? Commented Aug 16, 2013 at 14:42
  • In your partial, try debugging current_user and see if its actually being sent. Just at the end of your content block put a "ID: #{current_user.id}" or something along those lines to see the output. Commented Aug 16, 2013 at 14:52
  • If it does read current_user, then perhaps the if statement is executing to false? Which is strange due to the fact that it works on reload. Perhaps try using a different if syntax? (this is just a random guess / couldn't hurt to try kinda thing). Maybe try "if current_user.id == tweet.user.id" ? Commented Aug 16, 2013 at 15:00
  • @apneadiving, thanks for your insightful input :) Commented Aug 16, 2013 at 15:02

3 Answers 3

2

If current_user is the user object for the current logged in user, and you have a user associated with your tweet. To verify ownership, I would recommend:

    <% if current_user.id == tweet.user.id %>

Edited:

Doing the above will require rails to perform an addition database query to obtain the tweet.user user data. Changing it to the following will eliminate the additional query:

    <% if current_user.id == tweet.user_id %>
Sign up to request clarification or add additional context in comments.

2 Comments

I guess its doing an extra query to get the user data for tweet.user. Perhaps doing if current_user.id == tweet.user_id will save that extra query.
This is what I was trying to get at, nice job Michael. It's been a while since I have even looked at Ruby :)
1

Not at a computer, but try: if current_user == @user

3 Comments

So if you're setting @user = current_user in the controller and then checking if current_user == @user wouldn't that always return true?
If you're using = instead of == then this isn't the answer (you may want to create your own answer and select that). I was curious if current_user was supposed to be compared to @user, not sure if it would work if they're both null. I think I was more questioning whether the conditional is doing what you think it is current_user.
I think this answer may help explain current_user: stackoverflow.com/questions/5575188/…
0

Try setting the current_user to some instance variable on your controller action:

@current = current_user

I believe you don't have access to this variable there, but you do have access to all instance variables you set up before returning the response from the controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.