简体   繁体   中英

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

I am trying to create a property in my application that has a link to galleries associated with the property. The issue is that I am getting an error when I try to create the first property because of this link here at the bottom of the property form <%= link_to "Add Pictures", new_property_gallery_path(@property) %> .

I would like to basically create a property and then have a link to create a gallery that is associated with the property. That's my goal.

Here is the actual error output I'm getting, showing that I'm missing my property params:

在此处输入图片说明

Here is my routes file

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

And here is the 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

The issue is that I am getting an error when I try to create the first property because of this link here at the bottom of the property form <%= link_to "Add Pictures", new_property_gallery_path(@property) %>

You have nested routes where galleries nested inside properties . If you are creating a property using this link <%= link_to "Add Pictures", new_property_gallery_path(@property) %> then it won't work because it should be used to create a new gallery for the associated property and it expects a property_id which shouldn't be nil .

If you want to create a property first then use the below link.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM