Saya mencoba menyimpan di database saya beberapa informasi dalam suatu model, yang memiliki model lain, seperti:

class Saloon < ApplicationRecord    
   has_many :saloon_address

   accepts_nested_attributes_for :saloon_address
end

Dan memiliki alamat:

class SaloonAddress < ApplicationRecord
    belongs_to :saloon
end

Dan ini pandangan saya:

<%= 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 %>

Dan pengontrol saya:

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

Tetapi ketika saya mencoba untuk menyimpan, saya selalu mendapatkan pesan:

Parameter tidak diizinkan: :saloon_addresss

Setiap ideias dari apa yang terjadi?

Saya telah mencari dan mencoba begitu banyak cara berbeda untuk mengizinkan, tetapi saya tidak dapat mencapainya.

Perbarui

Saya sudah melakukan seperti jawaban pertama:

class Saloon < ApplicationRecord    
    has_many :saloon_addresses

    accepts_nested_attributes_for :saloon_addresses
end

Tapi sekarang saya mendapatkan kesalahan ini:

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'
0
user827322 12 Desember 2017, 06:53

1 menjawab

Jawaban Terbaik

Panggilan has_many Anda di bagian atas kelas Saloon Anda harus berbentuk jamak saloon_address seperti:

class Saloon < ApplicationRecord    
   has_many :saloon_addresses

   accepts_nested_attributes_for :saloon_address
end

Demikian juga, atribut Anda di params harus jamak:

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

Terakhir saya tidak tahu apakah ini hanya kesalahan pengetikan ketika Anda menulis pertanyaan, tetapi saloon_address tampaknya salah eja dalam kesalahan (tambahan). Itu akan menjadi masalah jika Anda salah mengetik model seperti itu di suatu tempat dalam kode.

Parameter tidak diizinkan: :saloon_addresss

3
inveterateliterate 12 Desember 2017, 04:14