我想通过关系编辑一个has_many,但不是编辑关系,而是创建一个新模型。
以我的形式:
<%= form_for @service do |f| %>
<%= f.fields_for :service_users do |ac| %>
<% end %>
<% end %>
在我的模特中:
class Service < ActiveRecord::Base
has_many :service_users
has_many :users, :through => :service_users
accepts_nested_attributes_for :service_users
end
开始情况: 当我更新评论字段时:
更新后,我将编辑后的关系视为第一个的重复。 在某种程度上,我必须检查是否已经存在关系,但是如何?
更新
我的控制器:
class ServicesController < ApplicationController
before_action :set_service, only: [:show, :edit, :update, :destroy, :users]
before_filter :authenticate_user!
# GET /services
# GET /services.json
def index
services = current_user.available_services
@available_services = services.group_by { |t| t.date.beginning_of_month }
end
# GET /services/1
# GET /services/1.json
def show
# service_users = current_user.service_users
#
# Service.find_each do |service|
# unless service_users.detect { |m| m.service_id == service.id }
# current_user.service_users.build service_id: service.id
# end
# end
#
@available_users = @service.available_users.group_by { |u| u.group }
@planned_users = @service.planned_users.group_by { |u| u.group }
@reserve_users = @service.reserve_users.group_by { |u| u.group }
end
# GET /services/new
def new
@service = Service.new
end
# GET /services/1/edit
def edit
@service.service_users.create
end
# POST /services
# POST /services.json
def create
@service = Service.new(service_params)
p service_params
respond_to do |format|
if @service.save
format.html { redirect_to @service, notice: 'Service was successfully created.' }
format.json { render action: 'show', status: :created, location: @service }
else
format.html { render action: 'new' }
format.json { render json: @service.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /services/1
# PATCH/PUT /services/1.json
def update
respond_to do |format|
p service_params[:service_users_attributes]
if @service.update(service_params)
format.html { redirect_to @service, notice: 'Service was successfully updated.' }
format.json { render action: 'show', status: :ok, location: @service }
else
format.html { render action: 'edit' }
format.json { render json: @service.errors, status: :unprocessable_entity }
end
end
end
def users
@users = @service.users
end
# DELETE /services/1
# DELETE /services/1.json
def destroy
@service.destroy
respond_to do |format|
format.html { redirect_to services_url }
format.json { head :no_content }
end
end
def destroy_association
if params[:id].present?
ServiceUser.find(params[:id]).delete
redirect_to root_path
end
end
def make_user_available_for_service
p '########'
p params
p @service
redirect_to root_path
end
private
# Use callbacks to share common setup or constraints between actions.
def set_service
@service = Service.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def service_params
params.require(:service).permit(:date, :comments,
service_users_attributes: [:user_id,
:service_id,
:availability,
:comments],
service_groups_attributes: [:service_id,
:group_id,
:start_time,
:end_time])
end
end
答案 0 :(得分:1)
尝试构建而不是创建。
像这样:
def edit
@service.service_users.build
end
并检查您的service_params。 您缺少service_users的id。