Rails不像它所说的那样呈现部分

时间:2013-11-01 21:24:43

标签: html ruby-on-rails

我的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)

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

您需要使用渲染ERB标记:

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

答案 1 :(得分:1)

你错过了= -

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

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

答案 2 :(得分:0)

所以从你的布局/关系部分你渲染另一个部分(layouts / edit_relation),对吗?也许这就是问题所在。