简体   繁体   English

Rails 简单表单关联提交未更新

[英]Rails Simple Form Association submission not updating

I have a problem with has_many relationships setting up forms that do not update the values after submission.我在设置提交后不更新值的表单的 has_many 关系方面存在问题。

Example例子

Upon submission of a new character, vn_id does not get updated and in Rails Consoles when I try to check for characters in a Vn, it returns empty.提交新字符后,vn_id 不会更新,并且在 Rails 控制台中,当我尝试检查 Vn 中的字符时,它返回空。

I am trying to set up a form for characters which belongs to Vn which will be linked through the association but upon submission, it is not linked to Vn.我正在尝试为属于 Vn 的字符设置一个表单,该表单将通过关联链接,但在提交时,它未链接到 Vn。

class Character < ActiveRecord::Base
    validates :name, :presence => true
    validates :summary, :presence => true
    belongs_to :vn
end

class Vn < ActiveRecord::Base
  has_many :characters
  validates :name, presence: true
  accepts_nested_attributes_for :characters
end

Form to create a new Character创建新角色的表格

<%= simple_form_for @character do |f| %>
      <div class="col-lg-12">
        <%= f.input :summary,input_html: {style: "height:150px;"} %>
    </div>
      <div class="col-lg-12">
        <%= f.association :vn, as: :check_boxes %>
    </div>
      <%= f.button :submit , class: "btn btn-primary" %>
    <% end %>

Controllers控制器

class CharactersController < ApplicationController

    def show
        @character = Character.find(params[:id])
    end

    def new
        @character = Character.new
    end

    def create  
        @character = Character.new(char_params)  
        if @character.save

        else
            render :action=>"new"
        end

    end

    private
        def char_params
        params.require(:character).permit(:name, :summary,:voiceactor,:vn_name,vn_id: [])
    end

end

class VnsController < ApplicationController

    def show
        @vn = Vn.find(params[:id])
    end

    def new
        @vn = Vn.new
    end

    def create  
        @vn = Vn.new(vn_params)  
        if @vn.save

        else
            render :action=>"new"
        end

    end

    private
        def vn_params
    def vn_params
        params.require(:vn).permit(:name, :summary,:genre,:developer,:rating,vn_id: [])
    end

    end

end

Submission unpermitted vn_id提交未经许可的 vn_id

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"O2s6GVs77GGUMC5u3eZ9ebv/0l5u0MwP44yS8WGCQnjgwSgHfkbCmhEOUo6WKIMSMo5IfDuNYtMzyphnT/5cwQ==", "character"=>{"name"=>"2222", "voiceactor"=>"111", "summary"=>"one two tthee", "vn_id"=>"32"}, "commit"=>"Create Character"}
Unpermitted parameter: vn_id
   (0.1ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "characters" ("name", "summary", "voiceactor", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["name", "2222"], ["summary", "one two tthee"], ["voiceactor", "111"], ["created_at", "2015-10-23 10:34:00.285447"], ["updated_at", "2015-10-23 10:34:00.285447"]

不知何故,将vn_id: []改回:vn_id有效。

The problem is you're trying to permit an array for a singular association .问题是您试图允许一个数组用于单一关联


The f.association input is for your belongs_to association, so why would it allow multiple records? f.association输入用于您的belongs_to关联,那么为什么它允许多条记录?

You can even see how this works here :你甚至可以在这里看到它是如何工作的:

在此处输入图片说明

The above are methods only for has_many以上只是has_many方法

In short, they don't exist for belongs_to简而言之,它们不存在于belongs_to


Thus, when you call f.association :vn , you're populating an attribute vn_id which can then be associated in your database.因此,当您调用f.association :vn ,您正在填充一个属性vn_id ,然后可以将其关联到您的数据库中。

The only time you'd have vn_ids is if you used has_many etc.您唯一拥有vn_ids是您使用了has_many等。

This means...这意味着...

def character_params
    params.require(:character).permit(:vn_id) 
end

with

f.association :vn

... should work ... 应该管用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM