7

我上的應用程序工作,這 有1個模塊具有多對多的關係,它的做工精細, 我可以創建我的「單位」與嵌套形式,但是當我在編輯狀態,當我到了1個問題我點擊提交按鈕它給我一個錯誤。即使在編輯模式下,rails的嵌套形式總是「POST」?造成路由錯誤

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

我認爲操作應該是補丁而不是職位?我檢查在瀏覽器中 生成html,我可以看到有一個隱藏字段 名爲「_method」與「補丁」的價值。

我生成該模塊與支架

單元模型

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 

出租週期模型

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

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

end 

單元出租週期模型

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


end 

我不知道爲什麼,我的表單在創建模式下工作正常,但不能在編輯模式下工作。我試圖刪除我的嵌套表單,我的更新行爲工作正常,但是如果我把我的嵌套表單,它會拋出路由錯誤。

,使我的觀點更容易閱讀,這是我的看法

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

控制器

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 

路由文件

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 

耙路輸出的簡單版本:

 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 

順便說一句我也試圖窩我的資源是這樣的:

resources :units do 
    resources :unitrentperiods 
    end 

仍然沒有運氣。

+0

你可以在這裏更新你的控制器代碼 –

+0

@bkchovatiya更新與控制器。 – NomNomNom

+0

我看不到'@ unit'在控制器的'edit'動作中被實例化,而你正在使用它的形式 – RAJ

回答

1

我找到了解決方法,萬一別人面臨着同樣的問題。在我原來的觀點,我已經把這個在我的嵌套形式:

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

%> 

爲了解決這個問題,我刪除了name屬性,並在路由文件還刪除了嵌套的資源。然後,我改變了我的選擇元素在嵌套的形式爲:

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

%> 

和一切工作正常。

我還是不明白不過,爲什麼name屬性將導致路由錯誤。