繁体   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