簡體   English   中英

Ruby on Rails 中的嵌套屬性未保存

[英]Nested attributes in Ruby on Rails not saving

我先說我已經查看了以下答案,但仍然沒有找到有效的解決方案(但是,考慮到我可能忽略了某些內容,我將它們包括在內以供參考):

問題描述:我有一個包含 Cue 嵌套表單的表單塊。 表單正確呈現並且塊正確保存,但是提示沒有出現在提示表中,即當提​​交塊時提示沒有保存。 我正在使用 Rails 4.2.5.1。 我在提交時也沒有收到任何錯誤,這使得診斷有點困難。

代碼:

_form.html.erb - 塊

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

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

  <div class="field hidden">
    <%= f.label :block_code, class: "hidden" %><br>
    <%= f.text_field :block_code, class: "form-control hidden" %>
  </div>
  <div class="field">
    <%= f.label :block_duration %><br>
    <div class="input-group">
      <%= f.number_field :block_duration, class: 'text_field form-control', :step => 'any' %>
      <div class="input-group-addon">seconds</div>
    </div>
  </div>
  <div class="field">
    <label>Does this block have a cue associated with it?</label>
    <input type="radio" name="cue" value="cueYes" data-type="cueYes" data-radio="cue"> Yes
    <input type="radio" name="cue" value="cueNo" data-type="cueNo" data-radio="cue" checked> No
    <div class="field" id="cueYes">
      <%= f.fields_for :cues do |ff| %>
        <div class="field hidden">
          <%= ff.label :cue_code, class: "hidden" %><br>
          <%= ff.text_field :cue_code, class: "hidden" %>
        </div>
        <div class="field">
          <%= ff.label "Cue Type" %><br>
          <%= ff.collection_select(:cue_type_code, CueType.all, :cue_type_code, :cue_type_name, {prompt: "Select a cue type..."}, {class: "form-control"}) %>
        </div>
        <div class="field">
          <%= ff.label "Cue Description" %><br>
          <%= ff.text_area :cue_description, class: "form-control" %>
        </div>
        <div class="field">
          <%= ff.label "Cue Method" %><br>
          <%= ff.collection_select( :cue_method_code, CueMethod.all, :cue_method_code, :cue_method_name, {prompt: "Select a cue method..."}, {class: "form-control"}) %>
        </div>
      <% end %>
    </div>
  </div>
  <div class="field">
    <%= f.label "Location" %><br>
    <%= collection_select :block, :location_code, Location.all, :location_code, :location_name, {prompt: "Select a location..."}, {class: "form-control"} %>
  </div>
  <div class="field">
    <%= f.label "Scene" %><br>
    <%= collection_select :block, :scene_code, Scene.all, :scene_code, :actAndScene, {prompt: "Select a scene..."}, {class: "form-control"} %>
  </div>
  <div class="field">
    <%= f.label "Block Description" %><br>
    <%= f.text_area :block_description, class: "form-control" %>
  </div>
  <div class="actions">
    <%= f.submit "Create Block", class: "btn btn-primary" %>
  </div>
<% end %>

blocks_controller.rb

class BlocksController < ApplicationController
  before_action :set_block, only: [:show, :edit, :update, :destroy]

  # GET /blocks
  # GET /blocks.json
  def index
    @blocks = Block.all
  end

  # GET /blocks/1
  # GET /blocks/1.json
  def show
  end

  # GET /blocks/new
  def new
    @block = Block.new

    # Set block code as next integer after max block code.
    @block.block_code = (Block.maximum(:block_code).to_i.next).to_s(2)

  end

  # GET /blocks/1/edit
  def edit
  end

  # POST /blocks
  # POST /blocks.json
  def create
    @block = Block.new(block_params)

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

  # PATCH/PUT /blocks/1
  # PATCH/PUT /blocks/1.json
  def update
    respond_to do |format|
      if @block.update(block_params)
        format.html { redirect_to @block, notice: 'Block was successfully updated.' }
        format.json { render :show, status: :ok, location: @block }
      else
        format.html { render :edit }
        format.json { render json: @block.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /blocks/1
  # DELETE /blocks/1.json
  def destroy
    @block.destroy
    respond_to do |format|
      format.html { redirect_to blocks_url, notice: 'Block was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def block_params
      params.require(:block).permit(:block_code, :block_duration, :cue_code, :location_code, :scene_code, :block_description, :cues_attributes => [:cue_code, :cue_type_code, :cue_description, :cue_method_name])
    end
end

塊.rb

class Block < ActiveRecord::Base
  has_one :cue, -> { where processed: true }
  accepts_nested_attributes_for :cue, allow_destroy: true

end

提示文件

class Cue < ActiveRecord::Base
  belongs_to :block
end

cues_controller.rb

class CuesController < ApplicationController
  before_action :set_cue, only: [:show, :edit, :update, :destroy]

  # GET /cues
  # GET /cues.json
  def index
    @cues = Cue.all
  end

  # GET /cues/1
  # GET /cues/1.json
  def show
  end

  # GET /cues/new
  def new
    @cue = Cue.new

    # Set cue code as next integer after max cue code.
    @cue.cue_code = (Cue.maximum(:cue_code).to_i.next).to_s(2)
  end

  # GET /cues/1/edit
  def edit
  end

  # POST /cues
  # POST /cues.json
  def create
    @cue = Cue.new(cue_params)

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

  # PATCH/PUT /cues/1
  # PATCH/PUT /cues/1.json
  def update
    respond_to do |format|
      if @cue.update(cue_params)
        format.html { redirect_to @cue, notice: 'Cue was successfully updated.' }
        format.json { render :show, status: :ok, location: @cue }
      else
        format.html { render :edit }
        format.json { render json: @cue.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cues/1
  # DELETE /cues/1.json
  def destroy
    @cue.destroy
    respond_to do |format|
      format.html { redirect_to cues_url, notice: 'Cue was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def cue_params
      params.require(:cue).permit(:cue_code, :cue_type_code, :cue_description, :cue_method_code)
    end
end

如果還有什么需要,請告訴我! (如果格式不是很好也很抱歉)。

任何幫助深表感謝!! 謝謝!!

更新 1

if @block.save in blocks_controller.rb (上圖),我目前在行上收到錯誤undefined method 'encoding' for 7:Fixnum 我根據IngoAlbers給出的答案(如下)和這里找到的答案更改了以下內容。

我改變了以下幾點:

blocks_controller.rb

def block_params
  params.require(:block).permit(:block_code, :block_duration, :cue_code, :location_code, :scene_code, :block_description, :cue_attributes => [:id, :cue_code, :cue_type_code, :cue_description, :cue_method_code])
end

_form.html.erb - 塊

<%= f.fields_for :cue, @block.build_cue do |ff| %>

塊.rb

class Block < ActiveRecord::Base
  has_one :cue
  accepts_nested_attributes_for :cue, allow_destroy: true

end

非常感謝到目前為止的幫助! 我想我真的很接近了!

更新 2

因此,我已將block_id作為屬性添加到 Cue 並運行以下兩個遷移:

class AddBlockIdToCues < ActiveRecord::Migration
  def self.up
    add_column :cues, :block_id, :binary
  end

  def self.down
    remove_column :cues, :block_id
  end
end


class AddBelongsToToCues < ActiveRecord::Migration
  def change
    add_reference :cues, :blocks, index: true
    add_foreign_key :cues, :blocks
  end
end

我仍然得到錯誤undefined method 'encoding' for 7:Fixnum就行了if @block.saveblocks_controller.rb

問題應該出在您的fields_for 大概應該是:

<%= f.fields_for :cue do |ff| %>

不是cues因為它只有一個。 然后你需要建立球桿。 這可以在控制器或直接在視圖中完成,例如像這樣:

<%= f.fields_for :cue, @block.build_cue do |ff| %>

在您的塊參數中,您還需要將其更改為cue_attributes ,並且還允許id

def block_params
  params.require(:block).permit(:block_code, :block_duration, :cue_code, :location_code, :scene_code, :block_description, :cue_attributes => [:id, :cue_code, :cue_type_code, :cue_description, :cue_method_name])
end

您還可以在此處閱讀更多信息:

http://guides.rubyonrails.org/form_helpers.html#nested-forms

關於您的第二次更新:

您的第一次遷移添加了一個類型為binary的列block_id 它絕對應該是integer 這就是說,你甚至不需要在所有的第一次遷移,因為你的第二個遷移,將正確地處理這一切,如果你改變blocksblockadd_reference 它應該是這樣的:

class AddBelongsToToCues < ActiveRecord::Migration
  def change
    add_reference :cues, :block, index: true
    add_foreign_key :cues, :blocks
  end
end

add_reference需要添加對一個block而不是多個block的引用。 這將為您創建正確的列。

另見: http : //api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_reference

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM