simple_form_for 如何将参数传递给控制器
simple_form_for how to pass params to controller
我想将参数从 event/id(显示页面)传递到我的 order_controller。
我使用simple_form_for传递event.id和促销代码 用户输入
#event.show.html.haml
= simple_form_for order_url, url: orders_path(@event, :promocode), method: :post do |f|
= f.hidden_field :event_id, params: {id: @event.id}
= f.input :promocode, value: :promocode, class: 'form-control', placeholder: "Enter your PromoCode"
= f.submit 'APPLY PromoCode'
IDK 如果需要 hidden_field 通过 event_id
#order_controller
class OrdersController < ApplicationController
before_action :order, only: %i[show]
def index
@orders = Order.all.order(created_at: :desc).page(params[:page]).per(5)
end
def show; end
def create
@order = Order.create(title: event.title, user_id: current_user.id, event_id: event.id, order_amount: event.price, order_currency: event.currency)
if !promo.nil?
redirect_to_order
elsif @order.save
redirect_to checkout_create_path(id: @order.id)
else
redirect_to event, alert: 'Something went wrong, try again later'
end
end
def redirect_to_order
promo_validate
order_amount_promo_code = @order.order_amount - promo.promo_code_amount
order.update(order_amount: order_amount_promo_code)
redirect_to @order
end
def promo_validate
if promo.present? && promo.promo_code_amount.positive? && promo.promo_code_currency == event.currency
promo.update(order_id: @order.id)
else
redirect_to event, alert: "This PromoCode is invalid or Your PromoCode Currency doesn't match with Event"
end
end
private
def promo
@promo ||= PromoCode.find_by(uuid: params[:promocode])
end
def event
@event ||= Event.find(params[:id])
end
def order
@order ||= Order.find(params[:id])
end
def order_params
params.require(:order).permit(:title, :event_id, :promocode, :event)
end
end
我正在使用方法 def event 和 def promo 从视图中获取此参数。
我的路线也是这样。
- 资源:事件
- 资源:订单
我会嵌套路线:
resources :events do
resources :orders, shallow: true
end
这在两个资源之间创建了一个明确的关系,只需查看 URL 就可以看出这一点。要创建与 even 绑定的订单,您可以向 /events/:event_id/orders
.
发送 POST 请求
class EventsController
def show
# ..
@order = @event.orders.new
end
end
= simple_form_for [@event, @order] do |f|
= f.input :promocode, value: :promocode, class: 'form-control', placeholder: "Enter your PromoCode"
= f.submit 'APPLY PromoCode'
class OrdersController < ApplicationController
# POST /events/:id/orders
def create
@event = Event.find(params[:event_id])
@order = @event.orders.new(title: @event.title, user: current_user order_amount: @event.price, order_currency: @event.currency)
begin
@promo = PromoCode.find_by!(uuid: params[:order][:promocode])
rescue ActiveRecord::RecordNotFound
@order.errors.add(:promocode, 'is invalid')
end
if @order.save
redirect_to checkout_create_path(id: @order.id)
else
redirect_to @event, alert: 'Something went wrong, try again later'
end
end
# ...
end
除此之外,您对促销代码的处理非常不稳定。与其四处游荡并通过更新记录从“金额”中扣除回扣,不如同时存储原始销售价格和回扣,然后在结账时计算总额——这也应该单独存储。不这样做意味着记录保持得非常糟糕,可能会给您带来麻烦 - 涉及金钱时,请务必谨慎行事。
我想将参数从 event/id(显示页面)传递到我的 order_controller。
我使用simple_form_for传递event.id和促销代码 用户输入
#event.show.html.haml
= simple_form_for order_url, url: orders_path(@event, :promocode), method: :post do |f|
= f.hidden_field :event_id, params: {id: @event.id}
= f.input :promocode, value: :promocode, class: 'form-control', placeholder: "Enter your PromoCode"
= f.submit 'APPLY PromoCode'
IDK 如果需要 hidden_field 通过 event_id
#order_controller
class OrdersController < ApplicationController
before_action :order, only: %i[show]
def index
@orders = Order.all.order(created_at: :desc).page(params[:page]).per(5)
end
def show; end
def create
@order = Order.create(title: event.title, user_id: current_user.id, event_id: event.id, order_amount: event.price, order_currency: event.currency)
if !promo.nil?
redirect_to_order
elsif @order.save
redirect_to checkout_create_path(id: @order.id)
else
redirect_to event, alert: 'Something went wrong, try again later'
end
end
def redirect_to_order
promo_validate
order_amount_promo_code = @order.order_amount - promo.promo_code_amount
order.update(order_amount: order_amount_promo_code)
redirect_to @order
end
def promo_validate
if promo.present? && promo.promo_code_amount.positive? && promo.promo_code_currency == event.currency
promo.update(order_id: @order.id)
else
redirect_to event, alert: "This PromoCode is invalid or Your PromoCode Currency doesn't match with Event"
end
end
private
def promo
@promo ||= PromoCode.find_by(uuid: params[:promocode])
end
def event
@event ||= Event.find(params[:id])
end
def order
@order ||= Order.find(params[:id])
end
def order_params
params.require(:order).permit(:title, :event_id, :promocode, :event)
end
end
我正在使用方法 def event 和 def promo 从视图中获取此参数。 我的路线也是这样。
- 资源:事件
- 资源:订单
我会嵌套路线:
resources :events do
resources :orders, shallow: true
end
这在两个资源之间创建了一个明确的关系,只需查看 URL 就可以看出这一点。要创建与 even 绑定的订单,您可以向 /events/:event_id/orders
.
class EventsController
def show
# ..
@order = @event.orders.new
end
end
= simple_form_for [@event, @order] do |f|
= f.input :promocode, value: :promocode, class: 'form-control', placeholder: "Enter your PromoCode"
= f.submit 'APPLY PromoCode'
class OrdersController < ApplicationController
# POST /events/:id/orders
def create
@event = Event.find(params[:event_id])
@order = @event.orders.new(title: @event.title, user: current_user order_amount: @event.price, order_currency: @event.currency)
begin
@promo = PromoCode.find_by!(uuid: params[:order][:promocode])
rescue ActiveRecord::RecordNotFound
@order.errors.add(:promocode, 'is invalid')
end
if @order.save
redirect_to checkout_create_path(id: @order.id)
else
redirect_to @event, alert: 'Something went wrong, try again later'
end
end
# ...
end
除此之外,您对促销代码的处理非常不稳定。与其四处游荡并通过更新记录从“金额”中扣除回扣,不如同时存储原始销售价格和回扣,然后在结账时计算总额——这也应该单独存储。不这样做意味着记录保持得非常糟糕,可能会给您带来麻烦 - 涉及金钱时,请务必谨慎行事。