I made a new controller to integrate Stripe Checkout, but it's acting as its own entire website and none of my partials (_header, _footer) are displaying.
The current setups is straight from their tutorial https://stripe.com/docs/checkout/guides/rails
Charges Controller
class ChargesController < ApplicationController
def new
end
def create
# Amount in cents
#amount = 100
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken],
:plan => "tier1",
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
end
Routes
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get 'charges' => 'charges#new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :charges
end
/layouts/charges.html.erb (controller view)
<% provide(:title, 'Edit Billing') %>
<%= yield %>
views/charges/new.html.erb (form with Stripe Checkout on it)
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $1.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="Tier 1 Monthly Subscription"
data-amount="100"
data-locale="auto"
data-image="images/marketplace.png"
data-name="Pallet Tier 1"
data-description="Tier 1 Monthly Subscription"
data-label="Join" >
</script>
views/charges/create.html.erb (redirect after successful charge)
<% provide(:title, 'Edit Billing') %>
<h2>Thanks, you paid <strong>$1.00</strong>!</h2>
Related
My standard current_user.role is not changing to premium within my ChargesController create action, even though my create (post) action follows through. My downgrade action within my UsersController performs properly and changes a users role just find, using the same current_user.role = 'new_role_here' just fine. I'm curious as to why my current_user.role is not changing upon a successful save message? Any help would be appreciated, thanks.
ChargesController Using stripe
before_action :require_sign_in
def new
#stripe_btn_data = {
key: Rails.configuration.stripe[:publishable_key].to_s,
description: "BigMoney Membership - #{current_user.email}",
amount: 10
}
end
def create
#amount = 500
customer = Stripe::Customer.create(
email: current_user.email,
source: params[:stripeToken]
)
charge = Stripe::Charge.create(
customer: customer.id,
amount: #amount,
description: "BigMoney Membership - #{current_user.email}",
currency: 'usd'
)
current_user.role = 'premium'
if current_user.save!
flash[:notice] = "Thanks for all the money, #{current_user.email}! changed to premium member, #{current_user.role}"
redirect_to wikis_path # or wherever
end
# Stripe will send back CardErrors, with friendly messages
# when something goes wrong.
# This `rescue block` catches and displays those errors.
rescue Stripe::CardError => e
flash[:alert] = e.message
redirect_to new_charge_path
end
end
charges/new.html.erb
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $5.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount="500"
data-locale="auto"></script>
<% end %>
routes
Rails.application.routes.draw do
get 'charges/create'
get "log_in" => 'sessions#new', :as => "log_in"
get "log_out" => 'sessions#destroy', :as => "log_out"
get "sign_up" => "users#new", :as => "sign_up"
get "my_account" => "users#show", :as => "my_account"
get 'users/confirm' => 'users#confirm'
root :to => "sessions#new"
resources :users
resources :sessions
resources :wikis
resources :charges, only: [:new, :create]
I figured out that that if I used current_user.update_attribute(:role, 'premium') instead of current_user.role = 'premium it worked.
I'm trying to have subscribers unsubscribe via a delete form where they enter their email and then submit and then delete the subscription. Not sure why this isn't working. I've tried a number of things via my controller. Would love some suggestions or alternatives.
Currently getting this error
Couldn't find Subscription without an ID
Subscriptions controller
def destroy
#subscriptions = Subscription.all
#subscription = #subscriptions.find(params[:id])
if #subscription.email == params[:email]
#subscription.destroy
flash[:notice] = "You have been unsubscribed"
redirect_to root_path
else
end
end
Routes
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get 'users/new'
resources :posts
root 'static_pages#new_home'
resources :subscriptions, only: [:new, :create, :edit, :update, :show, :destroy]
get '/home' => 'static_pages#home'
get 'subscriptions' => 'subscriptions#show'
delete 'subscriptions' => 'subscriptions#destroy'
get '/about' => 'static_pages#about'
get '/author' => 'static_pages#author'
get '/contact' => 'static_pages#contact'
get '/book' => 'static_pages#book'
get '/signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts do
resources :comments
end
end
Unsubscribe partial in subscriptions view
<%= form_for #subscription, method: :delete do |f| %>
<div class="row">
<h2>Unsubscribe</h2>
</div>
<div class="row newsletter">
<form method="post" action="#" class="contact-form">
<div class="row email-label">
<%= f.label :email, :class => 'label-email' %>
<%= f.email_field :email %>
</div>
<div class="row">
<%= f.submit 'Unsubscribe', :class => 'btn-full-subscribe' %>
</div>
</form>
</center
<% end %>
I've tried a number of things with no luck. Help would be appreciated. Thanks. Subscriptions are not related to User model.
This line #subscription = #subscriptions.find(params[:email]) is causing the issue.
Try #subscription = #subscriptions.find_by_email(params[:email]) or better yet, use the id #subscription = #subscriptions.find(params[:id]).
ActiveRecord::find only uses ids: http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find.
Problem is here
#subscription = #subscriptions.find(params[:email])
because find uses id to fetch the record if you want to fetch by any other field use find_by_"attribute_name", Replace it with
#subscription = Subscription.find_by_email(params[:email])
Or no need to write down extra code. No need to fetch all the subscriptions. To delete use
#subscription = Subscription.find_by_email(params[:email])
or
#subscription = Subscription.find(params[:id])
if #subscription.destroy
flash[:notice] = "You have been unsubscribed"
redirect_to root_path
end
This code is all based on the Ruby on Rails tutorial by Michael Hartl. I created an assigned action in my users_controller with
def assign
puts "in assign..."
#scout = User.find(params[:follower_id])
#der = User.find(params[:followed_id])
end
I realize this doesn't do anything currently, but in the _follow partial, I have
<%= form_for #user, url: { action: "assign" } do |f| %>
<div><%= hidden_field_tag :follower_id, #user.id %></div>
<%= f.label :followed_id, "Assign #{#user.name} to" %>
<%= f.collection_select :following, #ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
But I'm getting the error No route matches {:action=>"assign", :controller=>"users", :id=>"4"}. I'm new to rails so this could just be a stupid question. What am I doing wrong? Do I need to modify my routes.rb file? Also, if I tried <%= form_for #user do |f| %>, how does the controller know which action to use? Is it based on the action displaying the form?
Edit: my routes.rb file is
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
Edit 2: the HTML generated by #Rich's form_for block is
<form class="edit_user" id="edit_user_5" action="/users/5/assign" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="authenticity_token" value="...">
<label for="user_followed_id">Assign Mr. Marley Muller to</label>
<select name="user[following]" id="user_following">
<option value="1">Example User</option>
<option value="2">Matthew Swartz</option>
<option value="3">Joseph Swartz</option>
</select>
<input type="submit" name="commit" value="Assign" class="btn btn-primary">
</form>
Which makes sense why I'm currently getting a can't find id error since it's not sending a real id (edit_user_5)
Edit 3: Here are the parameters being passed for the request
Started PATCH "/users/6/assign" for 68.100.59.128 at 2015-12-15 03:54:39 +0000
Processing by UsersController#assign as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "user"=>{"following"=>"2"}, "commit"=>"Assign", "id"=>"6"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 6]]
Unpermitted parameter: following
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 9ms (ActiveRecord: 1.0ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=):
app/controllers/users_controller.rb:69:in `assign'
Edit 4: With the following routes file,
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
and the following form for assigning one user to follow another,
<%= form_for #user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{#user.name} to" %>
<%= f.collection_select :following, #ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
I am getting the ActiveRecord::RecordNotFound in UsersController#assign error of Couldn't find User with 'id'=, but with the following parameters:
{"utf8"=>"✓",
"_method"=>"patch",
"authenticity_token"=>"...",
"user"=>{"following"=>"3"},
"commit"=>"Assign",
"id"=>"4"}
The ID's are correct: "user"=>{"following"=>"3"} and "id"=>"4", I believe I am just trying to access them incorrectly. This is the assigns action in the users controller:
def assign
#scout = User.find(params[:id])
#der = User.find(params_hash[:followed_id])
# make scout follow der here
redirect_to #scout
end
Thoughts?
In your routes.rb file you have to define the route for the specific controller action like that:
resource :users do
member do
post 'assign' # change the request method to 'put'
end
end
It will then generate a route for your assign method in the controller like: users/4/assign
For more info visit guides
To add to Emu's answer, the code you have won't work...
def assign
puts "in assign..."
#scout = User.find(params[:follower_id])
#der = User.find(params[:followed_id])
end
The params you'd get from the form_for object would be structured as:
params[:user][:follower_id]
params[:user][:followed_id]
You'd have to use the following:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def assign
#scout = User.find params[:id]
#der = User.find params_hash[:followed_id]
end
private
def params_hash
params.require(:user).permit(:followed_id)
end
end
You could also get rid of the hidden field in your form if you're using the same #user object to populate it:
<%= form_for #user, url: { action: "assign" } do |f| %>
<%= f.label :followed_id, "Assign #{#user.name} to" %>
<%= f.collection_select :following, #ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
Update
You need to include :assign in your routes:
Rails.application.routes.draw do
root 'static_pages#home'
resources :static_pages, only: [], path: "" do
collection do
get :help, :about, :contact
end
end
resources :sessions, only: [:new, :create, :destroy], path_names: {new: "login", create: "login", destroy: "logout"}
resources :users, path_names: { new: "signup" } do
member do
get :following, :followers, :assign
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
It turns out it was a combination of Emu and Rich's answers. The form in the HTML is
<%= form_for #user, url: { action: "assign" } do |f| %>
<%= f.label :follower_id, "Assign #{#user.name} to" %>
<%= f.collection_select :following, #ders, :id, :name, prompt: true %>
<%= f.submit "Assign", class: "btn btn-primary" %>
<% end %>
With controller action
def assign
#scout = User.find(params[:id])
#der = User.find(params[:user][:following])
#scout.follow(#der)
redirect_to #scout
end
and routes as
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
match :assign, to: :assign, via: [:post, :patch]
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
end
I am going through Michael Hartl's tutorial and am stuck on the very end of Chapter 10. When I click "I forgot my password" - the email sends successfully. However the "Reset Password" link, directs me back to the home page, rather then the reset password page.
In views/password_resets/edit.html.rb
<% provide(:title, 'Reset password') %>
<h1>Reset password</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user, url: password_reset_path(params[:id])) do |f| %>
<%= render 'shared/error_messages' %>
<%= hidden_field_tag :email, #user.email %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Update password", class: "btn btn-primary" %>
<% end %>
</div>
</div>
mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "noreply#example.com"
def account_activation(user)
#user = user
mail to: user.email, subject: "Account activation"
end
en.user_mailer.password_reset.subject
#
def password_reset(user)
#user = user
mail to: user.email, subject: "Password reset"
end
end
views/user_mailer/password_reset.html.erb
<h1>Password reset</h1>
<p>To reset your password click the link below:</p>
<%= link_to "Reset password", edit_password_reset_url(#user.reset_token,
email: #user.email) %>
<p>This link will expire in two hours.</p>
<p>
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>
In models/users.rb
# Sets the password reset attributes.
def create_reset_digest
self.reset_token = User.new_token
update_attribute(:reset_digest, User.digest(reset_token))
update_attribute(:reset_sent_at, Time.zone.now)
end
# Sends password reset email.
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
# Returns true if a password reset has expired.
def password_reset_expired?
reset_sent_at < 2.hours.ago
end
config/routes.rb
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
Please let me know if there is anything else I can provide to help.
I spent hours trying to figure this out, and finally discovered why I was having this problem. First of all, take a look at the routes file for this chapter Michael has on github here: github
My problem was simple. I hadn't authenticated the user for which I was trying to reset the password, thus I wasn't a valid user and couldn't reset my password.
Mr. Hartl has his routes like so:
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
I think your extra entries are short-circuiting the intended routes.
I'm trying to get a user from his profile (user_path) can register his vehicles (new_user_car_path). but i got this error:
Routing Error
No route matches {:action=>"new", :controller=>"cars"}
for that i have the next routes.rb
Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :users do
resources :cars, :only => [:new, :create, :edit, :destroy]
end
here is part of the user_path
<div class="container">
<fieldset>
<h1><%= #user.email %></h1>
<br>
<h2>options</h2>
<p>
<strong>new car registration</strong>
<%= link_to "Sign up now!", new_user_car_path, :class => "btn btn-primary" %>
</p>
<p>
<strong>all cars view</strong>
</p>
</fieldset>
</div> <!-- /container -->
and my CarsController
class CarsController < ApplicationController
def new
#car = Car.new
end
def create
#car = current_user.cars.build(params[:car])
if #car.save
redirect_to current_user, :flash => { :success => "car created!" }
else
redirect_to new_user_car_path, :flash => { :success => "sorry try again" }
end
end
end
you need to pass information to route, so that it can determine where to go.
if you want to create a new car for a user, you need to pass the user. otherwise rails will not know where to get the user from... which is kind of logical if you think about it for more then a second...
new_user_car_path(user)