未定义的方法`order_url'

时间:2014-03-05 15:04:56

标签: ruby-on-rails ruby-on-rails-4

尝试设置页面,以便用户在登录时下订单。

如果你输入/ listings / 27 / orders / new,这将转到一个新的订单表格,这样你就可以订购第27项。但是当我填写地址详细信息并创建订单时,我在OrdersController中收到错误..NoMethodError #create..undefined方法`order_url'代表#

OrdersController#创建

class OrdersController < ApplicationController
  before_action :set_order, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!

  # GET /orders
  # GET /orders.json
  def index
    @orders = Order.all
  end

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

  # GET /orders/new
  def new
    @order = Order.new
    @listing = Listing.find(params[:listing_id])
  end

  # GET /orders/1/edit
  def edit
  end

  # POST /orders
  # POST /orders.json
  def create
    @order = Order.new(order_params)
    @listing = Listing.find(params[:listing_id])
    @seller = @listing.user


    @order.listing_id = @listing.id
    @order.buyer_id = current_user.id
    @order.seller_id = @seller.id


    respond_to do |format|
      if @order.save
         # ERROR on the following line!!
        format.html { redirect_to @order, notice: 'Order was successfully created.' }
        format.json { render action: 'show', status: :created, location: @order }
      else
        format.html { render action: 'new' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /orders/1
  # PATCH/PUT /orders/1.json
  def update
    respond_to do |format|
      if @order.update(order_params)
        format.html { redirect_to @order, notice: 'Order was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.json
  def destroy
    @order.destroy
    respond_to do |format|
      format.html { redirect_to orders_url }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def order_params
      params.require(:order).permit(:delivery_address, :delivery_city, :delivery_state)
    end
end

listing.db

class Listing < ActiveRecord::Base
    if Rails.env.development?
        has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "photo.jpg"
    else
    has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "photo.jpg",
                      :storage => :dropbox,
                      :dropbox_credentials => Rails.root.join("config/dropbox.yml"),
                      :path => ":style/id_:filename"
    end
    validates :name, :description, :price, presence: true
    validates :price, numericality: { greater_than: 0 }
    validates_attachment_presence :image

    belongs_to :user
    has_many  :orders
end

order.rb

class Order < ActiveRecord::Base
    validates :delivery_address, :delivery_city, :delivery_state, presence: true

    belongs_to :listing
    belongs_to :buyer, class_name: "User"
    belongs_to :seller, class_name: "User"
end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

   validates :name, presence: true

   has_many :listings, dependT.nt: :destroy
   has_many :sales, class_name: "Order", foreign_key: "seller_id"
   has_many :purchases, class_name: "Order", foreign_key: "buyer_id"
end

rake routes

devise_for :users
    resources :listings do
         resources :orders
  end


  get "pages/about"
  get "pages/contact"
  get 'seller' => "listings#seller"


  root 'listings#index'

3 个答案:

答案 0 :(得分:1)

根据嵌套路线,

devise_for :users
    resources :listings do
         resources :orders
end

order_url不存在,重定向到@order会使rails找到路径order_url。 执行rake routes并检查可用路径(查看prefix列)。

使用

redirect_to listing_order_url(@listing,@order)

而不是

redirect_to @order

create的{​​{1}}和update行动中

另外,更新OrdersController操作如下

destroy

答案 1 :(得分:0)

我相信您正在重定向到变量,而您应该重定向到路径文件中的URL。

我认为应该是:

# ...

respond_to do |format|
  if @order.save
    format.html { listing_order_url(@listing, @order), notice: 'Order was successfully created.' }
    format.json { render action: 'show', status: :created, location: @order }
  else
    format.html { render action: 'new' }
    format.json { render json: @order.errors, status: :unprocessable_entity }
  end
end

答案 2 :(得分:0)

您正在使用嵌套资源。您应该更改重定向以使用嵌套资源URL,在本例中为:

redirect_to listing_order_url(@listing, @order), notice: 'Your order has been created'

根据Rails Guides,如果它是link_to,您可以使用其他格式:

link_to 'Order Details', [@listing, @order]