내 앱에는 두 가지 모델이 있습니다. 하나는 회의라고하고 다른 하나는 결과입니다. 나는 각 회의 결과를 @outcome=current_meeting.outcome.build(params[:outcome])
을 사용하여 만들고 싶었다. 또한 각 회의에는 하나의 결과 만 있으므로 분명히 has_one
관계입니다. 나는 current_meeting을 얻는 것에 대해 정말로 혼란 스럽다. 나뿐만 회의 및 결과 모델이 있습니다레일에있는 has_one 관계의 현재 ID에 액세스
회의를 모델
나는 새로운 결과는 회의의 쇼 내부에 존재하는 것으로 원하는 결과 모델class Outcome < ActiveRecord::Base
attr_accessible :result
belongs_to :meeting
validates :meeting_id, presence: true
end
class Meeting < ActiveRecord::Base
attr_accessible :info, :meeting_date, :name, :venue
has_many :participants, :dependent => :destroy
has_one :outcome, :dependent => :destroy
validates_presence_of :name, :info, :meeting_date, :venue
end
, 만약 존재하지 않고 현재가 존재한다면 새로운 결과를 창출 할 수 없다.
회의/show.html.erb
<% if @meeting.outcomes.any? %>
<%= render @outcomes %>
<% else %>
<%= link_to "Add the outcome of meeting", new_outcome_path %>
<% end %>
내 컨트롤러는 다음과 같습니다
회의 컨트롤러
def show
@meeting = Meeting.find(params[:id])
@outcomes = @meeting.outcomes.all
end
성과 컨트롤러
def new
@outcome = current_meeting.microposts.new
end
def create
@outcome = current_meeting.outcomes.build(params[:outcome])
if @outcome.save
flash[:success] = "Outcome created!"
redirect_to root_url
else
render 'static_pages/home'
end
end
current_meeting을 찾는 방법을 모르겠습니다. 도와주세요.
감사합니다. 그것은 정말로 많은 도움이되었습니다. – sushilthe