Ruby on rails。从另一个控制器

时间:2015-12-18 17:30:31

标签: ruby-on-rails post controller braintree

我有一个“工作网站”,用户可以在其中创建工作,其他用户可以申请。用户需要“令牌”来创建作业,而当他们没有剩余时,会被重定向到购买令牌页面。令牌系统工作正常。 我已经设置了一个也适用的braintree表单,但是没有为用户创建任何令牌。我想在事务完成时为用户创建一个新的令牌。

到目前为止我已经

的routes.rb

resources :users do
resources :tokens
resources :transactions

Token.rb

class Token < ActiveRecord::Base
 belongs_to :user
end

Transactions.rb

 class Transaction < ActiveRecord::Base
  belongs_to :user
end

新令牌/付款表单

    <form id="checkout" method="post" action="/transactions">
  <div id="payment-form"></div>
  <h2 class="mbs">Transaction</h2>
  <p> Payment formfor one gig </p>
  <%= form_tag transactions_path do%>
    <p>Please enter your payment details:</p>
    <div id="dropin"></div>
    <%=submit_tag "Pay €1.99", class: "button mt1" %>
  <%end%>
</form>

transactions_controller

 class TransactionsController < ApplicationController
before_action :find_user

  def new
  end


def create
    @result = Braintree::Transaction.sale(
              amount: 10,
              payment_method_nonce: params[:payment_method_nonce])
    if @result.success?
      @user = Token.build(params[:user_id => current_user.id])
      redirect_to new_gig_path, notice: "Congraulations! Your transaction has been successfully!"
    else
      flash[:alert] = "Something went wrong while processing your transaction. Please try again!"
      render :new
    end
  end

private
def generate_client_token
  Braintree::ClientToken.generate
end

def token_params
  params.require(:token).permit(:user_id)
end
def find_user
  @user = User.find(params[:user_id])
end
end

但是我收到错误'找不到'id'='

的用户

braintree clienttokens在令牌控制器中生成,因为表单位于新令牌页面上。这很有效。

这是我最后一次尝试这样做,但我尝试了多种方法为current_user创建令牌,但我无法让它工作。 令牌模型属性是token_id和user_id。

帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:1)

修正:

before_action :find user

def find_user
  @user = User.find(params[:user_id])
end

wern不需要。

删除它们并添加

Token.create(user_id: current_user.id)

修复了它。谢谢。

答案 1 :(得分:1)

您的before_action使用请求发送的参数设置@user。 before_action:find_user表示无论TransactionController中的操作如何,在运行任何代码之前,都要运行find_user中的代码。在find_user中,使用请求参数设置@user变量。请求参数是在表单或查询字符串中发送的参数。使用post操作调用create方法,这意味着参数应包括调用其提交操作的表单中的所有属性。在表单视图中,我看到两个嵌套表单。

使用嵌套资源(如上面的路径文件所反映的),Rails指南上的Nested Resources演示了user_id是由事务提交的路径设置的。这意味着表单操作的url帮助需要为params[:user_id => current_user.id]。基本上,表单需要指向/ users / [:user_id] / transactions

使用Rail的form_for helper on the API可能更容易。这是一个非常复杂的解决方案,它有许多根本性的错误。我从未见过像package main import ( "fmt" "os" "path/filepath" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3/s3manager" ) var ( // empty strings for security reasons Bucket = "" // Download from this bucket Prefix = "" // Using this key prefix LocalDirectory = "s3logs" // Into this directory ) func main() { sess := session.New() client := s3.New(sess, &aws.Config{Region: aws.String("us-west-1")}) params := &s3.ListObjectsInput{Bucket: &Bucket, Prefix: &Prefix} manager := s3manager.NewDownloaderWithClient(client, func(d *s3manager.Downloader) { d.PartSize = 64 * 1024 * 1024 // 64MB per part d.Concurrency = 8 }) // works //manager := s3manager.NewDownloaderWithClient(client) //works d := downloader{bucket: Bucket, dir: LocalDirectory, Downloader: manager} client.ListObjectsPages(params, d.eachPage) } type downloader struct { *s3manager.Downloader bucket, dir string } func (d *downloader) eachPage(page *s3.ListObjectsOutput, more bool) bool { for _, obj := range page.Contents { d.downloadToFile(*obj.Key) } return true } func (d *downloader) downloadToFile(key string) { // Create the directories in the path file := filepath.Join(d.dir, key) if err := os.MkdirAll(filepath.Dir(file), 0775); err != nil { panic(err) } fmt.Printf("Downloading " + key) // Setup the local file fd, err := os.Create(file) if err != nil { panic(err) } defer fd.Close() // Download the file using the AWS SDK fmt.Printf("Downloading s3://%s/%s to %s...\n", d.bucket, key, file) params := &s3.GetObjectInput{Bucket: &d.bucket, Key: &key} _, e := d.Download(fd, params) if e != nil { panic(e) } 这样的东西我猜你正在尝试在查找Ruby doc内进行哈希分配。

两条建议:

  • 从简单的事情开始
  • 符合Rails惯例

快乐脚本

相关问题