簡體   English   中英

如何解決帶條件流的動作控制器錯誤?

[英]How do I Fix Action Controller Error w/ conditional flow?

我試圖在我的應用程序中創建一個屬性,該屬性具有與該屬性相關聯的畫廊的鏈接。 問題是,當我嘗試創建第一個屬性時遇到錯誤,因為此鏈接位於屬性表單底部的<%= link_to "Add Pictures", new_property_gallery_path(@property) %>

我想基本上創建一個屬性,然后有一個鏈接來創建與該屬性相關聯的圖庫。 那是我的目標

這是我得到的實際錯誤輸出,表明我缺少屬性參數:

在此處輸入圖片說明

這是我的路線文件

Rails.application.routes.draw do

  devise_for :users, :controllers => { registrations: 'registrations' }

  resources :pictures

  resources :properties do
    resources :galleries
  end

  resources :spaces do
    collection do
      get 'search'
    end
    resources :galleries
    resources :contracts
    resources :reviews
  end

  root 'spaces#index'
  get 'payment' => "contracts#payment"
  get 'dashboard' => "spaces#dashboard"

  get 'about_us' => "marketing_pages#about_us"
  get 'team' => "marketing_pages#team"
  get 'how_it_works' => "marketing_pages#how_it_works"

#new code trying to set up messaging
  resources :users

  get "/messages" => redirect("/conversations")
  resources :messages do
  member do
    post :new
  end
end
resources :conversations do
  member do
    post :reply
    post :trash
    post :untrash
  end
 collection do
    get :trashbin
    post :empty_trash
 end
end





  # 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 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end

這是properties_controller.rb

class PropertiesController < ApplicationController
  before_action :authenticate_user!, only: [:index, :new, :edit, :create, :update, :destroy]
  before_action :set_property, only: [:show, :edit, :update, :destroy]

  # GET /properties
  # GET /properties.json
  def index
    @properties = Property.all
  end

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

  # GET /properties/new
  def new
    @property = Property.new
  end

  # GET /properties/1/edit
  def edit
  end

  # POST /properties
  # POST /properties.json
  def create
    @property = Property.new(property_params)
    @property.landlord_id = current_user.id

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

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

  # DELETE /properties/1
  # DELETE /properties/1.json
  def destroy
    @property.destroy
    respond_to do |format|
      format.html { redirect_to properties_url, notice: 'Property was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def property_params
      params.require(:property).permit(:name, :description, :neighborhood, :street, :city, :state, :country, :company, :price, :amenities, :status, :building_owner, :building_class, :images)
    end
end

問題是,當我嘗試創建第一個屬性時遇到錯誤,因為此鏈接位於屬性表單的底部<%= link_to "Add Pictures", new_property_gallery_path(@property) %>

您有nested routes ,其中畫廊嵌套在屬性中 如果使用此鏈接<%= link_to "Add Pictures", new_property_gallery_path(@property) %>創建屬性 ,則該property_id將不起作用,因為應使用該屬性關聯的屬性創建新的圖庫 ,並且需要有property_id這不應該是

如果要首先創建屬性,請使用以下鏈接。

<%= link_to "Add Property", new_property_path %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM