嵌套的has_many属性(Ruby on Rails)抛出错误

时间:2016-04-10 20:13:09

标签: ruby-on-rails ruby-on-rails-4 foreign-keys nested-attributes has-many

所以这可能与我之前提到的问题有些相关,所以here是指向它的链接,以防万一。

所以,目前我收到错误undefined method 'location' for #<Block:0x6503478>

我已经获得了has_one工作的cue关系,但似乎问题是与has_many和{{1}的block_block关系}}

传递的参数是:

block_character

这是我的块表格(views / block / _form.html.erb):

{"utf8"=>"✓",
 "authenticity_token"=>"blahblahblah",
 "block"=>{"block_code"=>"1",
 "block_duration"=>"",
 "cue"=>"no",
 "cue_attributes"=>{"cue_code"=>"",
 "cue_type_code"=>"",
 "cue_description"=>"",
 "cue_method_code"=>""},
 "location_code"=>"1",
 "scene_code"=>"1",
 "block_description"=>"",
 "block_block"=>{"block_block_code"=>"",
 "primary_block_code"=>"",
 "secondary_block_code"=>"",
 "block_block_start"=>""},
 "block_character"=>{"block_character_code"=>"",
 "character_code"=>"",
 "block_code"=>"",
 "character_action"=>"",
 "character_motivation"=>""}},
 "blockblock"=>{"block"=>"no"},
 "blockblockdirect"=>{"blockdirect"=>"before"},
 "blockchar"=>{"character"=>"no"},
 "commit"=>"Create Block"}

此外,以下是与<%= 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 "cue", "Does this block have a cue associated with it?" %> <!-- Add whether it is specific/generic cue --> <%= radio_button_tag "block[cue]", "yes", false %> Yes <%= radio_button_tag "block[cue]", "no", true %> No <div class="field" id="cue_fields" style="display:none;"> <%= f.fields_for :cue, @block.build_cue 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", required: true} %> </div> <div class="field"> <%= f.label "Scene" %><br> <%= f.collection_select :scene_code, Scene.all, :scene_code, :actAndScene, {prompt: "Select a scene..."}, {class: "form-control", required: true} %> </div> <div class="field"> <%= f.label "Block Description" %><br> <%= f.text_area :block_description, class: "form-control" %> </div> <!-- This needs work --> <div class="field"> <%= label "blockblock", "Is this block associated with any other blocks?" %> <%= radio_button_tag "blockblock[block]", "yes", false %> Yes <%= radio_button_tag "blockblock[block]", "no", true %> No <div class="field" id="blockblock_fields" style="display:none;"> <ol> <li> <%= label "blockdirect", "Does this block come directly before or after another block?" %> <%= radio_button_tag "blockblockdirect[blockdirect]", "before", true %> Before <%= radio_button_tag "blockblockdirect[blockdirect]", "after", false %> After <%= f.fields_for :block_block do |gg| %> <div class="field hidden"> <%= gg.label :block_block_code, class: 'hidden' %><br> <%= gg.text_field :block_block_code, class: 'hidden' %> </div> <div class="field" id="blockblock_after_fields" style="display:none;"> <%= gg.label "Primary Block Code" %><br> <%= gg.text_field :primary_block_code, class: 'form-control' %> </div> <div class="field" id="blockblock_before_fields"> <%= gg.label "Secondary Block Code" %><br> <%= gg.text_field :secondary_block_code, class: 'form-control' %> </div> <div class="field"> <%= gg.label "Block Start" %><br> <p><i>This field indicates the amount of time after the first block starts that the second block should begin, i.e. if you type "1" here, then the second block will start 1 second after the first block starts</i></p> <div class="input-group"> <%= gg.number_field :block_block_start, class: 'text_field form-control', :step => 'any' %> <div class="input-group-addon">seconds</div> </div> </div> <% end %> </li> </ol> </div> </div> <!-- This needs work --> <div class="field"> <%= label "character", "Are any characters associated with this block?" %> <%= radio_button_tag "blockchar[character]", "yes", false %> Yes <%= radio_button_tag "blockchar[character]", "no", true %> No <div class="field" id="character_fields" style="display:none;"> <ol> <%= f.fields_for :block_character do |hh| %> <li> <div class="field hidden"> <%= hh.label :block_character_code, class: 'hidden' %><br> <%= hh.text_field :block_character_code, class: 'hidden' %> </div> <div class="field"> <%= hh.label "Character" %><br> <%= hh.collection_select :character_code, Character.all, :character_code, :character_name, {prompt: "Select a character..."}, {class: "form-control"} %> </div> <div class="field hidden"> <%= hh.label :block_code, class: 'hidden' %><br> <%= hh.text_field :block_code, class: 'hidden' %> </div> <div class="field"> <%= hh.label :character_action %><br> <%= hh.text_field :character_action, class: 'form-control' %> </div> <div class="field"> <%= hh.label :character_motivation %><br> <%= hh.text_area :character_motivation, class: 'form-control' %> </div> </li> <% end %> </ol> </div> </div> <div class="actions"> <%= f.submit "Create Block", class: "btn btn-primary" %> </div> <% end %> blockblock_character相关联的模型:

block_block

以下是class Block < ActiveRecord::Base validates_presence_of :location validates_presence_of :scene has_one :cue has_many :block_blocks has_many :block_characters accepts_nested_attributes_for :cue, allow_destroy: true accepts_nested_attributes_for :block_blocks, allow_destroy: true accepts_nested_attributes_for :block_characters, allow_destroy: true attr_accessor :block_blocks attr_accessor :block_characters end class BlockBlock < ActiveRecord::Base belongs_to :block end class BlockCharacter < ActiveRecord::Base belongs_to :block end 的控制器:

block

最后,我为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, :cue_attributes => [:id, :cue_code, :cue_type_code, :cue_description, :cue_method_code], :block_block_attributes => [:id, :block_block_code, :primary_block_code, :secondary_block_code, :block_block_start], :block_character_attributes => [:id, :block_character_code, :character_code, :block_code, :character_action, :character_motivation]) end end id添加了foreign_keyblock_block的迁移:

block_character

老实说,非常感谢你们的帮助,我绝不是一个先进的Rails用户,这一切都非常有帮助。如果您需要更多信息,请与我们联系!

更新1

所以我在block.rb(模型文件)中更改了以下内容,正如Vijay所建议的那样(非常感谢!!!):

class AddBelongsToToBlockBlocks < ActiveRecord::Migration
  def change
    add_reference :block_blocks, :block, index: true
    add_foreign_key :block_blocks, :blocks
  end
end

class AddBelongsToToBlockCharacters < ActiveRecord::Migration
  def change
    add_reference :block_characters, :block, index: true
    add_foreign_key :block_characters, :blocks
  end
end

此时,块保存(并显示在索引视图中),但block_block和block_character不会这样做。但是没有相关的错误......

更新2

目前这是params提交的内容:

Params submitted

这很奇怪,因为它说cue,block_block和block_character是不允许的参数,即使在控制器中它们是:

class Block < ActiveRecord::Base
    validates_presence_of :location_code
    validates_presence_of :scene_code

  has_one :cue
  has_many :block_blocks
  has_many :block_characters

  accepts_nested_attributes_for :cue, allow_destroy: true
  accepts_nested_attributes_for :block_blocks, allow_destroy: true
  accepts_nested_attributes_for :block_characters, allow_destroy: true

    attr_accessor :block_blocks
    attr_accessor :block_characters
end

1 个答案:

答案 0 :(得分:1)

不确定您为什么在Rails 4中添加了这些

    attr_accessor :block_blocks
    attr_accessor :block_characters

尝试删除它们,看看它们是否在干扰控制器(我猜测,但如果你设置一个jsfiddle则很乐意玩)