开发者

Undefined local variable or method `current_submission' for #<OrdersController:0x000000056a6a30>

开发者 https://www.devze.com 2023-04-10 07:08 出处:网络
I am trying to create an app for managing an academic conference. Users will sign in and submit abstracts for consideration of being presented at our annual conference. I have followed the \"Ruby on R

I am trying to create an app for managing an academic conference. Users will sign in and submit abstracts for consideration of being presented at our annual conference. I have followed the "Ruby on Rails 3 Tutorial" for most of what I have done. My modifications include开发者_如何学Go adding a Submission model, a Registration model, and an Order model. The Submission model belongs_to the User model and has_one order.

I am trying to follow the Railscast 145/146 episodes about integrating PayPal Express checkout using ActiveMerchant. My goal is to put a PayPal checkout button on the submissions show.html.erb page so that after the author has submitted his abstract he can pay the submission fee right away.

The error arises from the following code after I have submitted the order:

class OrdersController < InheritedResources::Base

  def new
    @order = Order.new
  end

  def create
    @order = current_submission.orders.build_order(params[:order])

    @order.ip_address = request.remote_ip
    if @order.save

      # flash[:notice] = "Successfully created order."
      #redirect_to orders_url
    else
      render :action => 'new'
    end
  end
end

From what I have read, I think the problem has to do with how orders are related to submissions. Each submission should have a related order entry, so in the Order table there should be a submission_id. I am really confused about how this works.

Could someone please point me in the right direction? Is there a user guide that you would recommend that I read? There seems to be a lot of conflicting information out there between the different versions of Rails. I am using Rails 3.0.10.

Thanks!


You should setup your resources as

resources :submissions do
  resource :order
end

This way your params will reflect the particular submission being accessed via an :id attribute. Your orders#create action would then change to

@submission = Submission.find(params[:submission_id])
@order = @submission.orders.build_order(params[:order])

The relation you have reflected above seems to indicate Submission has_many :orders. Regarding your comment

The Submission model belongs_to the User model and has_one order.

In that case you need to change the above to - notice how orders becomes order:

@order = @submission.order.build_order(params[:order])

So your relationships are Submission has_one :order; Order :belongs_to :submission and User :has_one :submission?

Update

Based on your comments describing your requirements, your relationships need to change as follows:

class User < ActiveRecord::Base
  has_many :orders
  has_many :submissions, :through => :orders

class Order < ActiveRecord::Base
  # this table needs attributes user_id and session_id
  belongs_to :user
  belongs_to :session

  validates :submission_id, :uniqueness => true

class Submission < ActiveRecord::Base
  has_many :orders
  has_many :users, :through => :orders

Since I've placed a uniqueness constraint on the join table, it will ensure that a particular submission_id can only occur once in any record. You can setup your controller using 'orders' like before and it'll most likely be @submission = Submission.find(params[:id]) rather than @submission = Submission.find(params[:submission_id]) since orders is a singular resource.

If you need further assistance with this drop me an email at mike at bsodmike dot com.

0

精彩评论

暂无评论...
验证码 换一张
取 消