i have 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 tutorial. need in line:
@registration.paypal_url(registration_path(@registration))
now, own controller, feed_controller
, where
def create @feed = feed.new(check_params) end
in view erb file put:
@feed.paypal_url(feed_path(@feed))
in 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 /:locale/feed(.:format) feed#index feed#create post /:locale/feed/create(.:format) feed#new feed_new /:locale/feed/new(.:format) feed#destroy feed_destroy /:locale/feed/destroy(.:format) feed#edit feed_edit /:locale/feed/edit(.:format) feed#update feed_update /:locale/feed/update(.:format)
but prints next error:
undefined method `paypal_url' <#feed::activerecord_relation:0x007fee24f5fc98>
how can fix it? 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
simply use def self.paypal_url(return_path)
instead of def paypal_url(return_path)
.
explanation
you ran problem defining class method
instead of instance method
, there's other posts discussing this.
the basic difference is, when defining:
def self.get_some_url # code return url of instance end
you can desired url of objects, in view:
<% @feeds.each |feed| %> <%= feeds.get_some_url %> <% end %>
now calling feed.get_some_url
on class make no sense. url of thousands call?
but there lot of use class methods
(where define method without self
did)
def get_top_5 # code return top 5 viewed feeds end
since has nothing single instance, define entire class
. leading call: feed.get_top_5
, makes sense.
the second problem not understanding difference between where
& find
, this post out that.
Comments
Post a Comment