Ruby on rails 初始创建时未保存嵌套模型
我有一个双嵌套模型“days”,最初不会保存,但在以后编辑记录时会保存。我相信这是由于我如何在第一个模型控制器中创建第二个模型“时间表” 我的对象及其关联: 活动有一个时间表 日程属于\u至\u活动有\u多天 天属于时间表 我想在活动中自动创建日程安排,有没有办法做到这一点,或者这仅仅是一种不好的做法,我应该以不同的方式尝试这一点Ruby on rails 初始创建时未保存嵌套模型,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个双嵌套模型“days”,最初不会保存,但在以后编辑记录时会保存。我相信这是由于我如何在第一个模型控制器中创建第二个模型“时间表” 我的对象及其关联: 活动有一个时间表 日程属于\u至\u活动有\u多天 天属于时间表 我想在活动中自动创建日程安排,有没有办法做到这一点,或者这仅仅是一种不好的做法,我应该以不同的方式尝试这一点 class EventsController < ApplicationController before_action :set_event, o
class EventsController < ApplicationController
before_action :set_event, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [ :show ]
def index
@events = current_user.events.all
end
def show
end
def new
@event = current_user.events.build
@schedule = @event.build_schedule(event_id: @event.id, user_id: current_user.id, )
end
def edit
if @event.user_id != current_user.id
flash[:error] = "Not allowed"
redirect_to root_path
else
@schedule = @event.schedule
end
end
# POST /events
# POST /events.json
def create
@event = current_user.events.create(event_params)
@schedule = @event.create_schedule(event_id: @event.id, user_id: current_user.id)
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
def update
if @event.user_id == current_user.id
respond_to do |format|
# puts "call sanitize_url"
# sanitize_url
if @event.update(event_params)
format.html { redirect_to @event, notice: 'Event was successfully updated.' }
format.json { render :show, status: :ok, location: @event }
else
format.html { render :edit }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
else
flash[:error] = "Not allowed"
redirect_to root_path
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
if @event.user_id == current_user.id
@event.destroy
respond_to do |format|
format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
format.json { head :no_content }
end
else
flash[:error] = "Not allowed"
redirect_to root_path
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end
def event_params
params.require(:event).permit(
:name, :description,
links_attributes: [:id, :label, :url, :_destroy],
schedule_attributes: [:id, :_destroy, days_attributes: [:id, :date, :_destroy]]
)
end
end
正如您所看到的,由于某些原因,它无法将参数保存数天。因此我发现删除这一行
@schedule = @event.create_schedule(event_id: @event.id, user_id: current_user.id)
通过create方法解决了这个问题。我猜,因为我已经使用@event参数和嵌套表单保存了它,所以这个额外的创建方法中断了这个过程
显然,我还有很多东西需要学习Rails 所以我想去掉这条线
@schedule = @event.create_schedule(event_id: @event.id, user_id: current_user.id)
通过create方法解决了这个问题。我猜,因为我已经使用@event参数和嵌套表单保存了它,所以这个额外的创建方法中断了这个过程
显然,我还有很多东西需要学习Rails 你的事件参数方法在哪里?可能是强参数的问题。请发布
表单
代码。@Pavan我更新了表单代码,尽管我相信问题一定是在我处理日程模型的方式上,因为如果我以后编辑它,一切都会正常工作,只是在最初创建时没有。你的事件参数方法在哪里?可能是强参数的问题。请发布表单
代码。@Pavan我更新了表单代码,尽管我相信问题一定是在我处理日程模型的方式上,因为如果我以后编辑它,一切都会正常工作,只是在最初创建时没有。你的事件参数方法在哪里?可能是因为强参数有问题。请发布表单
代码。@Pavan我更新了表单代码,尽管我相信问题一定是在我处理日程模型的方式上,因为如果我以后编辑它,一切都会正常,只是在最初创建时没有。
<div class="nested-fields">
<%= f.input :date, input_html: { class: "form-input form-control" } %>
<%= link_to_remove_association "Remove", f, class: "form-button btn btn-default" %>
<hr>
SQL (0.5ms) INSERT INTO "schedules" ("user_id", "created_at", "updated_at") VALUES (?, ?, ?) [["user_id", 1], ["created_at", "2015-06-04 02:34:38.724966"], ["updated_at", "2015-06-04 02:34:38.724966"]]
(4.5ms) commit transaction
(0.1ms) begin transaction
Day Load (0.2ms) SELECT "days".* FROM "days" WHERE "days"."schedule_id" = ? [["schedule_id", 31]]
SQL (0.4ms) DELETE FROM "days" WHERE "days"."id" = ? [["id", 24]]
SQL (0.2ms) DELETE FROM "schedules" WHERE "schedules"."id" = ? [["id", 31]]
(1.5ms) commit transaction
@schedule = @event.create_schedule(event_id: @event.id, user_id: current_user.id)