I am writing a basic index page where I want to show a user a list of their credits cards if they have any, else I want to show them an add credit card button.
I know the statement is something like if current_user >1 credit_card
I have user model using devise and a credit_card model and basically I want to show the following.
<% if current_user has atleast 1(>1) credit_card>
show this
<% else %>
show that
<% end %>
Credit_Cards Controller
class CreditCardsController < ApplicationController
before_action :authenticate_user!
before_action :set_credit_card, only: [:show, :edit, :update, :destroy]
# GET /credit_cards
# GET /credit_cards.json
def index
#credit_cards = current_user.credit_cards
#credit_card_debt = current_user.credit_cards.sum(:card_balance)
#credit_limit = current_user.credit_cards.sum(:card_limit)
#available_credit = current_user.credit_cards.sum(:card_limit) - current_user.credit_cards.sum(:card_balance)
end
# GET /credit_cards/1
# GET /credit_cards/1.json
def show
end
# GET /credit_cards/new
def new
#credit_card = current_user.credit_cards.build
end
# GET /credit_cards/1/edit
def edit
end
# POST /credit_cards
# POST /credit_cards.json
def create
#credit_card = current_user.credit_cards.new(credit_card_params)
respond_to do |format|
if #credit_card.save
format.html { redirect_to #credit_card, notice: 'Credit card was successfully created.' }
format.json { render :show, status: :created, location: #credit_card }
else
format.html { render :new }
format.json { render json: #credit_card.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /credit_cards/1
# PATCH/PUT /credit_cards/1.json
def update
respond_to do |format|
if #credit_card.update(credit_card_params)
format.html { redirect_to #credit_card, notice: 'Credit card was successfully updated.' }
format.json { render :show, status: :ok, location: #credit_card }
else
format.html { render :edit }
format.json { render json: #credit_card.errors, status: :unprocessable_entity }
end
end
end
# DELETE /credit_cards/1
# DELETE /credit_cards/1.json
def destroy
#credit_card.destroy
respond_to do |format|
format.html { redirect_to credit_cards_url, notice: 'Credit card was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_credit_card
#credit_card = CreditCard.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def credit_card_params
params.require(:credit_card).permit(:card_name, :card_provider, :points_provider, :interest_rate, :card_balance, :card_limit, :user_id )
end
end
Credit Card Model
class CreditCard < ApplicationRecord
belongs_to :user
def credit_available
card_limit - card_balance
end
def annual_interest
card_balance * interest_rate
end
def minimum_payment
n = card_balance / 100
b = n * 1
i = card_balance * interest_rate
c = i / 12
c + b
end
def total_monthly_payment
total_monthly_payment = 0
#total_monthly_payment = total_monthly_payment + current_user.credit_cards.minimum_payment.sum
end
end
You have used this #credit_cards = current_user.credit_cards the line in the index action that's why you don't need to use current_user on the index.html.erb file because above line make sure you have credit card or no then your HTML file looks like
<% if #credit_cards.count >= 1 %>
show this
<% else %>
show that
<% end %>
Or you can use something like this
<% if #credit_cards.any? %>
show this
<% else %>
show that
<% end %>
Or you can use something like this
<% if current_user.credit_cards.count >= 1 %> #=> or current_user.credit_cards.any?
show this
<% else %>
show that
<% end %>
That's time you don't need this #credit_cards = current_user.credit_cards from index action
Here is you can see some if else basic explanation of Ruby The Bastards Book of Ruby
Related
sorry for this question but I'm struggling with this issue for hours now and can't find the answer anywhere.
Here is the thing, I have a rails app with "Reservation" and "Space" models with the following relations:
class Reservation < ActiveRecord::Base
belongs_to :space
belongs_to :user
end
class Space < ActiveRecord::Base
belongs_to :condo
has_many :reservations
end
When the user creates a new Reservation, in the form he gets to choose from a dropdown (f.select) the spaces available for him. The f.select in the form look like this:
<div class="field">
<%= #user_spaces = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(#user_spaces, :id, :name), :prompt => "Select space"
%>
</div>
That select it supose to assign a value to the key "space_id" in the Reservation that is being created (column's table is created). But when I check the last reservation in Rails console, space_id value is "nil". What am I doing wrong?
Thank you very much for your help
Reservation controller file:
class ReservationsController < ApplicationController
before_action :set_reservation, only: [:show, :edit, :update, :destroy]
# GET /reservations
# GET /reservations.json
def index
#reservations = Reservation.all
end
# GET /reservations/1
# GET /reservations/1.json
def show
end
# GET /reservations/new
def new
#reservation = Reservation.new
end
# GET /reservations/1/edit
def edit
end
# POST /reservations
# POST /reservations.json
def create
#reservation = Reservation.new(reservation_params)
#user = current_user.id
#reservation.user_id = #user
respond_to do |format|
if #reservation.save
format.html { redirect_to #reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: #reservation }
else
format.html { render :new }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservations/1
# PATCH/PUT /reservations/1.json
def update
respond_to do |format|
if #reservation.update(reservation_params)
format.html { redirect_to #reservation, notice: 'Reservation was successfully updated.' }
format.json { render :show, status: :ok, location: #reservation }
else
format.html { render :edit }
format.json { render json: #reservation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservations/1
# DELETE /reservations/1.json
def destroy
#reservation.destroy
respond_to do |format|
format.html { redirect_to reservations_url, notice: 'Reservation was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reservation
#reservation = Reservation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def reservation_params
params.require(:reservation).permit(:eventdate)
end
end
Space controller file:
class SpacesController < ApplicationController
before_action :set_space, only: [:show, :edit, :update, :destroy]
# GET /spaces
# GET /spaces.json
def index
#spaces = Space.all
end
# GET /spaces/1
# GET /spaces/1.json
def show
end
# GET /spaces/new
def new
#space = Space.new
end
# GET /spaces/1/edit
def edit
end
# POST /spaces
# POST /spaces.json
def create
#space = Space.new(space_params)
respond_to do |format|
if #space.save
format.html { redirect_to #space, notice: 'Space was successfully created.' }
format.json { render :show, status: :created, location: #space }
else
format.html { render :new }
format.json { render json: #space.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /spaces/1
# PATCH/PUT /spaces/1.json
def update
respond_to do |format|
if #space.update(space_params)
format.html { redirect_to #space, notice: 'Space was successfully updated.' }
format.json { render :show, status: :ok, location: #space }
else
format.html { render :edit }
format.json { render json: #space.errors, status: :unprocessable_entity }
end
end
end
# DELETE /spaces/1
# DELETE /spaces/1.json
def destroy
#space.destroy
respond_to do |format|
format.html { redirect_to spaces_url, notice: 'Space was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_space
#space = Space.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def space_params
params.require(:space).permit(:name)
end
end
And full Reservation Form:
<%= form_for(#reservation) do |f| %>
<% if #reservation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#reservation.errors.count, "error") %> prohibited this reservation from being saved:</h2>
<ul>
<% #reservation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :eventdate %><br>
<%= f.date_select :eventdate %>
</div>
<div class="field">
<%= #user = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(#user, :id, :name), :prompt => "Select space"
%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
pretty sure you need to permit the space_id attribute in your strong params.
def reservation_params
params.require(:reservation).permit(:eventdate, :space_id)
end
whats happening is that when you go to create a reservation, youre passing in set of params, that is the output of reservation_params
#reservation = Reservation.new(reservation_params)
if space_id is not being permitted in your strong params, then it will be nil when created.
if this is not the issue, can you post what params are getting to the server, and what the output of reservation_params are.
I have a list of posts and all of them can be votable. I can count the number of votes for each post, but how can I count the number for all of them? I'm using the gem acts_as_votable for the voting system
I count the number of posts like this: <%= performance_indicator.improvement_actions.count %>
this is my "posts" controller:
class ImprovementActionsController < ApplicationController
before_action :set_improvement_action, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :authenticate_user!, except: [:index, :show]
# GET /improvement_actions
# GET /improvement_actions.json
def index
end
# GET /improvement_actions/1
# GET /improvement_actions/1.json
def show
end
# GET /improvement_actions/new
def new
#performance_indicator = PerformanceIndicator.find(params[:performance_indicator_id])
#improvement_action = ImprovementAction.new
#comment = #improvement_action.comments.new
end
# GET /improvement_actions/1/edit
def edit
end
# POST /improvement_actions
# POST /improvement_actions.json
def create
#performance_indicator = PerformanceIndicator.find(params[:performance_indicator_id])
#improvement_action = #performance_indicator.improvement_actions.create(params[:improvement_action].permit(:description))
#improvement_action.user_id = current_user.id if current_user
#improvement_action.save
respond_to do |format|
if #improvement_action.save
format.html { redirect_to #performance_indicator }
format.json { render :show, status: :created, location: #improvement_action }
else
format.html { render :new }
format.json { render json: #improvement_action.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /improvement_actions/1
# PATCH/PUT /improvement_actions/1.json
def update
respond_to do |format|
if #improvement_action.update(improvement_action_params)
format.html { redirect_to performance_indicator_path(#improvement_action.performance_indicator), notice: 'Improvement action was successfully updated.' }
format.json { render :show, status: :ok, location: #performance_indicator }
else
format.html { render :edit }
format.json { render json: #improvement_action.errors, status: :unprocessable_entity }
end
end
end
def destroy
#improvement_action.destroy
respond_to do |format|
format.html { redirect_to performance_indicator_path(#improvement_action.performance_indicator), notice: 'Improvement action was successfully deleted.' }
format.json { head :no_content }
end
end
#upvote_from user
#downvote_from user
def upvote
#improvement_action.upvote_from current_user
# respond_to do |format|
# format.html { redirect_to :back }
# format.js { render layout: false }
# end
redirect_to :back
end
def downvote
#improvement_action.downvote_from current_user
redirect_to :back
##respond_to do |format|
# format.html { redirect_to :back }
# format.js { render layout: false }
# end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_improvement_action
#improvement_action = ImprovementAction.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def improvement_action_params
params.require(:improvement_action).permit(:description, :upvote, :downvote, :score, :active)
end
end
And I want to put here the counter:
<% #performance_indicators.each do |performance_indicator| %>
<p> Number of votes </p>
<% end %>
Do you have any cache column for votes in the ImprovementAction model? (https://github.com/ryanto/acts_as_votable#caching)
It is for keeping total amount of votes for each post. You should have it to do the calculation you want:
# in this case the cache column is :cached_votes_total
sum = performance_indicator.improvement_actions.sum(:cached_votes_total)
This will make only one database request.
Never do like this:
# DON'T DO THIS !!!
performance_indicator.improvement_actions.inject(0) {|sum, post| sum + post.votes_for.size }
This will have to load and instantiate all the records and make a separate request for each of them to retrieve their votes. Very BAD solution !
I'm having a hard time implementing a checkout process where once at the cart, user can checkout and have order shipped. Maybe I need an order model and transaction controller?
I'm just not sure how to set those up. Currently the cart works and can be cleared as well as have items be added, its just I'm not sure how to implement a checkout and order system.
Idea is: User at cart clicks the checkout button, then is taken to checkout where he/she can input payment information, then taken back to products page. Issue is I'm not sure again how to connect the cart to the checkout and payment process into one simple easy system.
Any help would be appreciated, I'm still very new at this. Thank you.
class CartController < ApplicationController
before_action :authenticate_user!, except: [:index]
def add
id = params[:id]
if session[:cart] then
cart = session[:cart]
else
session[:cart] = {}
cart = session[:cart]
end
if cart[id] then
cart[id] = cart[id] + 1
else
cart[id] = 1
end
redirect_to :action => :index
flash[:notice] = 'added to cart'
end
def clearCart
session[:cart] = nil
redirect_to :action => :index
flash[:notice] = 'cart cleared'
end
def index
if session[:cart] then
#cart = session[:cart]
else
#cart = {}
end
end
end
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def index
#products = Product.all
end
def show
end
def new
#product = Product.new
end
def edit
end
def create
#product = Product.new(product_params)
respond_to do |format|
if #product.save
format.html { redirect_to #product, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: #product }
else
format.html { render :new }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product, notice: 'Product was successfully updated.' }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
def destroy
#product.destroy
respond_to do |format|
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_product
#product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:title, :description, :price, :category, :subcategory)
end
end
class Product < ActiveRecord::Base
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
views/cart/index.html.erb
<h1>Your Cart</h1>
<% if #cart.empty? %>
<p> Your cart is currently empty</p>
<% else %>
<%= link_to 'Empty your Cart', cart_clear_path %>
<br><br>
<% end %>
<% total = 0 %>
<ul>
<% #cart.each do | id, quantity | %>
<% product = Product.find_by_id(id) %>
<li>
<%= link_to product.title, product %>
<p><%= product.description %></p>
<p><%= number_to_currency product.price %></p>
<p>Quantity: <%= quantity %></p>
</li>
<% total += quantity * product.price %>
<% end %>
<p><b><%= number_to_currency total, :unit => '$' %> </b></p>
</ul>
What I've tried below
order.rb
class Order < ActiveRecord::Base
belongs_to :cart
end
cart.rb
class Cart < ActiveRecord::Base
has_many :line_items
has_one :order
end
Orders_Controller.rb
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
# GET /orders
# GET /orders.json
def index
#orders = Order.all
end
# GET /orders/1
# GET /orders/1.json
def show
end
# GET /orders/new
def new
#order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(order_params)
respond_to do |format|
if #order.save
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render :show, status: :created, location: #order }
else
format.html { render :new }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /orders/1
# PATCH/PUT /orders/1.json
def update
respond_to do |format|
if #order.update(order_params)
format.html { redirect_to #order, notice: 'Order was successfully updated.' }
format.json { render :show, status: :ok, location: #order }
else
format.html { render :edit }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
end
# DELETE /orders/1
# DELETE /orders/1.json
def destroy
#order.destroy
respond_to do |format|
format.html { redirect_to orders_url, notice: 'Order was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_order
#order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:new, :cart_id, :ip_address, :first_name, :last_name, :user_id)
end
end
added Checkout to views/cart/index.html.erb
<%= link_to "Checkout", new_order_path, class: "btn btn-primary" %>
What do I do after this?
you've used a local variable product, not the controller attribute #product. (just add the '#' sign)
my advise to you is to use a gem like shoppe,
you can see example here (demo)
you can also navigate through the models of the gem to see how they did it, maybe it will give you a clear perspective of writing an e-commerce solution.
I have nested resources - users have_many manufacturers and manufacturers have_many lines. I wrote a before_filter to load the #manufacturer so it could go through the rest of the functions in the lines_controller. Problem is, I'm running into issues when I click edit on the lines/show view.
Error: Couldn't find Manufacturer with id=manufacturer_id
77 def load_manufacturer
78 #manufacturer = Manufacturer.find(params[:manufacturer_id])
79 end
So here's what I'm working with:
app/views/lines/show.html.erb
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= #line.name %>
</p>
<p>
<strong>Manufacturer:</strong>
<%= #line.manufacturer_id %>
</p>
<%= link_to 'Edit', edit_manufacturer_line_path(:manufacturer_id,#line) %> |
<%= link_to 'Back', manufacturer_lines_path(#line.manufacturer_id) %>
lines_controller.rb
class LinesController < ApplicationController
before_action :set_line, only: [:show, :edit, :update, :destroy]
before_filter :load_manufacturer
# GET /lines
# GET /lines.json
def index
#lines = Line.all
end
# GET /lines/1
# GET /lines/1.json
def show
end
# GET /lines/new
def new
#manufacturer = Manufacturer.find(params[:manufacturer_id])
#line = #manufacturer.lines.build
end
# GET /lines/1/edit
def edit
end
# POST /lines
# POST /lines.json
def create
#line = #manufacturer.lines.build(line_params)
respond_to do |format|
if #line.save
format.html { redirect_to manufacturer_line_path(#manufacturer, #line), notice: 'Line was successfully created.' }
format.json { render action: 'show', status: :created, location: #line }
else
format.html { render action: 'new' }
format.json { render json: #line.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /lines/1
# PATCH/PUT /lines/1.json
def update
respond_to do |format|
if #line.update(line_params)
format.html { redirect_to manufacturer_line_path(#manufacturer, #line), notice: 'Line was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #line.errors, status: :unprocessable_entity }
end
end
end
# DELETE /lines/1
# DELETE /lines/1.json
def destroy
#line.destroy
respond_to do |format|
format.html { redirect_to manufacturer_lines_url(#manufacturer) }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_line
#line = Line.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def line_params
params.require(:line).permit(:name, :manufacturer_id)
end
def load_manufacturer
#manufacturer = Manufacturer.find(params[:manufacturer_id])
end
end
This:
<%= link_to 'Edit', edit_manufacturer_line_path(:manufacturer_id,#line) %>
Should be
<%= link_to 'Edit', edit_manufacturer_line_path(#manufacturer,#line) %>
What is happening is it is changing the symbol :manufacturer_id to a string and passing it to the route so your route looks like
`/manufacturers/manufacturer_id/lines/###/edit`
when you want
/manufacturers/###/lines/###/edit
I want to create an object with random sequence generated through the follow method. But after clicking the "Generate" button, it creates 2 objects (1 with all attributes null and the other 1 with the value I entered). Can someone tell me where's the problem?
CONTROLLER :
class GeneratorsController < ApplicationController
before_action :set_generator, only: [:show, :edit, :update, :destroy]
after_action :random_generate, only: [:create, :new, :edit]
# GET /generators
# GET /generators.json
def index
#generators = Generator.all
end
# GET /generators/1
# GET /generators/1.json
def show
end
# GET /generators/new
def new
#generator = Generator.new
end
# GET /generators/1/edit
def edit
end
# POST /generators
# POST /generators.json
def create
#generator = Generator.new(generator_params)
respond_to do |format|
if #generator.save
format.html { redirect_to #generator, notice: 'Generator was successfully created.' }
format.json { render action: 'show', status: :created, location: #generator }
else
format.html { render action: 'new' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /generators/1
# PATCH/PUT /generators/1.json
def update
respond_to do |format|
if #generator.update(generator_params)
format.html { redirect_to #generator, notice: 'Generator was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #generator.errors, status: :unprocessable_entity }
end
end
end
# DELETE /generators/1
# DELETE /generators/1.json
def destroy
#generator.destroy
respond_to do |format|
format.html { redirect_to generators_url }
format.json { head :no_content }
end
end
def random_generate
if #generator.choice == 'Randomly'
length = #generator.primer_length
chars = 'ATGC'
seq = ''
length.times { seq << chars[rand(chars.size)] }
#generator.random_primer_generated = seq
end
#generator.save!
end
private
# Use callbacks to share common setup or constraints between actions.
def set_generator
#generator = Generator.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def generator_params
params.require(:generator).permit(:primer_length, :choice, :random_primer_generated)
end
end
VIEW :
<%= form_for (#generator ) do |f| %>
<% if #generator.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#generator.errors.count, "error") %> prohibited this generator from being saved:</h2>
<ul>
<% #generator.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<label>Primer Length</label><br>
<%= f.number_field :primer_length %>
</div>
<label>Selection :</label><br>
<label>Randomly</label>
<%= radio_button_tag(:choice, 'Randomly')%>
<%= button_to('Generate', random_generate_generator_path(#generator))%>
<% end %>
You have an after_action callback which triggers the random_generate action every time you display the new view. That's where your empty object comes from. You shouldn't need this after_action callback on new.