如何使用条件流修复操作控制器错误?

时间:2015-07-21 15:45:22

标签: ruby-on-rails

我正在尝试在我的应用程序中创建一个属性,该属性具有指向与该属性关联的库的链接。问题是当我尝试创建第一个属性时出现错误,因为此链接位于属性表单<%= link_to "Add Pictures", new_property_gallery_path(@property) %>的底部。

我想基本上创建一个属性,然后有一个链接来创建一个与该属性相关联的库。那是我的目标。

以下是我得到的实际错误输出,显示我错过了我的属性参数:

enter image description here

这是我的路线文件

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

1 个答案:

答案 0 :(得分:0)

  

问题是我在尝试创建第一个时遇到错误   属性因为此链接位于属性表单的底部   <%= link_to "Add Pictures", new_property_gallery_path(@property) %>

您有nested routes 图库 嵌套在 属性 中。如果您使用此链接创建属性<%= link_to "Add Pictures", new_property_gallery_path(@property) %>,那么它将无效,因为它应该用于为 <创建 新图库 em>关联属性 ,它期望property_id不应该 nil

如果您想先创建 属性 ,请使用以下链接。

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