【问题标题】:Ruby on Rails, nested parameters permitionRuby on Rails,嵌套参数权限
【发布时间】:2017-12-12 03:53:07
【问题描述】:

我正在尝试在我的数据库中保存一个模型中的一些信息,这些信息有另一个模型,如下所示:

class Saloon < ApplicationRecord    
   has_many :saloon_address

   accepts_nested_attributes_for :saloon_address
end

它有地址:

class SaloonAddress < ApplicationRecord
    belongs_to :saloon
end

这是我的观点:

<%= form_with scope: :saloon, local: true, :url => "/saloon" do |form| %>
    <p>
        <%= form.label :name %><br>
        <%= form.text_field :name %>
    </p>

    <p>
        <%= form.label :specialty %><br>
        <%= form.text_field :specialty %>
    </p>

    <p>
        <%= form.label :services %><br>
        <%= form.text_field :services %>
    </p>

    <p>
        <%= form.label :telephone %><br>
        <%= form.text_field :telephone %>
    </p>

    <p>
        <%= form.label :online %><br>
        <%= form.check_box :online %>
    </p>

    <%= form.fields_for :saloon_address do |address| %>
        <p>
            <%= address.label :zip_code %><br>
            <%= address.text_field :zip_code %>
        </p>
        <p>
            <%= address.label :street %><br>
            <%= address.text_field :street %>
        </p>
        <p>
            <%= address.label :complement %><br>
            <%= address.text_field :complement %>
        </p>
        <p>
            <%= address.label :number %><br>
            <%= address.text_field :number %>
        </p>
        <p>
            <%= address.label :city %><br>
            <%= address.text_field :city %>
        </p>
        <p>
            <%= address.label :state %><br>
            <%= address.text_field :state %>
        </p>
        <p>
            <%= address.label :zip_code %><br>
            <%= address.text_field :zip_code %>
        </p>
    <% end %>

    <p>
        <%= form.submit %>
    </p>
 <% end %>

和我的控制器:

class SaloonController < ApplicationController
    def new
    end

    def show
        @saloon = Saloon.find params[:id]
    end

    def index
        @saloons = Saloon.all
    end

    def create
        @saloon = Saloon.create(saloon_params)
        redirect_to @saloon
    end

    private 

    def saloon_params
        params.require(:saloon).permit(:name, :specialty, :online, :services, :average_price, :telephone,
            saloon_address_attributes: [:saloon_id, :street, :complement, :number, :city, :state, :zip_code, :latitude, :longitude])
    end
end

但是当我尝试保存时,我总是收到消息:

不允许的参数::saloon_addresss

对正在发生的事情有什么想法吗?

我已经搜索并尝试了许多不同的许可方式,但我无法实现。

更新

我已经完成了第一个答案:

class Saloon < ApplicationRecord    
    has_many :saloon_addresses

    accepts_nested_attributes_for :saloon_addresses
end

但是现在我收到了这个错误:

Started POST "/saloon" for 127.0.0.1 at 2017-12-13 00:21:25 -0200
Processing by SaloonController#create as HTML
Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"uF10p+ZHFiuard/CPxlt/yDgWhfzg2ttxdAWB81JTgLb+rC3ZTY+sNAgOWG3HRE8VKzHYP09QjkrL4ZMxQ3FFw==", "saloon"=>{"name"=>"asdjha", "specialty"=>"dsakhadksj", "services"=>"sdakha", "telephone"=>"21312312", "online"=>"1", "saloon_addresses"=>{"zip_code"=>"213213", "street"=>"sadasjdsa", "complement"=>"dsakhjdkasjh", "number"=>"23", "city"=>"sadjh", "state"=>"sadhj"}}, "commit"=>"Save Saloon"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)



ActiveRecord::AssociationTypeMismatch (SaloonAddress(#70246611463840) expected, got ["street", "sadasjdsa"] which is an instance of Array(#70246530918320)):

app/controllers/saloon_controller.rb:14:in `create'

【问题讨论】:

    标签: ruby-on-rails ruby forms activerecord


    【解决方案1】:

    你在Saloon 类顶部的has_many 调用应该是saloon_address 的复数形式,如下所示:

    class Saloon < ApplicationRecord    
       has_many :saloon_addresses
    
       accepts_nested_attributes_for :saloon_address
    end
    

    同样,参数中的属性应该是复数的:

    def saloon_params
      params.require(:saloon).permit(:name, :specialty, :online, :services, :average_price, :telephone, saloon_addresses_attributes: [:saloon_id, :street, :complement, :number, :city, :state, :zip_code, :latitude, :longitude])
    end
    

    最后,我不知道这是否只是您写问题时的输入错误,但saloon_address 似乎在错误中拼写错误(额外)。如果您在代码中的某处错误地键入了这样的模型,那将是一个问题。

    不允许的参数::saloon_addresss

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 2011-05-03
      • 1970-01-01
      相关资源
      最近更新 更多