먼저 첫 번째 것들.뷰는 컨트롤러의 작업을 나타냅니다.따라서 각 컨트롤러에 대해 여러 개의 뷰가 있을 수도 있고 전혀 없을 수도 있습니다.
그래서 렌더링을 하고 싶다면 @reviews
그리고 @logs
동일한 페이지에서 먼저 동일한 작업에서 두 인스턴스 변수를 모두 인스턴스화한 다음 동일한 작업에서 두 부분을 모두 렌더링해야 합니다.어떻게 합니까?쉬운.먼저 방금 보여준 컨트롤러로 이동하여 편집합니다. show
행동.
def show
# You can set the variable in the if-clause
# And you also need to use static finders with a hash as an argument in Rails4
if (@user = User.find_by(name: params[:id]))
@reviews = @user.reviews.all
@logs = @user.logs.all
# You don't need to call render explicitly
# if you render the view with the same name as the action
else
render file: 'public/404', status: 404, formats: [html]
end
end
두번째:너는 네 집으로 가 /app/views/reviews/show.html.erb
템플릿을 만들고 두 부분을 모두 이렇게 배치합니다(이것은 단지 예일 뿐이므로 필요에 맞게 마크업을 조정하세요).
<h1> Reviews and Logs</h1>
<div id="reviews_part">
<%= render @reviews %>
</div>
<div id="logs_part">
<%= render @logs %>
</div>
이제 2개의 새로운 부분을 만듭니다. /app/views/reviews/_review.html.erb
그리고 /app/views/logs/_log.html.erb
그리고 거기에 필요한 모든 마크업을 넣습니다(일반 변수 사용). review
그리고 log
반복되는 객체를 처리하기 위해).Rails는 필요한 만큼 자동으로 해당 부분을 반복합니다.
또는 부분 렌더링을 명시적으로 호출할 수 있습니다.
<div id="reviews_part">
<% @reviews.each do |review| %>
<%= render review %>
which is the same as
<%= render partial:"reviews/review", locals:{review:review} %>
<% end %>
</div>