0

我的 Rails 服务器说它已经在我的页面中呈现了我的部分内容,但内容不存在。即使我使用 Chrome DevTools 检查页面上的元素,应该呈现的 div 也不存在。

编辑:修复了该问题,但现在该部分内部的部分将不会呈现。我会针对这个问题提出一个新问题。

show.html.erb(有修复):

<% 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(部分):

<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(部分中的部分):

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

我的服务器日志显示部分已呈现:

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)

有任何想法吗?

4

3 回答 3

2

您需要使用渲染 ERB 标签:

<%= render 'layouts/relation' unless current_user?(@user) %>
于 2013-11-01T21:30:42.247 回答
1

你在这里少了一个=——

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

调用render仍会生成字符串,这就是您在日志中看到它的原因;没有等号,它不会出现在页面上。

于 2013-11-01T21:33:21.293 回答
0

因此,从您的布局/关系部分中,您正在渲染另一个部分(布局/编辑关系),对吗?也许这就是问题所在。

于 2013-11-01T21:31:18.013 回答