Rails - 如何在视图中创建不属于Rails 5中的同一个控制器

问题描述 投票:0回答:1

所以基本上我正在尝试实现应该在产品中的订单表单#show view这是一个不属于OrderController的视图。

P.S:订单和产品模型之间的适当关联是多对多的,但在我的情况下它是一对多的,因为订单只包含一个产品

到目前为止,我写了以下内容:订单模型

class Order < ApplicationRecord
  belongs_to :product
  belongs_to :user
end

产品型号

class Product < ApplicationRecord
  has_many :orders
  has_many :comments

  # define scope for fetching products that have images
  scope :products_with_images, -> {where.not(image_url: nil)}
  scope :products_with_comments, -> {joins(:comments).order(created_at: :desc)}
end

订单控制器

class OrdersController < ApplicationController
  before_action :authenticate_user!
  load_and_authorize_resource
  def index
    @orders = Order.includes(:product, :user).where(user_id: current_user.id).all
  end
  def new
  end
  def show
    @order = Order.find(params[:id])
  end
  def create
    @product = Product.find(params[:product_id])
    @order = @product.order.new(order_params)
    @order.user = current_user
    if @order.save
      redirect_to order_path(@order)
    else
      flash[:error] = "Something went wrong!"
    end
  end
  def destroy
  end
  private
  def order_params
    params.require(:order).permit(:user_id, :total)
  end
end

产品控制器

 def show
    @comments = @product.comments.order("created_at DESC")
    @order = Order.where('user_id= ?', current_user.id)
  end

  # GET /products/new
  def new
    @product = Product.new
    @order = Order.new
  end

意见/命令/ _new_order.html.erb

<%= form_with(model: @orders) do |f| %>
<%= f.hidden_field :product_id, value: @product.id %>
<%= f.hidden_field :total, value: @product.price %>
<%= f.submit "Order", class: "btn btn-info btn-lg my-2" %>
<% end %>

意见/产品/ show.html.erb

  # some code related to product show here and render new_order partial view
  <%= render 'orders/new_order' %>

路线文件:

Rails.application.routes.draw do
  # if user is signed in the root_path will be products#index
  authenticated :user do
    root 'products#index', as: :authenticated_root
  end
  root 'static_pages#index'
  get '/index', to: 'static_pages#index'
  get '/about', to: 'static_pages#about'
  get '/featured', to: 'static_pages#featured'
  post 'static_pages/thank_you'

  # devise_for :users
  devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout', sign_up: 'register'}

  resources :users

  resources :products do
    resources :comments
  end

  # get '/products/:id', to: 'products#show'
  resources :orders, only: [:index, :show, :create, :destroy]
  # resources :orders, except: [:new, :edit, :update]
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

因此,当我尝试在产品#show page中添加新订单时,我从服务器控制台收到此错误:

ActionController::RoutingError (No route matches [POST] "/products/1")

谢谢你的帮助

activerecord ruby-on-rails-5.1 ruby-on-rails-5
1个回答
0
投票

我们可以渲染表单,从您使用的任何控制器到另一个控制器,不需要任何路由。问题是实例变量中的拼写错误,如下所示

产品控制器

def show
  @comments = @product.comments.order("created_at DESC")
  @orders = Order.where('user_id= ?', current_user.id)
end
© www.soinside.com 2019 - 2024. All rights reserved.