i trying use fields_for helper method on project working on. original form works , saves fine. new attributes not save , nomethoderror , undefined method. missing?!
here listing model:
class listing < activerecord::base has_one :listing_commerical_attribute accepts_nested_attributes_for :listing_commerical_attribute, :allow_destroy => true
here listing_commercial_attribute model:
class listingcommercialattribute < activerecord::base belongs_to :listing accepts_nested_attributes_for :listing end
here controller:
def new @listing.build_listing_commercial_attribute respond_to |format| format.html # new.html.erb format.json { render json: @listing } end end private def commercial_params params.require(:commerical_listing_attribute) .permit(:gas_pipe_size, :amperage, :basement_ceiling_height, :ceiling_height, :door_size, :zoning, :previous_use, :community_board, :delivery_date, :key_money, :security_deposit, :price_per_sq_ft, :did_size) end
here _form.html.erb:
<h2 class="text-center">commercial</h2> <%= f.fields_for :listing_commerical_attributes |ff| %> <div class="field"> <%= ff.label :gas_pipe_size, "gas pipe size", class: "general-text-label" %> <%= ff.number_field :gas_pipe_size, class: "general-text-field" %> </div> <div class="field"> <%= ff.label :amperage, "amperage", class: "general-text-label" %> <%= ff.number_field :amperage, class: "general-text-field" %> </div> <div class="field"> <%= ff.label :ceiling_height, "ceiling height", class: "general-text-label" %> <%= ff.number_field :ceiling_height, class: "general-text-field" %> </div> <div class="field"> <%= ff.label :basement_ceiling_height, "basement ceiling height", class: "general-text-label" %> <%= ff.number_field :basement_ceiling_height, class: "general-text-field" %> </div> <div class="field"> <%= ff.label :door_size, "door size", class: "general-text-label" %> <%= ff.number_field :door_size, class: "general-text-field" %> </div> <div class="field"> <%= ff.label :zoning, "zoning", class: "general-text-label" %> <%= ff.text_field :zoning, class: "general-text-field" %> </div> <div class="field"> <label for="tenant_improvements" class="general-text-label">tenant improvements <small>(if applicable)</small></label> <%= ff.text_area :tenant_improvements, :rows => "4", class: "general-text-area" %> </div> <div class="field"> <label for="previous_use" class="general-text-label">previous use <small>(if applicable)</small></label> <%= ff.text_area :previous_use, :rows => "4", class: "general-text-area" %> </div> <div class= "field"> <%= ff.label :community_board, "community board", class: "general-text-label" %> <%= ff.text_field :community_board, class: "general-text-field" %> </div> <div class="field"> <%= ff.label :delivery_date, "delivery date", class: "general-text-label" %> <div class="input-group"> <span class="input-group-addon"><i class="nklyn-icon-calendar"></i></span> <%= ff.text_field :delivery_date, :class => "datepicker general-text-field" %> </div> <div class="field"> <%= ff.label :key_money, "key money", class: "general-text-label" %> <div class="input-group"> <span class="input-group-addon"><i class="nklyn-icon-money-bills"></i></span> <%= f.text_field :key_money, class: "general-text-field", value: number_with_precision(f.object.price, delimiter: ',', precision: 0) %> </div> </div> <div class="field"> <%= ff.label :security_deposit, "security deposit", class: "general-text-label" %> <div class="input-group"> <span class="input-group-addon"><i class="nklyn-icon-money-bills"></i></span> <%= f.text_field :security_deposit, class: "general-text-field", value: number_with_precision(f.object.price, delimiter: ',', precision: 0) %> </div> </div> <div class="field"> <%= ff.label :price_per_sq_ft, "price per sq ft", class: "general-text-label" %> <div class="input-group"> <span class="input-group-addon"><i class="nklyn-icon-money-bills"></i></span> <%= f.text_field :price_per_sq_ft, class: "general-text-field", value: number_with_precision(f.object.price, delimiter: ',', precision: 0) %> </div> </div> <div class="field"> <%= ff.label :did_size, "drive in doors size", class: "general-text-label" %> <%= ff.number_field :did_size, class: "general-text-field" %> </div> <% end %>
update
i made change listingcommercialattribute model , removed accepts nested attributes for.
i changed f.fields_for singular instead of plural.
i added in nested attributes after parent (see below)
def listing_params params.require(:listing) .permit(:access, :address, :apartment, :cats_ok, :cross_streets, :dogs_ok, :latitude, :longitude, :amenities, :date_available, :bathrooms, :bedrooms, :description, :fee, :exclusive, :featured, :rental, :residential, :landlord_contact, :listing_agent_id, :sales_agent_id, :neighborhood_id, :pets, :photo, :photo_tag, :primaryphoto, :price, :square_feet, :station, :status, :subway_line, :term, :title, :utilities, :move_in_cost, :owner_pays, :private, :office_id, :full_address, :zip, :convertible, :landlord_llc, :pinned, :image, listing_commercial_attribute_attributes: [ :gas_pipe_size, :amperage, :basement_ceiling_height, :ceiling_height, :door_size, :zoning, :previous_use, :community_board, :delivery_date, :key_money, :security_deposit, :price_per_sq_ft, :did_size]) end
here new controller actions:
def edit @listing.attributes = listing_params end def create @listing.attributes = listing_params respond_to |format| if @listing.save format.html { redirect_to @listing, notice: 'listing created.' } format.json { render json: @listing, status: :created, location: @listing } else format.html { render action: "new", notice: "correct mistakes below create new listing" } format.json { render json: @listing.errors, status: :unprocessable_entity } end end end
but getting nomethoderror in listings#show error. created partial commercial attributes. shouldn't included in strong params, or totally misunderstanding that?!
here partial:
gas pipe size: <%= listing_commercial_attributes.gas_pipe_size(@listing) %> amperage: <%= listing_commercial_attribute.amperage(@listing) %> basement ceiling height: <%= listing_commercial_attribute.basement_celing_height(@listing) %> ceiling height: <%= listing_commercial_attribute.ceiling_height(@listing) %> door size: <%= listing_commercial_attribute.door_size(@listing) %> zoning: <%= listing_commercial_attribute.zoning(@listing) %> build suit: <%= listing_commercial_attribute.build_to_suit(@listing) %> previous use: <%= listing_commercial_attribute.previous_use(@listing) %> community board: <%= listing_commercial_attribute.community_board(@listing) %> delivery date: <%= listing_commercial_attribute.delivery_date(@listing) %> key money: <%= listing_commercial_attribute.key_money(@listing) %>
update #2
i changed singular.
here complete error.
nameerror in listings#show
showing /users/code/app/views/listings/_commercial_attributes.html.erb line #1 raised:
undefined local variable or method `listing_commercial_attribute' #<#:0x007f86606f6a10> did mean? listing_collection_url
- gas pipe size: <%= listing_commercial_attribute.gas_pipe_size(@listing) %>
- amperage: <%= listing_commercial_attribute.amperage(@listing) %>
- basement ceiling height: <%= listing_commercial_attribute.basement_celing_height(@listing) %>
- ceiling height: <%= listing_commercial_attribute.ceiling_height(@listing) %>
- door size: <%= listing_commercial_attribute.door_size(@listing) %>
- zoning: <%= listing_commercial_attribute.zoning(@listing) %>
trace of template inclusion: app/views/listings/_listing_content_area.html.erb, app/views/listings/show.html.erb
update #3
def show @my_listing_collections = listingcollection.with_agent(current_agent).order("created_at desc") @listing_commercial_attributes = listingcommercialattribute.find(params[:id]) @regions = region.order(name: :asc) @listing = listing.includes(:photos, :likes, :interested_agents).find(params[:id]) if @listing.private && cannot?(:create, listing) redirect_to listings_path, notice: 'this listing no longer available' else agent = agent.where(id: params[:agent_id]).first @page = listings::showview.new(@listing, agent) respond_to |format| format.html end end end
i keep getting error:
activerecord::recordnotfound in listingscontroller#show
couldn't find listingcommercialattribute 'id'=5755
it searching commercial attribute id of 5755, listing id. i'm not sure pass in there...
- do not define
accepts_nested_attributes_for
on both models. on parent model. otherwise you'll run circular dependency issues. in case parent model looks it's listing, removeaccepts_nested_attributes_for :listing
listingcommercialattribute. - the first argument
f.fields_for
should name of association , yours off. havehas_one : listing_commerical_attribute
wantf.fields_for : listing_commerical_attribute
. - the strong parameters should require parent object first , include nested objects second. also, must append
_attributes
end of nested attribute name.
so, 3:
def listing_params params.require(:listing) .permit(:id, # ... listing_commercial_attribute_attributes: [ # note: _attributes :gas_pipe_size, # ... ]) end
- in create/edit actions, sure set params strong parameters method:
@listing.attributes = listing_params
.
read more in docs on accepts_nested_attributes_for
, strong parameters.