購入機能の実装を行っています。
購入するのボタンを押したらNoMethodErrorが出てしまい、
コントローラーを確認するも、記述ミスがわかりません。
idを遷移させたいのです。
よろしくお願いします。
エラーメッセージ Routing Error No route matches [GET] "/items/ItemOrder/order"
order/index <div class='buy-btn'> <%# <%= f.submit "購入" ,class:"buy-red-btn" %> <%= link_to '購入', order_item_path(), class: "buy-red-btn" %> </div>
order.controller class OrdersController < ApplicationController before_action :authenticate_user!, only: :index before_action :set_order, only: [:index, :create] before_action :set_present, only: :index # before_action :set_pay, only: [:create] before_action :find_item, only: :order before_action :set_card, only: :index before_action :set_address, only: :index def index redirect_to new_card_path and return unless current_user.card.present? @order = OrderDonation.new @cards = Card.all end def create @order = OrderDonation.new(order_params) if @order.valid? @order.save redirect_to root_path else render 'index' end end def order redirect_to new_card_path and return unless current_user.card.present? Payjp.api_key = ENV["PAYJP_SECRET_KEY"] customer_token = current_user.card.customer_token Payjp::Charge.create( amount: @item.price, customer: customer_token, currency: 'jpy' ) ItemOrder.create(item_id: params[:id]) redirect_to root_path end private def order_params params.require(:order_donation).permit(:post_id, :prefecture_id, :city, :address, :buildingname, :tel, :token).merge(user_id: current_user.id, item_id: params[:item_id]) end def set_order @item = Item.find(params[:item_id]) end def set_present if @item.order.present? redirect_to root_path elsif current_user.id == @item.user_id redirect_to root_path end end # def set_pay # Payjp.api_key = ENV['PAYJP_SECRET_KEY'] # Payjp::Charge.create( # amount: @item.price, # card: order_params[:token], # currency: 'jpy' # ) # end def find_item @item = Item.find(params[:id]) end def set_card @card = Card.find_by(user_id: current_user.id) end def set_address @address = Address.find_by(user_id: current_user.id) end end
routes.rb Rails.application.routes.draw do get 'cards/new' get 'users/show' devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks', registrations: 'users/registrations' } devise_scope :user do get 'addresses', to: 'users/registrations#new_address' post 'addresses', to: 'users/registrations#create_address' end root to: 'items#index' resources :items do resources :orders, only:[:index, :create] post 'order', on: :member resources :comments, only: :create end resources :users, only:[:show, :edit, :update] resources :cards, only: [:new, :create] end
order.model class Order < ApplicationRecord belongs_to :user belongs_to :item has_one :address end
回答1件
あなたの回答
tips
プレビュー