나는 현재 사용자에 대한 역할이 전무 경우는, 항상 true를 반환 한 문제 같습니다있다 레일 4.잘못 입증 된 경우에도 도우미 방법이 항상 true 반환
def is_blog_owner?(blog_id)
if current_user && blog_id
blog = Blog.find_by(id: blog_id)
roles = current_user.roles_for_blog(blog)
roles.each do |role|
if role.role == 'Blog-Owner'
true
end
end
end
end
에서 다음 헬퍼 메소드가 있습니다.
현재 작동하는 방식은 현재 사용자가 특정 블로그의 블로그 소유자 역할을하는 경우 true를 반환하는 것입니다.
내가 방문 그래서 경우 (사용자 ID로 1) users/1/blogs/2
내가 편집을보고 그때 난 아직 편집을보고 삭제 로그 아웃 한 사용자 ID 2로 로그인 users/1/blogs/2
를 방문하는 방법 이제까지 경우 show.html.erb
에 아래 그림과 같이 삭제됩니다 . 나는 그렇게해서는 안된다.
roles
가 설정됩니다 후
binding.pry
을 던져 버리고 내가 편집을보고 버튼을 삭제하지 말아야한다는 것을 의미한다이 전무 할 2의 사용자 ID를 하나의 블로그 ID에 사용자 ID 2
roles
을 발견,하지만 난 할 수 .. 무슨 일 이니?
<h2><%[email protected]%> Profile</h2>
<p class="text-muted">
Welcome to your blog porfile page. Here you can manage the blog. You can edit or
delete the specific blog.
</p>
<% if is_blog_owner?(params[:id]) %>
<hr>
<h3>Action</h3>
<p class="text-muted">You can currently do the following actions: </p>
<%= link_to "Edit", edit_user_blog_path(current_user.id, @blog.id), :class => 'btn btn-success' %> |
<%= link_to "Delete", user_blog_path(current_user.id, @blog.id),
data: { confirm: "This is permenent. Are you sure?" },
:method => :delete,
:class => 'btn btn-danger'%>
<% end %>
내가해야 내가 <%= is_blog_owner?(params[:id]).inspect
했다과 []
반환 ... OO을 가지고 광고. 거짓을 돌려 보내지 않아야합니까?
레일 4에 익숙하지 않지만 if 문을 통과 할 때 메서드가 false를 반환하지 않습니다 ('true'앞에 반환하면 도움이됩니다 ...).) –
루프에'return'을 사용해야합니다 :'role.role == 'Blog-Owner'이면 true를 반환합니다. 그런 다음 마지막 줄에'false'를 추가하십시오. 따라서 사용자가 소유자 인 경우 메서드가 true를 반환하고 false 인 경우 (루비 메서드는 기본적으로 항상 마지막 행을 반환 함). – markets