简体   繁体   中英

Rails doesn't render partial even though it says it has

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?

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.

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) %>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM