두 모델 : Activity
및 Comment
이 있습니다. Comment
belongs_to :activity
및 Activity
has_many :comments
. 활동을 구축, 나는 그래서, @activities.each do |activity|
조건 부분 _activities_feed.rb
를 렌더링 activity_presenter.rb
컨트롤러간에 데이터 전송
class ActivityPresenter < SimpleDelegator
attr_reader :activity
def initialize(activity, view)
super(view)
@activity = activity
end
def render_activity
div_for activity do
link_to((gravatar_for activity.user, {size: 20}) + activity.user.name, activity.user) + ' ' + render_partial
end
end
def render_partial
locals = {activity: activity, presenter: self}
locals[activity.trackable_type.underscore.to_sym] = activity.trackable
render partial_path, locals
end
def partial_path
partial_paths.detect do |path|
lookup_context.template_exists? path, nil, true
end || raise("No partial found for activity in #{partial_paths}")
end
def partial_paths
[
"activities/#{activity.trackable_type.underscore}/#{activity.action}",
"activities/#{activity.trackable_type.underscore}",
"activities/activity"
]
end
end
사용하고이 Railscast (Activity feed from scratch)
을 따랐습니다. each do |comment|
activity.comments.new
(형태)으로 목적을을
activity.comments
블록을 업데이트하기위한 form for activity
와
activity
그리고 이것은 문제가이 부분에서 는 I 3 개 블록을 갖는다. 하나의보기에 2 개의 양식이 있고 2submit
버튼이 2update
또는create
작업을 호출하기 때문에 과comments
컨트롤러에서 새 메모를 만들기위한 양식을 렌더링해야합니다. 그리고 물론, 나는 더 나은 애플 리케이션 로직을 만들기 위해 필요합니다.그러나
activity
을@activities.each do |activity|
에서 보낼 수 없습니다.이 부분은comments_controller
으로 부분적으로 사용합니다.나는
@activity = Activity.find(params[:id])
,@comment = Activity.comments.new(params[:activity_id])
,@comments = Activity.comments.all
activities
및comments
컨트롤러를 추가하려고하고, 또한@comment
에@activity
및activity.comments.new
에activity
을 변경했습니다. 그러나 도움이되지 않습니다. 컨트롤러 간의 상호 작용의 기본 논리는 이해할 수 없지만 실수는 찾을 수 없습니다.그래서, 내
activities_controller
의index
조치는 다음과 같습니다@activities = Activity.order('updated_at DESC')
그리고이
comments_controller
에 난 단지 확인 작업create
및destroy
작업을해야합니다.도움 주셔서 감사합니다.
UPD :
이 내
_activities_feed.rb
구조 :<% @activities.each do |activity| %> <%= form_for activity, remote: true do |a| %> <%= ActivityPresenter.new(activity, self).render_activity %> . . . <%= link_to ... class: 'add_comment', remote: true %> # Shows activity.new.comment form <%= link_to ... 'Edit' ... %> # edit activity link <%= a.submit 'Update' %> # first submit button for submitting activity changes . . . <% activity.comments.each do |comment| %> # activity.comments block <%= comment.text %> <%= link_to comment.user.name, comment.user %> <% end %> . . . <%= form_for activity.comments.new, remote: true do |f| %> # form for adding comment <%= f.text_area :text, 'rows' => '2', class: 'comment_text' %> <%= f.button 'Cancel', class: 'btn comment_cancel', type: 'button' %> <%= f.submit 'Post', class: 'btn btn-primary', controller: 'comments' %> # second submit button <%= f.hidden_field :activity_id, value: activity.id %> <% end %> <% end %> <% end %>
모든 세부 사항을 덤핑하는 대신, 좀 더 추상적이고 집중적 인 질문을 선호합니다. 나는 당신의 상세한 도메인 논리를 너무 많이 가진 진짜 문제가 무엇인지 파악하기가 어렵다고 느낍니다. –
내가 말하는 '양식'을 보여줄 수 있다면 더 좋을 것입니다. – David
죄송합니다, 양식을 잊어 버려. 추가됨. –