Must provide source or customer - ruby-on-rails-4

getting an error with stripe in local testing transfer recipients.
I check SQL lite browser and the user has a stripe recipient ID
I tried clearing my entire database, creating a new listing and then placing an order and getting the same error. not sure of the cause of this error, and help would be great!
Stripe::InvalidRequestError in OrdersController#create
Must provide source or customer.
begin
charge = Stripe::Charge.create(
:amount => (#listing.price * 100).floor,
:currency => "usd",
:card => token

This was a turbo-links issue. I fixed the issue by inputting the following code
<% if user_signed_in? %>
<%= link_to 'New Listing', new_listing_path, class: "btn btn-link", data: { no_turbolink: true } %>
<% end %>

Related

Not able to update the password in Rails 5 web app

In My Rails 5 Web App. I have installed Devise gem in it. When I wan to edit my password by clicking on link below but instead on going to the page for update password. Page get refreshed and It gives alert message you are already logged in .
I am using Below link in my Application:
<%= link_to('Edit Password' , edit_user_password_path)%>
Routes Files is as:
devise_for :users, controllers: { sessions: "users/sessions" }
Please suggest me the changes to redirect to edit password page all other links like below are working.
<%= link_to('Edit Registration',edit_user_registration_path )%>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<%= link_to('Edit Registration', edit_user_registration_path) %>
<%= link_to('Edit Password', edit_user_password_path) %>

rails4-autocomplete with belongs to association

I want to implement autocomplete via rails4-autocomplete
Rails 4.2.4
Here is the controller
app/controllers/samples_controller.rb
class SamplesController < ApplicationController
autocomplete :patient, :code
Here is the route file,
config/routes.rb
resources :samples do
get :autocomplete_patient_code, on: :collection
end
And that's the view
app/views/samples/_form.html.erb
<div class="field">
<%= f.label :patient_code %><br>
<%= f.autocomplete_field :patient_id, autocomplete_patient_code_samples_path %>
</div>
With this code I mange to get the autocomplete
However I get invalid foregin key error when try to save the sample that's because the patient's code is passed to the foregin key instead of ID. How do I fix this?
Here is the request Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"blabla",
"sample"=>{"patient_id"=>"A123",
"commit"=>"Create Sample"}
Get "/samples/autocomplete_patient_code?term=A12" returns
{"id":"15","label":"A123","value":"A123"}]
After reading the GitHub documentation of rails4-autocomplete, I devised the following solution:
Add attr_accessor :patient_name to your Sample model and modify the form as follows:
...
<%= f.autocomplete_field :patient_name, autocomplete_patient_code_samples_path, id_element: '#patient_id' %>
<%= f.hidden_field :patient_id, id: 'patient_id' %>
...
With this change whenever you select any patient name, that patient's ID will be updated on the hidden field and it will be submitted as patient_id.
Hope this solves your problem.
Source: Github

Capybara unable to click form button in modal and different capybara drivers see different things

Ok, so my problem is Capybara cannot click the submit button of a form (generated with simple forms) that resides in a modal(Bootstrap v2.3). Please note that the following code is very messy learners code. I am attempting to get it tested up so that I can refactor the hell out of it.
Modal code:
<div class="modal hide" id="updateModal">
<button type="button" class="close" data-dismiss="modal">×</button>
<div class="modal-header"
<h3>Update your score</h3>
</div>
<div class="modal-body">
<%= simple_form_for #update do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.input :newread , :label => "Amount Read", :placeholder => 'pages/screens/minutes #' , :input_html => { :maxlength => 5 } %>
<%= f.input :medium, :label=> "Medium Read", :collection => ["book", "manga", "game", "fgame", "net", "lyric", "subs", "news", "sent", "nico" ], :prompt => "Select medium read" %>
<% lang_list = Update::user_langs(current_user,ApplicationHelper::curr_round) %>
<%= f.input :lang, :label => "Language", :collection => lang_list, :prompt => "Select your language" %>
<%= f.input :repeat, :label => "Repeat number", :collection =>0..50 , :priority => '0' %>
<%= f.input :dr, :inline_label => 'Double Rowed?', :hint => 'Only to be used with Japanese books', :label => false %>
</div>
<div class="modal-footer">
<%= submit_tag 'Cancel', :class => "btn btn-danger", 'data-dismiss' => "modal" %>
<%= f.button :submit, 'Submit Update' , :class => "btn btn-primary"%>
</div>
<% end %>
</div>
Round Controller index function:
def index
#entrants = Round.includes(:user).where(:round_id => "#{ApplicationHelper::curr_round}")
if #entrants == nil
redirect_to root_url, :flash => { :error => "There are currently no users registered for this round." }
end
list = Round.where(:round_id => ApplicationHelper::curr_round).select(:tier).uniq
lang_list = Update.where(:round_id => ApplicationHelper::curr_round).select(:lang).uniq
#tier = list.map(&:tier)
#tier = #tier.sort{ |a,b| Tier::TIER_VALUES[a.to_sym] <=> Tier::TIER_VALUES[b.to_sym]}
#lang = lang_list.map(&:lang)
if signed_in?
#update = current_user.updates.build
end
end
Update_page_spec:
describe "Update Pages" do
before do
sign_in #omniauth fake signin
end
subject { page }
describe "a registered user submitting an update", :js => true do
before do
user = User.find_by_uid(123545)
user_round = user.rounds.create!(round_id: ApplicationHelper::curr_round, lang1: 'jp',
lang2: 'en', lang3:'zh', tier: 'Bronze', book: 10, manga: 10,
fgame: 10, game: 10, net: 10, news: 10, lyric: 10,
subs: 10, nico: 10, sent:10, pcount: 1010)
visit round_path(ApplicationHelper.curr_round)
end
it "should update successfully" do
click_link("Update")
fill_in('update[newread]', :with => '10')
select "book", :from => "Medium Read"
select "Japanese", :from => "Language"
click_button "Submit Update"
save_and_open_page
page.should have_selector('alert-success', :text => "Update successfully submitted")
end
end
end
So I do this and when I check what save_and_open_page sees and its the page with no changes at all. No evidence of the button having been pressed what so ever. So I figure that the modal being js generated might be a problem so I add , :js => true to the describe line, install the webkit driver and add Capybara.javascript_driver = :webkit to my spec_helper.rb file and run it again.
This time I am greeted to the "Signed in" flash from the top before block on the home page instead of being on the ranking page.
So I figure maybe this might turn out better with the selenium driver so I install that and try it again but this time my app complains about there being no one registered for that round. The only way for this to happen is if #entrants is nil and I have checked with pry that this is definitely not the case at least as far as the database is concerned.
Any help you guys can give will be GREATLY appreciated. I have no idea how to get this thing to press my button.
My recommendation:
Clean up the code as much as you can. That will help clarify what is happening, and what may be responsible for the unexpected behavior.
Don't rely on save_and_open_page to accurately show you what the driver is seeing exactly
You definitely need js for the Bootstrap modal
Get selenium working with Firefox so you can see on your screen if the modal is opening properly or not.
You may need the database_cleaner gem to properly access your db with the users you are creating.
you can also try the poltergeist/phantom gems for a js compatible driver. That has screenshot capabilities
Those pieces should show you what's happening with the modal.

error getting path returned rails 4

I'm trying to access my page gallery/new via a link, so i have created this
<%= link_to 'New gallery' new_gallery_path %>
rake routes gives
gallery_index_path GET /gallery(.:format) gallery#index
POST /gallery(.:format) gallery#create
new_gallery_path GET /gallery/new(.:format) gallery#new
edit_gallery_path GET /gallery/:id/edit(.:format) gallery#edit
gallery_path GET /gallery/:id(.:format) gallery#show
PATCH /gallery/:id(.:format) gallery#update
PUT /gallery/:id(.:format) gallery#update
DELETE /gallery/:id(.:format) gallery#destroy
and within my routes i have
resources :gallery
My view at gallery/new is
<%= nested_form_for #gallery, :html => { :multipart => true} do |f| %>
--content here
<% end %>
whenever i click the link to view this page i get
undefined method `galleries_path
Can someone point out my mistake, please?
You've chosen the wrong name for your resources. It should always be pluralized:
resources :galleries
From this Rails will generate the plural and singular paths correctly. galleries_path for the index, gallery_path for show, etc etc.

Rails: How to store values dynamically or as cookie

May be the question will sound weird. But as I am doing it very first time, don't know what is it exactly called.
I have a search method with typeahead used in it on homepage of www.mealnut.com (do check a live demo). There are two entries, city and category. City shows data in typeahead array and on submit, gives the search result. Here is a code:
<%= form_tag search_path, :method => 'get' do %>
<%= text_field_tag 'search', nil, :placeholder =>"Enter your city ", :autocomplete => :off, :'data-provide' => "typeahead", :id=>"citysearch" %>
<%= select_tag :category, options_from_collection_for_select(#categories, "id", "category", params[:category]), html_options = { :prompt => 'Select a category'} %>
<%= submit_tag "Search"%>
<% end %>
And here is typeahead script:
$(document).ready(function(){
var cities = ["Bengaluru", "Pune", "New Delhi", "Delhi", "Mumbai"
$("input[data-provide='typeahead']").typeahead({source: cities});
});
And here is issue: when user clicks on back button to return on home page, city gets wiped out and category remains as selected. Here user should see city, he entered last time instead of blank field.
I think this happens cause typeahead just gives array and doesn't store value anywhere. I dont want to create separate table for array.
i found cookie might be the option. But dont know how to go with it as never tried cookie. Also, what is the best solution on this cookie or anything else? Has anybody done this?
Please help!
Update:
I used cookie for one city in controller. My home controller's index action is:
def index
#categories = Category.all
cookies[:search] = "Mumbai"
end
And modified index page form for search to:
<%= text_field_tag 'search', nil, :placeholder =>"#{cookies[:search]} ", :autocomplete => :off, :'data-provide' => "typeahead", :id=>"citysearch" %>
This shows city Mumbai on back button click. But i dont want city to be fixed. It should be user entered value. Can anyone tell how to do it?