在一对一关系的嵌套资源中创建和新操作?

时间:2015-04-26 23:05:57

标签: ruby-on-rails ruby-on-rails-4

我有这种关系:

appointment has_one :feedback
feedback belongs_to :appointment

途径:

resources :appointments do
  resources :feedbacks
end

我对新反馈的链接是这样的:

new_appointment_feedback_path(@current_appointment)

我的表格:

<%= simple_form_for [@appointment, @feedback], html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>

<%= f.input :feedback %>

<% end %>

最后在我的反馈控制器中:

def new
    @appointment = Appointment.find(params[:appointment_id])
    @feedback = @appointment.build_feedback
end

def create 

end

错误atm:

NoMethodError in FeedbacksController#new
undefined method `build' for nil:NilClass

有人可以帮我解决这个问题。 TNX

更新:

新行动现在正在运作。

I made my form like this:

<%= simple_form_for [@appointment, @appointment.build_feedback], html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |f| %>

反馈控制器:

def new
    @appointment = Appointment.find(params[:appointment_id])
end

def create 
    @appointment = Appointment.find(params[:appointment_id])
    @feedback = @appointment.build_feedback(feedback_params)
    if @feedback.save
        ....
    else
        render 'new' 
    end
end

def feedback_params
    params.require(:feedback).permit(:id, :feedback)
end

0 个答案:

没有答案