SupremeA
SupremeA

Reputation: 1621

Passing an object to another controller in rails

I have a request model. When I create a new request from a form I have, if one of the items in the "request" model is true then I want to send the user to a charge page. I then want to get one of the fields from the request to set the charge amount. I am doing this thru the controllers but the charge is not picking up the request object to get the amount from it. Here is my request controller.

def create
  @user = current_user
    if @request = @user.request.create!(authorization_params)
      if @request.must_pay == true
        redirect_to new_users_charge_path(@request), :notice => "Please make payment before proceeding"
      else
        redirect_to users_dashboard_path, :notice => "Success"
      end
    else
      redirect_to users_dashboard_path, :error => "Oops!"
    end

def authorization_params
      params.require(:request).permit( :field_1, :field_2, etc...)
end

So when it passes this request to the new charge path, I want to get the how much to pay field out of the request so my charge controller looks like this.

def create
  # Amount in cents
  @request = Request.find(params[:id])
  @user = current_user
  @amount = @request.how_much_to_pay

  customer = Stripe::Customer.create(
    :email => @user.email,
    :card  => params[:stripeToken]
    ) 

  charge = Stripe::Charge.create(
    :customer    => customer.id,
    :amount      => @amount,

But I keep getting the error undefined method `how_much_to_pay' for nil:NilClass

So I don't think its passing the request to the charge controller. What am I missing?

Here is the charges form that is created.

<%= form_tag users_charges_path do %>
  <article>
    <label class="amount">
      <span>Amount: <%= number_to_currency (@request.how_much_to_pay.to_i/100) %></span>
    </label>
  </article>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="..."
          data-amount="<%= @request.how_much_to_pay %>"
          data-locale="auto"></script>
<% end %>

Upvotes: 1

Views: 892

Answers (1)

baron816
baron816

Reputation: 701

@request is nil, so Request.find(params[:id]) isn't finding anything. So it's not getting an id in the params to find a request.

I'm not totally clear on what you're trying to do. I don't see why you don't just use a model method for creating the customer and charge.

Upvotes: 1

Related Questions