0

我正在尝试提供一个表单来为 features_index 页面上的每个功能创建一个“消息”。不幸的是,我收到错误“找不到没有 ID 的功能”。

我是否正确地通过了当地人?参数似乎没有 feature_id!(也许现在是 @ 的时候了?

功能/索引视图:

 - @features.each do |feature|
%tr
  %td= feature.project_id
  %td= feature.subject
  %td= feature.details
  %td= link_to 'Show', feature
  %td= link_to 'Edit', edit_feature_path(feature)
  %td= link_to 'Destroy', feature, :confirm => 'Are you sure?', :method => :delete

  - if feature.messages.length > 0
    - feature.messages.each do |message|
      = render partial: "message_form", locals: {message: message}
  - else
    %td
      = render partial: "message_form", locals: {message: Message.new(feature_id: feature)}

%br/

特征/_message_form:

= simple_form_for(message) do |f|
  = f.error_notification

.form-inputs
  -# = f.input :feature_id
  -# = f.input :sender
  -# = f.input :receiver
  = f.input :subject
  = f.input :details

.form-actions
  = f.button :submit

和消息控制器:

def create
@feature = Feature.find(params[:message][:feature_id])

@message = @feature.messages.build(params[:message])

respond_to do |format|
  if @message.save
    flash[:notice] = "Message sent!"
    format.html { redirect_to @message, notice: 'Message was successfully created.' }
    format.json { render json: @message, status: :created, location: @message }
  else
    flash[:notice] = "Message failed to send."
    format.html { render action: "new" }
    format.json { render json: @message.errors, status: :unprocessable_entity }
  end
end
end

谢谢!

4

1 回答 1

1

我相信您将需要一个 hidden_​​field 将 feature_id 的参数传递到您的创建操作中。

f.hidden_input :feature_id
于 2012-06-08T01:56:03.693 回答