【问题标题】:Form using collection_select, edit action works entirely but create does not使用 collection_select 的表单,编辑操作完全有效,但创建不
【发布时间】:2014-11-24 05:13:53
【问题描述】:

我正在使用以下表单和控制器。如果我创建一个新通知,除了 Campus_id 之外的所有内容都会被保存。

虽然我从下拉列表中选择了不同的校园参数,但似乎给出了错误的校园参数。如果我之后编辑相同的条目,那么它会被保存吗?发生了什么,我该如何解决?

编辑和创建操作使用相同的表单。 (是部分)

值得注意的是,我为校园 (has_many) 和通知 (belongs_to) 使用了浅层路线。

routes.rb

  shallow do
    resources :campus do
      resources :notifications
    end
  end

表格:

<%= form_for [@campus,@notification] do |f| %>
  <% if @notification.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@notification.errors.count, "error") %> prohibited this notification from being saved:</h2>

      <ul>
      <% @notification.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :post %><br>
    <%= f.text_area :post %>
  </div>
  <div class="field">
    <%= f.label :campus %><br>
    <%= f.collection_select(:campus_id, Campus.all.order('name ASC'), :id, :name, prompt: true) %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这是控制器:

class NotificationsController < ApplicationController
  before_action :set_notification, only: [:show, :edit, :update, :destroy]
  before_action :set_campus, only: [:index, :new, :create]

  def index
    @notifications = @campus.notification
  end

  def show
  end

  def new
    @notification = @campus.notification.new
  end

  def edit
  end

  def create
    @notification = @campus.notification.new(notification_params)

    respond_to do |format|
      if @notification.save
        format.html { redirect_to @notification, notice: 'Notification was successfully created.' }
        format.json { render action: 'show', status: :created, location: @notification }
      else
        format.html { render action: 'new' }
        format.json { render json: @notification.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @notification.update(notification_params)
        format.html { redirect_to @notification, notice: 'Notification was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @notification.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @notification.destroy
    respond_to do |format|
      format.html { redirect_to campu_notifications_url(1) }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_notification
      @notification = Notification.find(params[:id])
    end

    def set_campus
      @campus = Campus.find(params[:campu_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def notification_params
      params.require(:notification).permit(:post, :campus_id)
    end
end

如果我查看日志,我发现输入了错误的参数。

在 84.193.153.106 开始 POST "/campus/1/notifications" 2014-09-29 18:29:33 +0000 开始发布“/campus/1/notifications” 84.193.153.106 在 2014-09-29 18:29:33 +0000 由 NotificationsController#create 作为 HTML 处理由 NotificationsController#create as HTML 参数:{"utf8"=>"_", "authenticity_token"=>"onNSlEFeukwEj2hIAT89wFdIYwjHO5c8lzBlCqMyk31Y=", "通知"=>{"post"=>"sdqfdsfd", "campus_id"=>"3"}, "commit"=>"创建通知", "campu_id"=>"1"} 参数: {"utf8"=>"_", "authenticity_token"=>"onNSlEFeukwEj2hIAT89wFdIYwjHO5c8lzBlCqMyk31Y=", "通知"=>{"post"=>"sdqfdsfd", "campus_id"=>"3"}, "commit"=>"创建通知", "campu_id"=>"1"} 校园负载 (0.4ms) SELECT "campus".* FROM "campus" WHERE "campus"."id" = $1 LIMIT 1 [["id", "1"]] 校园负载 (0.4ms) SELECT "campus".* FROM "campus" WHERE "campus"."id" = $1 LIMIT 1 [["id", "1"]] (0.1ms) BEGIN (0.1ms) BEGIN SQL (28.6ms) INSERT INTO "notifications" ("campus_id", "created_at", "post", "updated_at") 值 ($1, $2, $3, $4) 返回 "id" [["campus_id", 1], ["created_at", Mon, 29 Sep 2014 18:29:34 UTC +00:00],["post","sdqfdsfd"],["updated_at",星期一,9 月 29 日 2014 18:29:34 UTC +00:00]] SQL (28.6ms) 插入“通知” ("campus_id", "created_at", "post", "updated_at") 值 ($1, $2, $3, $4) 返回 "id" [["campus_id", 1], ["created_at", Mon, 29 Sep 2014 18:29:34 UTC +00:00],["post","sdqfdsfd"],["updated_at",星期一,9 月 29 日 2014 18:29:34 UTC +00:00]] (3.5ms) 提交 (3.5ms) 提交

【问题讨论】:

  • 一切正常。在提交表单时查看您的 log/development.log。它可以提供帮助!

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 collection-select


【解决方案1】:

可能希望像这样更改您的 newcreate 操作:

def new
  @notification = @campus.notifications.build
end

def create
  @notification = @campus.notifications.build(notification_params)

  respond_to do |format|
    if @notification.save
      format.html { redirect_to @notification, notice: 'Notification was successfully created.' }
      format.json { render action: 'show', status: :created, location: @notification }
    else
      format.html { render action: 'new' }
      format.json { render json: @notification.errors, status: :unprocessable_entity }
    end
  end
end

campus.build_notification 将实例化一个通知 belongs_to campus。使用 new 需要您将 notification[campus_id] 作为参数的一部分传递。

【讨论】:

  • 我得到一个未定义的方法 `build_notification' 用于 #<0x007fc704f3ab38>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 2014-07-20
  • 2015-08-13
相关资源
最近更新 更多