저는 Rails에 익숙합니다. Hartl의 튜토리얼을 진행하고 있지만, 때때로 나에게 특별한 것을 추가하고 싶습니다. 지금 마이크로 포스트를하고 있는데 마이크로 포스트를 삭제하는 "삭제"기능을 추가하고 싶지만 관리자와이 마이크로 포스트를 만든 사용자 만이 기능을 볼 수 있습니다. 이제는 어떻게해야할지 모르겠다. 설정하기를 원할 때 <% if current_user(micropost.user) && user.admin %>
나는 wrong number of arguments (given 1, expected 0)
오류가 발생하기 때문에. 에서 session_helper.rb
에 나는 def current_user
가 아니라 def current_user(micropost.user)
을 알고 있습니다. 어떻게 든이 micropost.user를 추가하고 이것을 할 수 있습니까? 모든 코드 우는 소리 :1 함수에 2 개의 인수를 추가하려면 어떻게합니까?
app/views/microposts/_micropost.html.erb
<li id="micropost-<%= micropost.id %>">
<%= link_to gravatar_for(micropost.user, size: 50), micropost.user %>
<span class="user"><%= link_to micropost.user.name, micropost.user %></span>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user(micropost.user) && user.admin %>
<%= link_to "delete", micropost, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
app/helpers/session_helper.html.erb
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
너무 쉽게 생각하지 않았습니다. 감사! – hydroxyzinum
BTW, 관리자에게도 기능을 정의 할 수있는 방법이 있습니까? admin으로 delete 버튼을 클릭하면 게시물을 삭제하지 않고 root_url로 리디렉션됩니다. – hydroxyzinum