Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails nested form always "POST" even on edit mode? caused route error

i work on an application which have 1 module with many to many relationship, its working fine, i can create my "unit" with nested form, but i got 1 problem when i am on edit state, when i hit submit button it throw me an error.

No route matches [POST] "/units/27"

i thought the action should be patch instead of post?? i inspect the generated html in browsers, and i can see there is a hidden field named "_method" with "patch" value.

i generated this module with scaffold

unit model

class Unit < ActiveRecord::Base
  enum unitview: [ :city, :pool, :sea ]
  enum furnish: [ "furnish", "non furnish", "semi furnish" ]
  enum unitstatus: [ :draft, :publish, :unpublish, :waiting ]

  belongs_to :apt
  belongs_to :tower
  belongs_to :room_type
  belongs_to :template
  belongs_to :floorplan
  belongs_to :unitplan

  validates :apt_id, presence: true
  validates :tower_id, presence: true
  validates :unitno, presence: true
  validates :room_type_id, presence: true
  validates :unitsize, presence: true
  validates :unitfloor, presence: true

  has_many :unitrentperiods, inverse_of: :unit
  has_many :rentperiods, :through => :unitrentperiods
  accepts_nested_attributes_for :unitrentperiods

end

rent period model

class Rentperiod < ActiveRecord::Base
    validates :rentmonth, presence: true, numericality: { only_integer: true }

    has_many :unitrentperiods, inverse_of: :rentperiod
    has_many :units, :through => :unitrentperiods

end

unit rent period model

class Unitrentperiod < ActiveRecord::Base
  belongs_to :unit ,inverse_of: :unitrentperiods
  belongs_to :rentperiod ,inverse_of: :unitrentperiods


end

i don't know why, my form working fine on create mode, but not on edit mode.

FYI, i tried to remove my nested form, and my update action working fine, but if i put my nested form, it will throw route error.

to make my view easier to read, here is the simple version of my view

<%= form_for @unit, html: {class: "form-horizontal"} do |f| %>

<div class="form-group row">
 <%= f.label 'Rent price', class: 'col-md-4 form-control-label' %>
 <div class="col-md-8">
  <table>
   <%= f.fields_for :unitrentperiods do |elem| %>
   <tr>                                           
     <td>
       <%= elem.text_field :rentprice, class: 'col-md-4 form-control', name: "unit[unitrentperiods_attributes][][rentprice]" %> IDR
     </td>
     <td>
       <%= elem.select :rentperiod_id, @rentperiods.map { |u| [u.rentmonth, u.id] }, { include_blank: true }, {:class => 'form-control', :name => 'unit[unitrentperiods_attributes][][rentperiod_id]'} %>
      Month
     </td>
   </tr>
<% end -%>
  </table>
 </div>
</div>

<div class="form-group row">
  <%= f.label 'Unit status', class: 'col-md-4 form-control-label' %>
  <div class="col-md-8">
    <%= f.select :unitstatus, options_for_select(@unit_statuses.collect { |s| [s[0].humanize, s[0]] }, selected: @unit.unitstatus), {} , class: "form-control" %>
  </div>
</div>  

<button type="submit" class="btn btn-sm btn-primary"><i class="fa fa-dot-circle-o"></i> Submit</button>      
<% end %>

controller

class UnitsController < ApplicationController
  before_action :set_unit, only: [:show, :edit, :update, :destroy]

  # GET /units
  # GET /units.json
  def index
    @units = Unit.paginate(page: params[:page])
  end

  # GET /units/1
  # GET /units/1.json
  def show
  end

  def gettowers

    tower = Tower.where(:apt_id => params[:apt_id])

    @towers = tower.map{|a| [a.tower_name, a.id]}.insert(0, "Select a Tower")
  end

  def getfloorplans
    floorplans = Floorplan.where(:apt_id => params[:apt_id]).select("id, floorplanphoto")
    @imgs = floorplans
  end

  def getunitplans
    unitplans = Unitplan.where(:apt_id => params[:apt_id]).select("id, unitplanphoto")

    @imgs = unitplans
  end

  def preparedata
    #master tables
    @apts = Apt.all
    @towers = Tower.all
    @roomtypes = RoomType.all
    @rentperiods = Rentperiod.all

    array = []
    Template.all.each do |t|
       hash = { :type => t.room_type.room_type_desc, :id => t.id }
       array.push(hash)
    end

    @templates = array

    #enum from model
    @unit_views = Unit.unitviews
    @furnish_types = Unit.furnishes
    @unit_statuses = Unit.unitstatuses
  end

  # GET /units/new
  def new
      @unit = Unit.new
      preparedata

      @rentperiods.each do |r| 
         @unit.unitrentperiods.build
      end
  end

  # GET /units/1/edit
  def edit
    preparedata
  end

  # POST /units
  # POST /units.json
  def create
    @unit = Unit.new(unit_params)

    respond_to do |format|
      if @unit.save
        format.html { redirect_to unit_url, notice: 'Unit was successfully created.' }
        format.json { render :show, status: :created, location: @unit }
      else
        format.html { render :new }
        format.json { render json: @unit.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /units/1
  # PATCH/PUT /units/1.json
  def update
    respond_to do |format|
      if @unit.update(unit_params)
        format.html { redirect_to unit_url, notice: 'Unit was successfully updated.' }
        format.json { render :show, status: :ok, location: @unit }
      else
        format.html { render :edit }
        format.json { render json: @unit.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /units/1
  # DELETE /units/1.json
  def destroy
    @unit.destroy
    respond_to do |format|
      format.html { redirect_to units_url, notice: 'Unit was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_unit
      @unit = Unit.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def unit_params
      params.require(:unit).permit(:apt_id, :tower_id, :unitno, :unitsize, :unitview, 
                                   :room_type_id, :unitfloor, 
                                   :unitdesc, :isforsell, :sellprice, :isforrent, :furnish, :totalbedroom, 
                                   :unitstatus, :template_id, :floorplan_id, :unitplan_id,
                                   unitrentperiods_attributes: [ :rentprice, :rentperiod_id ])
    end
end

route file

Rails.application.routes.draw do

  resources :rentperiods
  resources :pois
  resources :towers
  resources :apts
  resources :areas
  resources :regions
  resources :contacts
  resources :point_of_interest_types
  resources :facilities
  resources :room_types
  resources :developers
  resources :states
  resources :countries
  resources :unitplans, only: [:create, :destroy, :getunitplans]
  resources :floorplans, only: [:create, :destroy, :getfloorplans]
  resources :aptphotos, only: [:create, :destroy, :getphotos]

  resources :templates
  resources :templatephotos, only: [:create, :destroy, :getphotos]

  get "units/gettowers"
  get "units/getfloorplans"
  get "units/getunitplans"
  resources :units

  post "templatephotos/create"
  post "templatephotos/destroy"
  get "templatephotos/getphotos"

  post "unitplans/create"
  post "unitplans/destroy"
  get "unitplans/getunitplans"

  post "floorplans/create"
  post "floorplans/destroy"
  get "floorplans/getfloorplans"

  post "aptphotos/create"
  post "aptphotos/destroy"
  get "aptphotos/getphotos"




  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  root 'countries#index'


end

rake routes output :

      pois#edit
                        poi GET    /pois/:id(.:format)
      pois#show
                            PATCH  /pois/:id(.:format)
      pois#update
                            PUT    /pois/:id(.:format)
      pois#update
                            DELETE /pois/:id(.:format)
      pois#destroy
                     towers GET    /towers(.:format)
      towers#index
                            POST   /towers(.:format)
      towers#create
                  new_tower GET    /towers/new(.:format)
      towers#new
                 edit_tower GET    /towers/:id/edit(.:format)
      towers#edit
                      tower GET    /towers/:id(.:format)
      towers#show
                            PATCH  /towers/:id(.:format)
      towers#update
                            PUT    /towers/:id(.:format)
      towers#update
                            DELETE /towers/:id(.:format)
      towers#destroy
                       apts GET    /apts(.:format)
      apts#index
                            POST   /apts(.:format)
      apts#create
                    new_apt GET    /apts/new(.:format)
      apts#new
                   edit_apt GET    /apts/:id/edit(.:format)
      apts#edit
                        apt GET    /apts/:id(.:format)
      apts#show
                            PATCH  /apts/:id(.:format)
      apts#update
                            PUT    /apts/:id(.:format)
      apts#update
                            DELETE /apts/:id(.:format)
      apts#destroy
                      areas GET    /areas(.:format)
      areas#index
                            POST   /areas(.:format)
      areas#create
                   new_area GET    /areas/new(.:format)
      areas#new
                  edit_area GET    /areas/:id/edit(.:format)
      areas#edit
                       area GET    /areas/:id(.:format)
      areas#show
                            PATCH  /areas/:id(.:format)
      areas#update
                            PUT    /areas/:id(.:format)
      areas#update
                            DELETE /areas/:id(.:format)
      areas#destroy
                    regions GET    /regions(.:format)
      regions#index
                            POST   /regions(.:format)
      regions#create
                 new_region GET    /regions/new(.:format)
      regions#new
                edit_region GET    /regions/:id/edit(.:format)
      regions#edit
                     region GET    /regions/:id(.:format)
      regions#show
                            PATCH  /regions/:id(.:format)
      regions#update
                            PUT    /regions/:id(.:format)
      regions#update
                            DELETE /regions/:id(.:format)
      regions#destroy
                   contacts GET    /contacts(.:format)
      contacts#index
                            POST   /contacts(.:format)
      contacts#create
                new_contact GET    /contacts/new(.:format)
      contacts#new
               edit_contact GET    /contacts/:id/edit(.:format)
      contacts#edit
                    contact GET    /contacts/:id(.:format)
      contacts#show
                            PATCH  /contacts/:id(.:format)
      contacts#update
                            PUT    /contacts/:id(.:format)
      contacts#update
                            DELETE /contacts/:id(.:format)
      contacts#destroy
    point_of_interest_types GET    /point_of_interest_types(.:format)
      point_of_interest_types#index
                            POST   /point_of_interest_types(.:format)
      point_of_interest_types#create
 new_point_of_interest_type GET    /point_of_interest_types/new(.:format)
      point_of_interest_types#new
edit_point_of_interest_type GET    /point_of_interest_types/:id/edit(.:forma
      point_of_interest_types#edit
     point_of_interest_type GET    /point_of_interest_types/:id(.:format)
      point_of_interest_types#show
                            PATCH  /point_of_interest_types/:id(.:format)
      point_of_interest_types#update
                            PUT    /point_of_interest_types/:id(.:format)
      point_of_interest_types#update
                            DELETE /point_of_interest_types/:id(.:format)
      point_of_interest_types#destroy
                 facilities GET    /facilities(.:format)
      facilities#index
                            POST   /facilities(.:format)
      facilities#create
               new_facility GET    /facilities/new(.:format)
      facilities#new
              edit_facility GET    /facilities/:id/edit(.:format)
      facilities#edit
                   facility GET    /facilities/:id(.:format)
      facilities#show
                            PATCH  /facilities/:id(.:format)
      facilities#update
                            PUT    /facilities/:id(.:format)
      facilities#update
                            DELETE /facilities/:id(.:format)
      facilities#destroy
                 room_types GET    /room_types(.:format)
      room_types#index
                            POST   /room_types(.:format)
      room_types#create
              new_room_type GET    /room_types/new(.:format)
      room_types#new
             edit_room_type GET    /room_types/:id/edit(.:format)
      room_types#edit
                  room_type GET    /room_types/:id(.:format)
      room_types#show
                            PATCH  /room_types/:id(.:format)
      room_types#update
                            PUT    /room_types/:id(.:format)
      room_types#update
                            DELETE /room_types/:id(.:format)
      room_types#destroy
                 developers GET    /developers(.:format)
      developers#index
                            POST   /developers(.:format)
      developers#create
              new_developer GET    /developers/new(.:format)
      developers#new
             edit_developer GET    /developers/:id/edit(.:format)
      developers#edit
                  developer GET    /developers/:id(.:format)
      developers#show
                            PATCH  /developers/:id(.:format)
      developers#update
                            PUT    /developers/:id(.:format)
      developers#update
                            DELETE /developers/:id(.:format)
      developers#destroy
                     states GET    /states(.:format)
      states#index
                            POST   /states(.:format)
      states#create
                  new_state GET    /states/new(.:format)
      states#new
                 edit_state GET    /states/:id/edit(.:format)
      states#edit
                      state GET    /states/:id(.:format)
      states#show
                            PATCH  /states/:id(.:format)
      states#update
                            PUT    /states/:id(.:format)
      states#update
                            DELETE /states/:id(.:format)
      states#destroy
                  countries GET    /countries(.:format)
      countries#index
                            POST   /countries(.:format)
      countries#create
                new_country GET    /countries/new(.:format)
      countries#new
               edit_country GET    /countries/:id/edit(.:format)
      countries#edit
                    country GET    /countries/:id(.:format)
      countries#show
                            PATCH  /countries/:id(.:format)
      countries#update
                            PUT    /countries/:id(.:format)
      countries#update
                            DELETE /countries/:id(.:format)
      countries#destroy
                  unitplans POST   /unitplans(.:format)
      unitplans#create
                   unitplan DELETE /unitplans/:id(.:format)
      unitplans#destroy
                 floorplans POST   /floorplans(.:format)
      floorplans#create
                  floorplan DELETE /floorplans/:id(.:format)
      floorplans#destroy
                  aptphotos POST   /aptphotos(.:format)
      aptphotos#create
                   aptphoto DELETE /aptphotos/:id(.:format)
      aptphotos#destroy
                  templates GET    /templates(.:format)
      templates#index
                            POST   /templates(.:format)
      templates#create
               new_template GET    /templates/new(.:format)
      templates#new
              edit_template GET    /templates/:id/edit(.:format)
      templates#edit
                   template GET    /templates/:id(.:format)
      templates#show
                            PATCH  /templates/:id(.:format)
      templates#update
                            PUT    /templates/:id(.:format)
      templates#update
                            DELETE /templates/:id(.:format)
      templates#destroy
             templatephotos POST   /templatephotos(.:format)
      templatephotos#create
              templatephoto DELETE /templatephotos/:id(.:format)
      templatephotos#destroy
            units_gettowers GET    /units/gettowers(.:format)
      units#gettowers
        units_getfloorplans GET    /units/getfloorplans(.:format)
      units#getfloorplans
         units_getunitplans GET    /units/getunitplans(.:format)
      units#getunitplans
       unit_unitrentperiods GET    /units/:unit_id/unitrentperiods(.:format)
      unitrentperiods#index
                            POST   /units/:unit_id/unitrentperiods(.:format)
      unitrentperiods#create
    new_unit_unitrentperiod GET    /units/:unit_id/unitrentperiods/new(.:for
      unitrentperiods#new
   edit_unit_unitrentperiod GET    /units/:unit_id/unitrentperiods/:id/edit(
rmat) unitrentperiods#edit
        unit_unitrentperiod GET    /units/:unit_id/unitrentperiods/:id(.:for
      unitrentperiods#show
                            PATCH  /units/:unit_id/unitrentperiods/:id(.:for
      unitrentperiods#update
                            PUT    /units/:unit_id/unitrentperiods/:id(.:for
      unitrentperiods#update
                            DELETE /units/:unit_id/unitrentperiods/:id(.:for
      unitrentperiods#destroy
                      units GET    /units(.:format)
      units#index
                            POST   /units(.:format)
      units#create
                   new_unit GET    /units/new(.:format)
      units#new
                  edit_unit GET    /units/:id/edit(.:format)
      units#edit
                       unit GET    /units/:id(.:format)
      units#show
                            PATCH  /units/:id(.:format)
      units#update
                            PUT    /units/:id(.:format)
      units#update
                            DELETE /units/:id(.:format)
      units#destroy
      templatephotos_create POST   /templatephotos/create(.:format)
      templatephotos#create
     templatephotos_destroy POST   /templatephotos/destroy(.:format)
      templatephotos#destroy
   templatephotos_getphotos GET    /templatephotos/getphotos(.:format)
      templatephotos#getphotos
           unitplans_create POST   /unitplans/create(.:format)
      unitplans#create
          unitplans_destroy POST   /unitplans/destroy(.:format)
      unitplans#destroy
     unitplans_getunitplans GET    /unitplans/getunitplans(.:format)
      unitplans#getunitplans
          floorplans_create POST   /floorplans/create(.:format)
      floorplans#create
         floorplans_destroy POST   /floorplans/destroy(.:format)
      floorplans#destroy
   floorplans_getfloorplans GET    /floorplans/getfloorplans(.:format)
      floorplans#getfloorplans
           aptphotos_create POST   /aptphotos/create(.:format)
      aptphotos#create
          aptphotos_destroy POST   /aptphotos/destroy(.:format)
      aptphotos#destroy
        aptphotos_getphotos GET    /aptphotos/getphotos(.:format)
      aptphotos#getphotos
                       root GET    /
      countries#index

btw i also tried to nest my resource like this :

  resources :units do
    resources :unitrentperiods
  end

still no luck.

like image 937
NomNomNom Avatar asked Sep 02 '16 10:09

NomNomNom


1 Answers

I found a solution, in case someone else faces the same issue. In my original view, I had placed this in my nested form:

 <%= elem.select :rentperiod_id,
                  @rentperiods.map { |u| [u.rentmonth, u.id] },
                  { include_blank: true },
                  {:class => 'form-control', :name => 'unit[unitrentperiods_attributes][][rentperiod_id]'} 

 %> 

To fix the issue, I removed the name attribute and also removed the nested resources in route file. I then changed my select element in the nested form to this:

 <%= elem.select :rentperiod_id,
                  @rentperiods.map { |u| [u.rentmonth, u.id] },
                  { include_blank: true },
                  {:class => 'form-control'} 

 %> 

and everything works fine.

I still don't understand though, why the name attribute would cause a routing error.

like image 174
NomNomNom Avatar answered Oct 06 '22 06:10

NomNomNom