在任何人将此标记为重复之前,我从前一个问题中被告知要打开一个有关此问题的新问题。不幸的是,我的上一个问题的解决方案不适用于这个问题。
我的rails服务器说它已经在我的页面中呈现了我的部分,但内容不存在。 即使我使用Chrome DevTools检查页面上的元素,也不会出现应该渲染的div。
编辑:将我的所有部分内容从shared
更改为layouts
,我没有在此更改。
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)
有什么想法吗?
答案 0 :(得分:3)
此行有问题
<% case :action %>
action
应该是一个局部变量,而不是符号。符号:action
与when
中的任何值都不匹配,因此Rails很快就会经历这个部分并且什么也没做。
要修复,请将其更改为
<% case action %>
此外,还有两件事需要改进
如果不是布局的一部分,将部分放在layouts
下是不好的。 MarkoHiel也提到过。
视图中的逻辑过多会导致视图代码看起来非常难看。
答案 1 :(得分:3)
它可能表示已渲染,因为代码已执行,而不是输出。您需要在'&lt;%'标记后面添加一个'='字符,以便正确输出。
<%= render'layouts/relation' unless current_user?(@user) %>
答案 2 :(得分:1)
我不知道这是否有帮助。但通常你不会将partials放入layouts文件夹中。尝试制作一个新文件夹并从那里进行渲染。