0

Before anyone marks this as a duplicate, I was told from a previous question to open a new question with this problem. The solution for my last question didn't work for this problem, unfortunately.

My rails server says that it has rendered my partial in my page, but the content is not there. Even when I inspect the elements on the page using Chrome DevTools, the div that is supposed to be rendered isn't there.

EDIT: Changed all my partials to shared from layouts, anthough i haven't changed that here.

show.html.erb (with fix):

<% provide(:title, @user.name) %>
<% provide(:pagetitle, @user.name) %>

<%= render'layouts/relation' unless current_user?(@user)%>

<div id="stats">
    Player Statistics will appear here.
</div>

<div id="characters">
    Player Characters will appear here.
</div>

_relation.html.erb (the partial):

<div id="relation">
    <% if current_user.friends?(@user) %>
        <%= render 'layouts/edit_relation', locals: { action: "rm-friend" } %>
    <% elsif current_user.req_friends?(@user) %>
        Friend Request Pending...
    <% elsif current_user.pend_friends?(@user) %>
        <%= render 'layouts/edit_relation', locals: { action: "add-friend" } %>
        <%= render 'layouts/edit_relation', locals: { action: "rej-friend" } %>
    <% else %>
        <%= render 'layouts/edit_relation', locals: { action: "req-friend" } %>
    <% end %>
</div>

_edit_relation.html.erb (the partial within the partial):

<% case :action %>
    <% when "req-friend"%>
        <%= form_for(current_user.relation.build(character: @user.id, type: "freq"), remote: true) do |f| %>
            <div><%= f.hidden_field :character %></div>
            <div><%= f.hidden_field :type %></div>
            <%= f.submit "Add Friend", class: "btn btn-large btn-primary" %>
        <% end %>

    <% when "add-friend"%>
        <%= form_for(current_user.relation.build(character: @user.id, type: "friend"), remote: true) do |f| %>
            <div><%= f.hidden_field :character %></div>
            <div><%= f.hidden_field :type %></div>
            <%= f.submit "Accept Request", class: "submit" %>
        <% end %>

    <% when "rej-friend" %>
        <%= form_for(current_user.reverse_relation.find_by(owner: @user.id, type: "freq"), html: { method: :delete }, remote: true) do |f| %>
            <%= f.submit "Reject Request", class: "submit" %>
        <% end %>

    <% when "rm-friend"%>
        <%= form_for(current_user.reverse_relation.find_by(owner: @user.id, type: "friend"), html: { method: :delete }, remote: true) do |f| %>
            <%= f.submit "Remove Friend", class: "submit" %>
        <% end %>

<% end %>

My server log that says that the partial has been rendered:

Started GET "/melv" for 127.0.0.1 at 2013-11-01 21:22:21 +0000
Processing by UsersController#show as HTML
  Parameters: {"name"=>"melv"}
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token
" = '35599bfa491cb6b5e10f164c0191d51cd773f173' LIMIT 1
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."name" = 'melv'
 LIMIT 1
  Relation Load (1.0ms)  SELECT "relations".* FROM "relations" WHERE "relations"
."owner" = $1 AND "relations"."character" = 1 AND "relations"."type" = 'friend'
LIMIT 1  [["owner", 2]]
  Relation Load (0.0ms)  SELECT "relations".* FROM "relations" WHERE "relations"
."owner" = $1 AND "relations"."character" = 1 AND "relations"."type" = 'freq' LI
MIT 1  [["owner", 2]]
  Relation Load (1.0ms)  SELECT "relations".* FROM "relations" WHERE "relations"
."owner" = $1 AND "relations"."character" = 2 AND "relations"."type" = 'freq' LI
MIT 1  [["owner", 1]]
  Rendered layouts/_edit_relation.html.erb (0.0ms)
  Rendered layouts/_relation.html.erb (14.0ms)
  Rendered users/show.html.erb within layouts/application (23.0ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  Rendered layouts/_header.html.erb (1.0ms)
  Relation Load (1.0ms)  SELECT "relations".* FROM "relations" WHERE "relations"
."owner" = 1 LIMIT 1
  Rendered layouts/_aside.html.erb (4.0ms)
  Rendered layouts/_flash.html.erb (0.0ms)
Completed 200 OK in 299ms (Views: 290.0ms | ActiveRecord: 5.0ms)

Any ideas?

Melvin Sowah
  • 700
  • 1
  • 10
  • 29

3 Answers3

3

This line has problem

<% case :action %>

action is supposed to be a local variable, not symbol. The symbol :action doesn't match any of the values in when, so Rails went through this partial in no time and did nothing.

To fix, change it to

<% case action %>

Beside, there are two more things you need to improve

  1. It's not good to put partial under layouts if it is not part of layout. MarkoHiel also mentioned that.

  2. Too much logic in view makes the view code looks very ugly.

Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • This didn't work. This throws a NameError in Users#show: `undefined local variable or method 'action' for #<#:0x31c1448>` – Melvin Sowah Nov 02 '13 at 09:40
  • 1. Try rename `action` to others, this is a Rails method as well, probably name collision. 2. Try to comment out all codes in that parial and load it without local variables. Debug step by step and you'll know where the problem is. – Billy Chan Nov 02 '13 at 09:55
  • I renamed `action` to `doaction` and it threw the same error. I took out all the logic and rendered the `Accept Request` branch, and it rendered just fine. Am i using `locals` correctly, or is there a better way of doing it? – Melvin Sowah Nov 02 '13 at 10:43
  • I fixed it; apparently you can't use `locals` like I did, which I find very weird: http://stackoverflow.com/a/4402766/2407183 – Melvin Sowah Nov 02 '13 at 10:58
  • Yes, that's correct style. I always use `partial` if rendering partial, and never did your style before which doesn't user `partial` but the template name only, so I don't know that will bring problem. – Billy Chan Nov 02 '13 at 11:14
3

It probably said rendered because the code is executed, just not output. You need to put an '=' character after the '<%' tag so it will properly output.

<%= render'layouts/relation' unless current_user?(@user) %>

Jeff
  • 109
  • 2
  • That's a massive mistake on my part, sorry. A previous question had already solved that problem for me, but I neglected to reflect the change in the question when I copy and pasted it here. The person that solved that problem told me to post a new question. – Melvin Sowah Nov 02 '13 at 09:55
1

I don't know if that helps. But normally you don't put partials into the layouts folder. Try making a new folder and render it from there.

MarkoHiel
  • 15,481
  • 2
  • 21
  • 29