我有一个recipient
和category
型号。它是一个简单的1类别关联有很多收件人。当我尝试更新recipient
表单并指定category
时,它不会保存到记录中。如果我使用控制台并手动更新记录,例如Recipient.update(9, category_id: 13)
,我看到分配给收件人的正确类别,但是当我尝试编辑/更新记录时,它不会保存到新选择的类别。
这是我的recipient
型号
class Recipient < ActiveRecord::Base
belongs_to :category
accepts_nested_attributes_for :category
end
这是我的category
型号
class Category < ActiveRecord::Base
has_many :recipients
validates :category, presence: true
default_scope { order('category')}
end
这是recipient
控制器
class RecipientsController < ApplicationController
def index
@recipients = Recipient.order(:recipient_name).page(params[:page])
end
def new
@recipient = Recipient.new
end
def show
@recipient = Recipient.find(params[:id])
end
def create
@recipient = Recipient.new(recipient_params)
if @recipient.save
redirect_to recipients_path
else
render :new
end
end
def edit
@recipient = Recipient.find(params[:id])
end
def update
@recipient = Recipient.find(params[:id])
recipient_params = params.require(:recipient).permit(:recipient_name, :alternate_name, :description, :city, :state, :country, category_attributes: [:category, :id])
@recipient.update_attributes(recipient_params)
redirect_to recipient_path(id: @recipient.id)
end
def destroy
@recipient = Recipient.find(params[:id])
@recipient.destroy
redirect_to recipients_path
end
private
def recipient_params
params.require(:recipient).permit(:recipient_name, :alternate_name, :description, :city, :state, :country, product_attributes: [:product_name, recipient_products: [:recipient_id, :product_id]], channel_attributes: [:channel_name, recipient_channels: [:recipient_id, :channel_id]], category_attributes: [:id, :category])
end
end
这是edit
视图
<div class="row">
<div class="col-sm-6">
<h2>Edit <%= @recipient.recipient_name %></h2>
<%= simple_form_for @recipient do |form| %>
<%= form.error_notification %>
<%= form.input :recipient_name, placeholder: 'Recipient', label: 'Recipient Name' %>
<%= form.input :alternate_name, placeholder: 'Alternate Name' %>
<%= form.association :category, label_method: :category, value_method: :id %>
<%= form.input :description, placeholder: 'Description'%>
<%= form.input :city, placeholder: 'City'%>
<%= form.input :state, placeholder: 'State' %>
<%= form.input :country, as: :country, priority: ['US', 'CA'] %>
<%= form.button :submit, 'Update Recipient', {:class=>"btn btn-secondary"} %>
<%= link_to "Cancel", :back, {:class=>"btn btn-default"} %>
<% end %>
</div>
</div>
这是我的routes.rb
文件
Rails.application.routes.draw do
root to: 'home#index'
resources :media_points
resources :products
resources :channels
resources :recipients
resources :media_point_products
resources :distributions
resources :categories do
resources :recipients
end
get '/listing' => "listing#index"
devise_for :admins
devise_for :users
resources :users
end
答案 0 :(得分:0)
我把它写成答案,因为格式很容易引起评论:
更改您的recipient_params私有方法:
category_attributes: [:category, :id]
到
category_id: param_that_has_category_id_here
此外,您有两条收件人路线,它将使用第一条匹配路线,而不是您进一步向下的类别路线中的嵌套收件人。如果私有方法中的第一个更改没有修复它,我会在simpleform中指定嵌套情况,如下所示,因为您可能希望使用嵌套路由:
simple_form_for [@category, @recipient], url: 'nested_route_path_here' do |f|
只需将@category = Category.new
添加到收件人控制器中的新操作,然后在您的创建操作中,您将通过参数[:category_id]发送类别参数