Model and Link error Rails - ruby-on-rails

I am trying to implement Paypal into an application of mine. I have gone through the excellent railscasts demo at http://railscasts.com/episodes/141-paypal-basics
However I dont really have a cart in my app, its more of a one off purchase. But I want to get Paypal intergrated and not just have a "button".
I took the Cart model from the demo and built my own:
class Paypal < ActiveRecord::Base
def paypal_url
values = {
:business => 'seller_1229899173_biz#railscasts.com',
:cmd => '_cart',
:upload => 1,
:return => return_url,
:invoice => id
}
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end
end
Then for a link in my view I have:
<%= link_to "Checkout", #paypal.paypal_url(products_url) %>
I know this is not right, as I dont have any products as such. But I would like to pass varilables to payal and recieve variabled back. At the moment I get the error:
undefined method `paypal_url' for nil:NilClass
Which happens on load.
In my routes I have included:
resources :paypal
But no joy!

Related

Rails Simple Form Select from Array - polymorphic associations

I am making an app in Rails 4. I use Simple Form for forms.
I have a profile model and an organisation model.
The associations are:
profile.rb
has_one :organisation, as: :orgable
organisation.rb
has_many :profiles
In my organisation table, I have an attribute called :org_type.
In my organisation form, I ask users to select from an array of types of organisation:
<%= f.input :org_type, :label => "Organisation type", collection: [ "University", "Research Organisation", "Company"] %>
In my profile form, I want to ask users which uni they study at.
I want to use the array of universities created within the organisation model.
I have a scope in my organisation model to filter out the universities:
scope :all_uni, -> { where(org_type: 'University') }
In my profile form I have:
<%= f.input :institution, :label => "Graduated from" %>
But this just has a text field.
I have tried to replace that line with an attempt at making a select function in my form which refers to my organisation model scope for all_uni. It looks like this:
<%= f.select(:institution, #organisation.all_uni.title.map { |value| [ value, value ] }) %>
It gives this error:
undefined method `all_uni' for nil:NilClass
I don't understand what this error message means, but I'm also not sure I'm on the right track with the form select field either. Any tips for where to look to get this working. I'm not sure how to go about setting up the select field in the first place?
ANOTHER ATTEMPT:
I have also tried using this in my profile form:
<%= f.select(:institution, #organisation.all_uni.title) %>
But I get the same error. I must be way off track - i've exhausted every option I can find.
ANOTHER ATTEMPT
I found this post
Rails Simple Form custom association select field
Taking the example in that solution, I tried:
<%= f.input :institution do %>
<%= f.select :institution, Profile.Organisation.all_uni.map{ |l| [l.title {:title => l.title.titlecase}] } %>
<% end %>
But, I get this syntax error. I've tried removing the => but keep getting more syntax errors.
syntax error, unexpected =>, expecting '}' ...i.map{ |l| [l.title {:title => l.title.titlecase}] } );#out... ... ^
Not a complete answer but according to what I know is, If you got 2 models then instead of using
profile.rb
has_one :organisation, as: :orgable
organisation.rb
has_many :profiles
You can simply use
profile.rb
belongs_to :organisation
organisation.rb
has_many :profiles

Limiting down the amount of calls to the database during a simple_form with multiple HABTM

I am currently trying to speed up an old (3.2) Rails app and hitting an issue with way too many calls being made to the database during an edit view using simple_form
Here is the simplified setup;
class Event
HABTM Speakers
HABTM Sponsors
class Speaker
belongs_to Sponsor
HABTM Events
class Sponsor
have_many Speakers
HABTM Events
For an event we want to show both each potential speaker (of which there are thousands) and all the sponsors (hundreds).
For each speaker if they have a sponsor they need to show the sponsor name.
This all adds up to a lot of calls to the sponsors table, way too many.
Current Code
Right now the simple_form is set up as;
<%= simple_form_for [:admin, #event] do |f| %>
...
<%= f.association :speakers, :input_html => { :class => 'select2able' }, :label_method => :summary %>
...
<%= f.association :sponsors, :input_html => { :class => 'select2able' } %>
...
That label method is on the speaker model;
delegate :name, to: :sponsor, prefix: true, allow_nil: true
def summary
[name, job_title, sponsor_name].compact.join ' - '
end
The #event variable is assigned in the controller with a simple
#event = Event.find params['id']
This is where I have been spending most of my time trying to improve.
What I have tried
Calling Event.includes(:speakers => :sponsor).find() I thought would help. but it hasn't at all.
I have also tried just including speaker and sponsor separately.
I have searched around for people having issues with simple_form specifically but haven't found anything. I know this is a sign of the mental structure that was made, but anything I can do to improve it without a complete re-write would be excellent.
I was almost there with my original attempts.
This version of Rails didn't like the external includes (it executed but didn't apply anything)
The final code was a slight change where I brought the include inside the find_by.
Event.find_by_slug(params[:id], :include => [:speakers, :sponsors, :speakers => :sponsor])

PayPal integration in Ruby on Rails

I want to add PayPal payment system to my RoR app. For this I did install paypal-sdk-rest gem.
I have a model Feed and inside of index.html.rb, where the route is:
get '/:locale/feed', to: 'feed#index', as: 'feed'
I want to paste the next code:
<%= link_to 'checkout', feed.paypal_url(products_url) %>
And inside of the model feed.rb:
def paypal_url(return_url)
values = {
:business => 'my_paypal_mail#gmail.com',
:cmd => '_cart',
:upload => 1,
:return => return_url,
}
values.merge!({
"amount_1" => unit_price,
"item_name_1" => name,
"item_number_1" => id,
"quantity_1" => '1'
})
"https://www.sandbox.paypal.com/cgi-bin/webscr?" + values.to_query
end
But it prints me, that:
undefined local variable or method `feed' for #<#<Class:0x007f5644b79520>:0x007f5644b89858>
on
<%= link_to 'checkout', feed.paypal_url(feeds_url) %>
What is the problem and how can I fix this error?
UPDATE
I just want to paste button just for payment, to my website with the amount 1$. How can I make it?
Where is feed object defined? If in controller then it should be instance variable like #feed, To access variables defined in controller from views we have to defined them as instance variables like #feed.

ruby on rails TypeError in Users#show - Cannot visit Like

form that causes the problem:
<%= form_for Like.find(post.user.likes), :html => { :method => :delete , :class => "unlike_post_like_form" } do |f| %>
Post model
belongs_to :user
has_many :likes
User model
has_many :posts
has_many :likes
Like model
belongs_to :post
belongs_to :user
i keep getting the following error:
TypeError in Users#show
Cannot visit Like
on
Like.find(post.user.likes),
EDIT:
First solution:
changing
Like.find(post.user.likes),
to
Like.find(post.user.likes).limit(1),
Second solution:
changing
Like.find(post.user.likes),
to
current_user.likes.where(:post_id => post.post_id)
Like.find(post.user.likes) is attempting to find a Like given an array of likes. That doesn't really make much sense.
Most likely, you'll want to search post.user.likes, but even then, I'm a bit confused - you'll need to post more code.
Like.find is looking for either an id (integer), or a hash containing keys and values. An array doesn't give it any information to search.
EDIT: If you're looking for the likes of current user. You'll need to do the following...
current_user.likes.where(:post => #post)
The type error comes from passing a collection (post.user.likes - which is an array) into a method that's expecting a record id, for example, Likes.find(3) or Likes.find(params[:id])

upload multiple files through controller using paperclip and rails

I have used the following tutorials:
http://patshaughnessy.net/2009/5/16/paperclip-sample-app-part-2-downloading-files-through-a-controller
This tutorial walks you through downloading files through the controller rather than as static files. I did this to control access to the files.
http://emersonlackey.com/article/paperclip-with-rails-3
This tutorial walks you through the process of creating a model for your files and using that to allow multiple file uploads
So, for the most part I got the uploading to work fine, however, the problem is in the downloading.
A little background. I have the following key models.
class VisualSubmission < ActiveRecord::Base
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true;
end
class Asset < ActiveRecord::Base
belongs_to :visual_submission
has_attached_file :image, :styles => { :original => "100%", :large => "600x600>", :small => "150x150>", :thumb => "50x50>"}, :convert_options => {:all => "-auto-orient"},
:path => ':rails_root/secure/system/:attachment/:id/:style/:basename.:extension',
:url => '/:class/:id/:attachment?style=:style'
end
In order to download files through the controller, an action is created as such:
def images
visual_submission = VisualSubmission.find(params[:id])
style = params[:style] ? params[:style] : 'original'
send_file visual_submission.image.path(style), :type => visual_submission.image_content_type, :disposition => 'inline'
end
So, as I said above, uploading is just fine. One thing that is different but expected, when it stores the file it uses the ID from the assets model. This is all well and good, however, I am having trouble getting the URL correct now. I created a route to my images:
resources :visual_submissions do
member do
get :images
end
end
And this is what my route looks like.
images_visual_submission GET /visual_submissions/:id/images(.:format) {:action=>"images", :controller=>"visual_submissions"}
Now the piece of code that in theory is supposed to access the image.
This is from my edit form. It is supposed to show the current images stored.
<%= f.fields_for :assets do |asset_fields| %>
<% unless asset_fields.object.new_record? %>
<p>
<%= asset_fields.object.image.url %>
</p>
<% end %>
<% end %>
Now this is obviously not going to work. I am not sure what object is doing here, but what I do know is, my images should be going through my visual_submissions controller which this is not touching. I am not entirely sure if I am asking this questions correctly, but I am stuck. One idea I had was to create a assets controller and move the images method into there but I am not sure how much that will help.
Any thoughts?
Thanks!
#Raymond, just put the last code snippet inside the _form.html.erb corresponding to your visual_submissions. Just be sure that you put it before the <%= f.submit %>, inside the form_for, of course. Hope that helps,

Resources