Rails 4 Nested Attributes with fields_for Don't Save to Database

不羁的心 提交于 2019-12-24 00:04:22

问题


I want to create records on two different tables (venue and parking) via one form using accepts_nested_attributes_for. I want a user to be able to create a new venue, and also specify the parking options available to that venue via checkboxes. When I submit the form, the record for the containing model (venue) is created, but nothing happens with the nested model (parking). When I check the response from the server, I see that I'm encountering "Unpermitted parameters: parking_attributes," although I'm not sure why.

I've watched Railscast #196 Nested Model Form, and tried the suggestions from multiple stackoverflow posts (Rails 4 nested attributes not saving, Rails 4: fields_for in fields_for, and Rails 4 - Nested models(2 levels) not saving). If anybody can help me out, I'd greatly appreciate it.

I've included the two models, the venues controller, the venues/new view, and the response from the server.

venue.rb

class Venue < ActiveRecord::Base
  has_many :parkings
  accepts_nested_attributes_for :parkings
end

parking.rb

class Parking < ActiveRecord::Base
  belongs_to :venue
end

venues_controller.rb

class VenuesController < ApplicationController
  def index
    @venues = Venue.all
  end

  def new
    @venue = Venue.new
  end

  def create
    @venue = Venue.new(venue_params)
    if @venue.save
      redirect_to @venue, flash: { success: "Venue successfully created" }
    else 
      render :new
    end
  end

  def show
    @venue = Venue.find(params[:id])
  end

  def edit
    @venue = Venue.find(params[:id])
  end

  def update
    @venue = Venue.find(params[:id])
    if @venue.update(venue_params)
      redirect_to @venue
    else
      render "edit"
    end
  end

  def destroy
    @venue = Venue.find(params[:id])
    if @venue.destroy
      redirect_to venues_path, flash: { success: "Venue successfully destroyed" }
    else
      render "show", flash: { error: "Venue was not successfully destroyed" }
    end
  end

private

  def venue_params
    params.require(:venue).permit(
      :name,:address,:city,:state,:zip,
      parking_attributes: [:id, :venue_id, :none, :street_free])
  end

end

/venues/new.haml

%h1 Add a new venue
= form_for @venue do |f|
  = f.label :name
  = f.text_field :name
  = f.label :address
  = f.text_field :address
  = f.label :city
  = f.text_field :city
  = f.label :state
  = f.text_field :state
  = f.label :zip
  = f.text_field :zip

  = f.fields_for :parkings do |p|
    = p.label :none
    = p.check_box :none
    = p.label :street_free
    = p.check_box :street_free
  = f.submit

Server response

Started POST "/venues" for 127.0.0.1 at 2014-04-29 14:02:54 -0500
Processing by VenuesController#create as HTML
  Parameters: {"utf8"=>"✓",
    "authenticity_token"=>"kMcVVwXq7f22rIGm1rQ6+QzC80ScmXrVA2IE8TGbN7w=", 
    "venue"=>{"name"=>"The Five O'Clock Lounge", 
    "address"=>"11904 Detroit Ave", 
    "city"=>"Lakewood", 
    "state"=>"OH", 
    "zip"=>"44107", 
    "parkings_attributes"=>
      {"0"=>
        {"none"=>"1",
          "street_free"=>"0"
        }
      }
    }, 
    "commit"=>"Create Venue"}
Unpermitted parameters: parkings_attributes
  (0.2ms)  BEGIN
SQL (107.0ms)  INSERT INTO "venues" (
  "address", 
  "city", 
  "created_at", 
  "name", "state", 
  "updated_at", "zip"
) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  
  [
    ["address", "11904 Detroit Ave"], 
    ["city", "Lakewood"], 
    ["created_at", Tue, 29 Apr 2014 19:02:54 UTC +00:00], 
    ["name", "The Five O'Clock Lounge"], 
    ["state", "OH"], 
    ["updated_at", Tue, 29 Apr 2014 19:02:54 UTC +00:00], 
    ["zip", 44107]
  ]
SQL (47.5ms)  INSERT INTO "parkings" (
  "created_at", 
  "updated_at", 
  "venue_id") VALUES ($1, $2, $3) RETURNING "id"  
    [
      ["created_at", Tue, 29 Apr 2014 19:02:54 UTC +00:00], 
      ["updated_at", Tue, 29 Apr 2014 19:02:54 UTC +00:00], 
      ["venue_id", 10]
    ]
  (0.6ms)  COMMIT
Redirected to http://localhost:3000/venues/10
Completed 302 Found in 165ms (ActiveRecord: 155.2ms)

UPDATE: SOLVED

Following the advice of Kirti, I was able to get past the unpermitted parameters error.


回答1:


Update venue_params method as below:

 def venue_params
   params.require(:venue).permit(
      :name,:address,:city,:state,:zip,
      parkings_attributes: [:id, :venue_id, :none, :street_free])
 end

Notice parkings_attributes(plural parkings) and not parking_attributes(singular parking).

As you have 1-M relationship between Venue and Parking model you would receive parkings_attributes(plural parkings) in params hash BUT in your current code for venue_params you whitelisted parking_attributes(singular parking). This is causing the warning Unpermitted parameters: parkings_attributes



来源:https://stackoverflow.com/questions/23373668/rails-4-nested-attributes-with-fields-for-dont-save-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!