0

I have a piece of code in Rails,

def create
  @registration = Registration.new(registration_params)
  if @registration.save
    redirect_to @registration.paypal_url(registration_path(@registration))
  else
    render :new
  end
end

I took it from tutorial. But I need just in this line:

@registration.paypal_url(registration_path(@registration))

Now, about my own controller, feed_controller, where

def create
  @feed = Feed.new(check_params)
end

In the view erb file I put:

@feed.paypal_url(feed_path(@feed))

In my feed.rb (model):

def paypal_url(return_path)
  values = {
    business: "merchant@gotealeaf.com",
    cmd: "_xclick",
    upload: 1,
    return: "#{Rails.application.secrets.app_host}#{return_path}",
    invoice: id,
    amount: course.price,
    item_name: course.name,
    item_number: course.id,
    quantity: '1'
  }
  "#{Rails.application.secrets.paypal_host}/cgi-bin/webscr?" + values.to_query
end

Rake routes:

feed GET        /:locale/feed(.:format)                     feed#index
feed#create     POST       /:locale/feed/create(.:format)
feed#new        feed_new GET        /:locale/feed/new(.:format)
feed#destroy    feed_destroy GET        /:locale/feed/destroy(.:format)
feed#edit      feed_edit GET        /:locale/feed/edit(.:format)
feed#update    feed_update GET        /:locale/feed/update(.:format) 

But it prints the next error:

undefined method `paypal_url' for <#Feed::ActiveRecord_Relation:0x007fee24f5fc98>

How can I fix it? What is the problem?

UPDATE

def index
  @current_user_is = current_user.email
  session[:email] = @current_user_is
  session[:id] = current_user.id

  unless (current_user.member.present?)
    @member = Member.new(:user_id => current_user.id)
    @member.save()
    redirect_to '/feed'
  else
    @new_feed = Feed.new
    @feed = Feed.where(:member_id => current_user.member.id)
    @category = Category.all
    render 'home/uploads'
  end
end
Kristján
  • 18,165
  • 5
  • 50
  • 62

1 Answers1

0

Simply use def self.paypal_url(return_path) instead of def paypal_url(return_path).

Explanation

You ran into your problem by defining a Class Method instead of an Instance Method, there's other posts discussing this.

The basic difference is, when defining:

def self.get_some_url
  # code to return url of an instance
end

you can easily get the desired url of any objects, as in a view:

<% @feeds.each do |feed| %>
  <%= feeds.get_some_url %>
<% end %>

Now calling Feed.get_some_url on the class would make no sense. Which url of the thousands would it call?

But there is a lot of use for class methods (where you define the method without self as you did)

def get_top_5
  # code to return the top 5 most viewed feeds
end

Since this has nothing to do with a single instance, you define it for the entire Class. Leading to this call: Feed.get_top_5, which makes perfectly sense.


The second problem was not understanding the difference between where & find, this post will help you out with that.

Community
  • 1
  • 1
davegson
  • 8,205
  • 4
  • 51
  • 71