Вопрос

I have two models in my app. One is called meetings and the other is outcome. I wanted to create the outcome of each meeting using: @outcome=current_meeting.outcome.build(params[:outcome]). Further, each meeting would have only one outcome so it is clearly a has_one relationship. I am really confused about getting the current_meeting. I have the meetings and outcomes models as:

Meeting Model

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

Outcome Model

class Outcome < ActiveRecord::Base
  attr_accessible :result
  belongs_to :meeting
  validates :meeting_id, presence: true
end

I want the new outcome to be present inside the show of the meeting, if there are none present and if there is one present creating new outcome should be made impossible

meetings/show.html.erb

<% if @meeting.outcomes.any? %>
   <%= render @outcomes %>
<% else %>
   <%= link_to "Add the outcome of meeting", new_outcome_path %>
<% end %>

My controllers are:

Meetings controller

def show
  @meeting = Meeting.find(params[:id])
  @outcomes = @meeting.outcomes.all
end

Outcomes controller

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

I don't know how to find out the current_meeting. Please help.

Это было полезно?

Решение

First of all, the question is very confusing as to the plurality of outcome vs outcomes. If a Meeting has_one Outcome then you you would use the singular form when referring to the reference. Basically, given has_one :outcome, ":outcome" is the method name to be used. So you'd say meeting.outcome instead of meeting.outcomes. And the build method for has_one would be like meeting.build_outcome instead of meeting.outcomes.build. The latter is the api for a has_many relationship.

With that out of the way, if you want to get the current Meeting from the Outcomes controller, the best way to do this is with nested resources. So in the routes file you'd have, e.g.:

resources :meetings do
  resources :outcomes
end

After you do that, run rake routes to see the routes available to you. In there you'll see an expected url format of POST /meetings/:id/outcomes which you would use here. So in this case, the create method would get the Meeting object from params[:id], from which the outcome relationship can be created.

Другие советы

At first glance it does not seem like you are defining current_meeting anywhere. You probably already know this and if so the case would be that you are unsure of how/where to define it. You will probably need to do that somewhere in the code. This could mean saying something like this meeting is current because it is during the current time and/or today. This is based on how your app works to determine this logic.

In your controller or in a helper you will need to write a method that gives you the current meeting if one exists. From there the current_meeting variable in your controller will be set correctly and should call your other methods right.

If I have misunderstood the issue I apologize and please provide any other details you can and I can try to help.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top