ruby - Rails 4. 将表单信息从一个 Controller (无模型)移动到另一个(无模型)以便将参数传递给方法

标签 ruby ruby-on-rails-4 redis

我有一个简单的应用程序,用户可以在其中购买商品。在项目页面上会有一个添加到购物车按钮。 carts-controller 使用 REDIS 关系显示项目,它没有模型。一旦用户查看了商品并希望购买,他们将被引导至购买 View ,一旦他们使用 stripe 支付,订单将在购买 Controller 中创建。采购 Controller 也没有模型。

user.rb 模型:

class User < ActiveRecord::Base
    has_many :orders

    def get_cart_items
        cart_ids = REDIS.smembers "cart#{id}"
        Item.find(cart_ids)
    end

    def purchase_cart_items!(recipient_name:, recipient_address:)
        get_cart_items.each { |item| purchase!(item, recipient_name, recipient_address) }
        REDIS.del "cart#{id}"
    end

    def purchase!(item, recipient_name, recipient_address)
        self.orders.create!(user_email: self.email,
                            item_id: item.id,
                            recipient_name: recipient_name)
                            recipient_address: recipient_address)                     
   end
end

采购 Controller :

class PurchasesController < ApplicationController

    def create
        Stripe::Charge.create(
        ...
        )
        ##########
        current_user.purchase_cart_items!(recipient_name:, recipient_address:)
        ##########
    end
end

推车 Controller :

class CartsController < ApplicationController
    def create
        REDIS.sadd current_user_cart, params[:item_id]
    end
    def destroy
        REDIS.srem current_user_cart, params[:item_id]
    end
 end

创建 user_email: 的订单属性和 item_id:很容易通过调用 self.emailitem.id因为它们附加到模型,但我不确定如何使用购物车 View 中的表单让用户输入 recipient_name:recipient_address: purchase_cart_items!(recipient_name:, recipient_address:) 的参数purchases-controller 中的方法。

我需要:

  1. 在购物车 View 中创建一个表单,用户可以在其中为另一个 Controller 中的方法输入参数
  2. 提交表单详细信息后,我需要将用户重定向到购买 View
  3. 然后必须将表单信息作为参数传递给购买 Controller 中的方法

谢谢!

最佳答案

需要使用带有 GET 方法的 form_tag 来将表单信息提交给正确的 Controller 。 在购物车 View 中:

<%= form_tag({controller: "purchases", action: "new"}, method: "get") do %>
  <%= label_tag 'recipient name: ' %>
  <%= text_field_tag 'recipient_name' %>
  <%= label_tag 'recipient address: ' %>
  <%= text_field_tag 'recipient_address' %>
  <%= submit_tag "PAY" %>
<% end %>

在购买 Controller 的 new 操作中,我使用 params 获取表单信息,并使用 session 使表单信息可用在同一 Controller 中创建操作:

class PurchasesController < ApplicationController
    def new
        @name = params[:name]
        @address = params[:address]
        session[:name] = @name
        session[:address] = @address
    end
    def create
        Stripe::Charge.create(
        ...
        )
    ##########
        @name = session[:name]
        @address = session[:address]
        current_user.purchase_cart_destinations!(name: @name, address: @address)
    ##########
    end
end

一旦可以在 purchases-controller 的 create 操作中访问表单信息,current_user.purchase_cart_items! 的关键字参数就很容易用 的实例变量设置>@name@address

关于ruby - Rails 4. 将表单信息从一个 Controller (无模型)移动到另一个(无模型)以便将参数传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29065338/

相关文章:

ruby - 如何在 Sequel 中创建准备好的插入语句?

ruby - 安装 ruby​​gems 时出错

javascript - 使用数据属性的 Bootstrap 词缀仅适用于页面重新加载

ruby-on-rails - 从/lib 目录中定义的类访问 ActionView::Helpers::DateHelper

nosql - Leveldb 与 Redis、Riak 或 Tokyo Tyrant 相比如何?

php - 将大型数组转换为无循环的关联数组

asp.net - 这是 StackExchange Redis 流水线的良好实现吗?

ruby-on-rails - 列必须出现在 GROUP BY 子句中或用于聚合函数 Ruby

ruby-on-rails - 没有这样的文件或目录 -/dev/null (Errno::ENOENT)

ruby-on-rails - Rails 4 搜索绑定(bind)变量数量错误的单独关键字(1 对 2)