Ruby on Rails,嵌套参数permition

时间:2017-12-12 03:53:07

标签: ruby-on-rails ruby forms activerecord

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

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'

1 个答案:

答案 0 :(得分:3)

has_many课程顶部的Saloon来电应该如此复数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似乎在错误中被误导(额外的s)。如果您在代码中的某处错误地键入了模型,那将是一个问题。

  

未经许可的参数:: saloon_addresss