rails 4 stripe payment causing line item invalid error - ruby-on-rails-4

Like others working with Stripe payments, I was getting an error 'Cannot charge a customer that has no active card'. The form was not appending the token because the javascript did not have a matching id in the form, but now I am getting an rails error and the card is not being charged, the customer is created, the error states that there is an invalid line item, when the code to create the customer and charge the card is not run the order is created without error.
Order View
<script type="text/javascript" src="https://js.stripe.com/v2/">
$(function(){
Stripe.setPublishableKey('<%= Rails.configuration.stripe[:PUBLISHABLE_KEY] %>');
});
</script>
<div class = "Power Me" >
<fieldset>
<legend> Please enter your details </legend>
<%= render 'form', object: #object %>
</fieldset>
</div>
Rendered Order Form
<%= form_for(#order, :html => {:id => 'payment-form'}) do |f| %>
<% if #order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% #order.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= "Order Total: #{order_total.to_s}" %>
<%= "Order Currency: #{order_currency.to_s}" %>
</div>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :Address_line_1 %><br>
<%= f.text_area :address_line_1 %>
</div>
<div class="field">
<%= f.label :Address_Line_2 %><br>
<%= f.text_area :address_line_2 %>
</div>
<div class="field">
<%= f.label :City %><br>
<%= f.text_area :address_city %>
</div>
<div class="field">
<%= f.label :Region %><br>
<%= f.text_area :address_state %>
</div>
<div class="field">
<%= f.label :Postcode %><br>
<%= f.text_area :address_zip %>
</div>
<div class="field">
<%= f.label :Country %><br>
<%= f.select :address_country, Order::CC_COUNTRIES, prompt: 'Select the country' %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email, :placeholder => "you#example.com" %>
</div>
<div class="field">
<%= f.label :Payment_Type %><br>
<%= f.select :pay_type, Order::PAYMENT_TYPES, prompt: 'Select a payment method' %>
</div>
<div class="form-row">
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" data-stripe="number" id="number" class="credit-number", placeholder = "**** **** **** ****" pattern="[\d ]*" />
</div>
<div class="form-row">
<label>Security Code/CVC</label>
<input type="text" size="4" autocomplete="off" data-stripe="cvc" id="cvc" class="credit-scurity" placeholder="***" pattern="\d*" />
</div>
<div class="form-row">
<label>Expiration (MM/YYYY)</label>
<input type="text" size="2" data-stripe="exp-month" id="exp-month" class="card-expiry-month" placeholder="MM" pattern="\d*" />
<span> / </span>
<input type="text" size="4" data-stripe="exp-year" id="exp-year" class="card-expiry-year" placeholder="YYYY" pattern="\d*" />
</div>
<div class="actions">
<%= f.submit 'Pay', :class =>"stripe-button" %>
</div>
<% end %>
orders model
class Order < ActiveRecord::Base
# attr_accessor :stripeToken
PAYMENT_TYPES = ["credit card"]
CC_COUNTRIES = ["United Kingdom", "France", "Italy"]
validates :name, :address_line_1, :address_zip, :email, presence: true
# validates :pay_type, inclusion: PAYMENT_TYPES
has_many :line_items, dependent: :destroy
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
end
Orders Controller
class OrdersController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :create]
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
if #cart.line_items.empty?
redirect_to store_url, notice: "Your cart is empty"
return
end
#order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
#order = Order.new(order_params)
#order.add_line_items_from_cart(#cart)
respond_to do |format|
if #order.save
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render action: 'show', status: :created, location: #order }
else
format.html { render action: 'new' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
Stripe.api_key = "sk_test_BsdqHq0SQuPqHIsm46lcpX4v"
#amount = order_total.to_i * 100
token = params[:stripeToken]
=begin
begin
customer = Stripe::Customer.create(
:email => order_params[:email]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => #amount, # amount in cents, again
:currency => order_currency,
:card => token,
:description => order_params[:email]
)
redirect_to root_path
rescue Stripe::CardError => e
#error = e
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 { head :no_content }
else
format.html { render action: '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 }
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(:name, :email, :pay_type, :address_line_1, :address_line_2, :address_city, :address_state, :address_zip, :address_country)
end
end
My Application Helper
module ApplicationHelper
def order_total
total = LineItem.joins(:product).select("sum(line_items.quantity * products.price) as total").where("cart_id = ?", session[:cart_id]).first.total
end
def order_currency
currency = LineItem.joins(:product).joins(:currency).select("currencies.name as iso_name").where("cart_id = ?", session[:cart_id]).first.iso_name
end
# Returns the full title on a per-page basis.
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
def bootstrap_class_for flash_type
{ success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info" }[flash_type] || flash_type.to_s
end
def flash_messages(opts = {})
flash.each do |msg_type, message|
concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do
concat message
end)
end
nil
end
# before filters
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
def correct_user
#user = User.find(params[:id])
redirect_to(root_url) unless current_user?(#user)
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
end
Orders.js
$('#payment-form').submit(function(event) {
var form = $(this);
form.find('button').prop('disabled', true);
Stripe.createToken(form, stripeResponseHandler);
return false;
});

Working Order Form below, you cannot pass fields to Stripe and use them in the rails form to save in the db, its either or (previous example attempted to save address fields in the db and pass them to stripe as part of the transaction validation.
<%= form_for(#order, :html => {:id => 'payment-form'}) do |f| %>
<% if #order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% #order.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= "Order Total: #{order_total.to_s}" %>
<%= "Order Currency: #{order_currency.to_s}" %>
</div>
<div class="form-row">
<label>Full Name</label>
<input type="text" size="20" autocomplete="off" data-stripe="name" />
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email, :placeholder => "you#example.com" %>
</div>
<div class="field">
<%= f.label :Payment_Type %><br>
<%= f.select :pay_type, Order::PAYMENT_TYPES, prompt: 'Select a payment method', :selected => "credit card" %>
</div>
<div class="form-row">
<label>Address Line 1</label>
<input type="text" size="20" autocomplete="off" data-stripe="address_line1" />
</div>
<div class="form-row">
<label>Address Line 2</label>
<input type="text" size="20" autocomplete="off" />
</div>
<div class="form-row">
<label>Address City</label>
<input type="text" size="20" autocomplete="off" />
</div>
<div class="form-row">
<label>Address State</label>
<input type="text" size="20" autocomplete="off" />
</div>
<div class="form-row">
<label>Zip/Postcode</label>
<input type="text" size="20" autocomplete="off" data-stripe="address_zip" />
</div>
<div class="form-row">
<label>Country</label>
<input type="text" size="20" autocomplete="off" data-stripe="address_country" />
</div>
<div class="form-row">
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" data-stripe="number" placeholder = "**** **** **** ****" pattern="[\d ]*" />
</div>
<div class="form-row">
<label>Security Code/CVC</label>
<input type="text" size="4" autocomplete="off" data-stripe="cvc" placeholder="***" pattern="\d*" />
</div>
<div class="form-row">
<label>Expiration (MM/YYYY)</label>
<input type="text" size="2" data-stripe="exp-month" placeholder="MM" pattern="\d*" />
<span> / </span>
<input type="text" size="4" data-stripe="exp-year" placeholder="YYYY" pattern="\d*" />
</div>
<div class="actions">
<%= f.submit 'Pay', :class => "button" %>
</div>
<% end %>
The form required the id referenced inthe javascript below
var stripeResponseHandler = function(status, response) {
var $form = $('#payment-form');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// token contains id, last4, and card type
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and re-submit
$form.get(0).submit();
}
};
jQuery(function($) {
$('#payment-form').submit(function(e) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
Order Model, fields removed
class Order < ActiveRecord::Base
PAYMENT_TYPES = ["credit card"]
CC_COUNTRIES = ["United Kingdom", "France", "Italy"]
validates :email, presence: true
validates :pay_type, inclusion: PAYMENT_TYPES
has_many :line_items, dependent: :destroy
def add_line_items_from_cart(cart)
cart.line_items.each do |item|
item.cart_id = nil
line_items << item
end
end
end
Application header
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
$(function(){
Stripe.setPublishableKey('<%= Rails.configuration.stripe[:publishable_key] %>');
});
</script>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
Order Controller
class OrdersController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:new, :create]
before_action :set_order, only: [:show,:edit, :update, :destroy]
# before_action :admin_user, only: [:destroy, :show]
# 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
if #cart.line_items.empty?
redirect_to store_url, notice: "Your cart is empty"
return
end
#order = Order.new
end
# GET /orders/1/edit
def edit
end
# POST /orders
# POST /orders.json
def create
begin
#order = Order.new(order_params)
#order.add_line_items_from_cart(#cart)
Stripe.api_key = "sk_test_xxxxxxxxxxxxxxx"
#amount = order_total.to_i * 100
token = params[:stripeToken]
# Create a Customer
customer = Stripe::Customer.create(
:description => order_params[:email],
:card => token,
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => #amount, # amount in cents, again
:currency => order_currency,
# :card => token,
:description => order_params[:email]
)
respond_to do |format|
if #order.save
puts "saving now"
Cart.destroy(session[:cart_id])
session[:cart_id] = nil
format.html { redirect_to #order, notice: 'Order was successfully created.' }
format.json { render action: 'show', status: :created, location: #order }
else
format.html { render action: 'new' }
format.json { render json: #order.errors, status: :unprocessable_entity }
end
end
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to root_path
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 { head :no_content }
else
format.html { render action: '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 }
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(:email, :pay_type)
end
end

Related

in my rails app, I need to display a users listings when a customer clicks the users name on the homepage

I have an index page that shows all listings from multiple users. I am trying to create a page that dynamically shows a specific users listings when a shopper clicks on the sellers name on the homepage. I created a seller id in my listings controller, created the route and the show view. The problem I am having is building the logic in the controller that will show posts only from the specific user that is chosen by a shopper on the homepage. I hope this makes sense and any help would be awesome.
listings controller
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, only: [:seller, :new, :create, :edit, :update, :destroy]
before_filter :check_user, only: [:edit, :update, :destroy]
def seller
#listings = Listing.where(user: current_user).order("created_at DESC")
end
def seller_id
#??????
end
# GET /listings
# GET /listings.json
def index
if params[:category].blank?
#listings = Listing.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 16)
else
#category_id = Category.find_by(name: params[:category]).id
#listings = Listing.where(category_id: #category_id).order("created_at DESC").paginate(:page => params[:page], :per_page => 16)
end
end
# GET /listings/1
# GET /listings/1.json
def show
end
# GET /listings/new
def new
#listing = Listing.new
end
# GET /listings/1/edit
def edit
end
# POST /listings
# POST /listings.json
def create
#listing = Listing.new(listing_params)
#listing.user_id = current_user.id
if current_user.recipient.blank?
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]
recipient = Stripe::Recipient.create(
:name => current_user.name,
:type => "individual",
:bank_account => token
)
current_user.recipient = recipient.id
current_user.save
end
respond_to do |format|
if #listing.save
format.html { redirect_to #listing, notice: 'Listing was successfully created.' }
format.json { render :show, status: :created, location: #listing }
else
format.html { render :new }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /listings/1
# PATCH/PUT /listings/1.json
def update
respond_to do |format|
if #listing.update(listing_params)
format.html { redirect_to #listing, notice: 'Listing was successfully updated.' }
format.json { render :show, status: :ok, location: #listing }
else
format.html { render :edit }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
# DELETE /listings/1
# DELETE /listings/1.json
def destroy
#listing.destroy
respond_to do |format|
format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
#listing = Listing.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:name, :category_id, :description, :price, :image)
end
def check_user
if current_user != #listing.user
redirect_to root_url, alert: "Sorry, this listing belongs to someone else"
end
end
end
routes
get 'seller_id' => "listings#seller_id"
and my index page that I want to be able to link the listing
<div class="jumbotron">
<h1>Demo Site </h1>
<h2>Discover one-of-a-kind items</h2>
</div>
<div class="center">
<div class="row">
<% #listings.each do |listing| %>
<div class="col-md-3">
<div class="thumbnail">
<%= link_to image_tag(listing.image.url), listing %>
<div class="caption">
<h3><%= listing.name %></h3>
<p><%= number_to_currency(listing.price) %></p>
<p><%= "Sold by #{listing.user.name}" %></p>
<P><%= link_to "Shop Page", seller_id_path %></P>
</div>
</div>
</div>
<% end %>
</div>
</div>
<br>
<div class="center">
<%= will_paginate #posts, renderer: BootstrapPagination::Rails %>
</div>
<% if user_signed_in? %>
<div class="right">
<%= link_to new_listing_path, class: "btn btn-primary", data: { no_turbolink: true } do %>
<i class="glyphicon glyphicon-plus"></i> New Listing
<% end %>
</div>
<% end %>
<br>
and the seller_id page
<table class="table table-striped table-bordered">
<tr>
<th class="center">Image</th>
<th class="center">Name</th>
<th class="center">Description</th>
<th class="center">Category</th>
<th class="center">Price</th>
</tr>
<% #listings.each do |listing| %>
<tr>
<td><%= image_tag listing.image.url(:thumb) %> </td>
<td><%= listing.name %> </td>
<td><%= listing.description %> </td>
<td><%= listing.category.name %> </td>
<td><%= number_to_currency(listing.price) %> </td>
</tr>
<% end %>
</table>
<br>
<br>
You would need something like this, but this is only for example.
routes
resources :sellers do
resources :listings
end
listings controller
def index
#seller = Seller.find(params[:seller_id])
#listings = #seller.listings
end
url
yourapp.com/sellers/2/listings

render multiple objects to a users profile page

I am trying to show the 'status' & 'project' from an '#user' on that users profile page, I believe I have the correct relationships etc but the error I am getting is that I cannot call render multiple times in one action.
class ProfilesController < ApplicationController
def show
#user = User.find_by_profile_name(params[:id])
if #user
#statuses = #user.statuses.all
render actions: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
#user = User.find_by_profile_name(params[:id])
if #user
#projects = #user.projects.all
render actions: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
end
What is the best way to express the above, to render both items on the users profile page without calling render twice?
This is my view:
<div class="container">
<div class="page-header">
<h1><%= "Hi " + #user.first_name + "!" %>
</div>
<div class="col-md-6 col-md-offset-3">
<%= link_to "Create Project", new_project_path, class: "btn btn-success btn-block" %>
</div>
<% if #statuses %>
<% #statuses.each do |status| %>
<div class="well">
<%= status.content %>
<hr />
<%= link_to time_ago_in_words(status.created_at), status_path(status) %> ago
</div>
<% end %>
<% end %>
<% if #projects %>
<% #projects.each do |project| %>
<div class="well">
<%= project.title %>
<hr />
<%= link_to time_ago_in_words(project.created_at), projects_path(project) %> ago
</div>
<% end %>
<% end %>
</div>
Thanks for your time!
Try as below.
class ProfilesController < ApplicationController
def show
#user = User.find_by_profile_name(params[:id])
if #user
#statuses = #user.statuses.all
#projects = #user.projects.all
render action: :show
else
render file: 'public/404', status: 404, formats: [:html]
end
end
end

Forms, nested resources and routing errors Rails 4

Rails newbie here. I have created a basic website that tracks events during a shift. I have created the models and nested them. However, I do not understand the connections completely.
When I create the nested event from a form linked to a shift I get the following error:
No route matches [POST] "/shifts/events" on url http://localhost:3000/shifts//events
should be http://localhost:3000/shifts/3/events/1 I think
_form.html.erb
<%= form_for shift_events_path(#shift) do |f| %>
<% if #event.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#event.errors.count, "error") %> prohibited </h2>
<ul>
<% #event.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :time_s %><br>
<%= f.time_select :time_s %>
</div>
<div class="field">
<%= f.label :time_f %><br>
<%= f.time_select :time_f %>
</div>
<div class="field">
<%= f.label :odometre %><br>
<%= f.number_field :odometre %>
</div>
<div class="field">
<%= f.label :location %><br>
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :activity %><br>
<%= f.text_field :activity %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description, rows: 4 %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
roots.rb
resources :shifts do
resources :events#,
end
events_controller.eb
class EventsController < ApplicationController
before_filter :get_shift
before_action :set_event, only: [:show, :edit, :update, :destroy]
# GET /events
# GET /events.json
def index
#events = #shift.events
end
# GET /events/1
# GET /events/1.json
def show
end
# GET /events/new
def new
#event = #shift.events.build
end
# GET /events/1/edit
def edit
end
# POST /events
# POST /events.json
def create
#event = #shift.events.build(event_params)
respond_to do |format|
if #event.save
format.html { redirect_to shift_event_path, notice:
'Event was successfully created.' }
format.json { render action: 'show', status: :created, location: #event }
else
format.html { render action: 'new' }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /events/1
# PATCH/PUT /events/1.json
def update
respond_to do |format|
if #event.update(event_params)
format.html { redirect_to shift_event_path, notice:
'Event was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #event.errors, status: :unprocessable_entity }
end
end
end
# DELETE /events/1
# DELETE /events/1.json
def destroy
#event.destroy
respond_to do |format|
format.html { redirect_to shift_event_path }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_event
#event = #shift.events.find(params[:id])
end
def get_shift
#shift = Shift.find(params[:shift_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
params.require(:event).permit(:time_s, :time_f, :odometre,
:location, :activity, :shift_id)
end
end
routes
shift_events_path GET /shifts/:shift_id/events(.:format) events#index
POST /shifts/:shift_id/events(.:format) events#create
new_shift_event_path GET /shifts/:shift_id/events/new(.:format) events#new
edit_shift_event_path GET /shifts/:shift_id/events/:id/edit(.:format) events#edit
shift_event_path GET /shifts/:shift_id/events/:id(.:format) events#show
PATCH /shifts/:shift_id/events/:id(.:format) events#update
PUT /shifts/:shift_id/events/:id(.:format) events#update
DELETE /shifts/:shift_id/events/:id(.:format) events#destroy
shifts_path GET /shifts(.:format) shifts#index
POST /shifts(.:format) shifts#create
new_shift_path GET /shifts/new(.:format) shifts#new
edit_shift_path GET /shifts/:id/edit(.:format) shifts#edit
shift_path GET /shifts/:id(.:format) shifts#show
PATCH /shifts/:id(.:format) shifts#update
PUT /shifts/:id(.:format) shifts#update
DELETE /shifts/:id(.:format) shifts#destroy
root_path GET / shifts#index
I figure the form first line is wrong but I don't know what to change it to. Secondly, I think some the connections in the events controller are wrong. Any help would be appreciated.
Try this:
<%= form_for [#shift, #event] do |f| %>
Now you will post a particular event for a particular shift.

First argument cannot contain nil or be empty rails 4

I'm making a single page application based on Futureme.org for practice. The user goes to the home page, sees a form to put their email address, subject, and the body of their message and sends it.
The problem I am having is I get an error "First argument in form cannot contain nil or be empty". Here is my code;
Model;
class Letter < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A[\w+\-,]+#[a-z\d\-.]+\.[a-z]+\z/i
validates_presence_of :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates_length_of :subject, presence: true, length: { maximum: 50 }
validates_presence_of :message
end
Controller;
class LettersController < ApplicationController
def new
#letter = Letter.new
end
def create
#letter = Letter.new(params[:letter])
if #letter.save
redirect_to letters_path, :notice => "Your letter was sent!"
else
render "new"
end
end
end
View form;
<%= form_for #letter, :html => {:class => 'form-horizontal'} do |f| %>
<% if #letter.errors.any? %>
<div class="error_messages">
<h2><%= pluralize(#letter.errors.count, "error")%>stopped this message from being saved</h2>
<ul>
<% #letter.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %><br />
</div>
<div class="field">
<%= f.label :message, "Message" %><br />
<%= f.text_area :message, size: "60x10" %>
</div>
<div class="actions"><%= f.submit "Submit", class: "btn btn-small btn-primary" %></div>
<% end %>
The form is on the home page which is in the "Welcome Controller".
Any help would be great.
It looks like you build letter in action new when form is drawn on other view :)
You should move #letter = Letter.new to appropriate action
One of the variant is:
#WelcomeController
def home
#letter = Letter.new
end
#LettersController
def create
#letter = Letter.new(params[:letter])
if #letter.save
redirect_to letters_path, :notice => "Your letter was sent!"
else
render "welcome/home"
end
end
be careful if you prepare some data in action home you should care about initializing them for action create when validation failed because you render "welcome/home" view

Error on validation if dropdown is blank

My form is:
<%= form_for #setup_limo, :html => { :class => 'form-horizontal' } do |f| %>
<%= render 'Partials/errors', object: #setup_limo %>
<div class="control-group">
<%= f.label :car_type_id, :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :car_type_id,CarType.all,:id,:name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :car_id, :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :car_id,Car.all,:id,:name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :driver_id, :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :driver_id,Driver.all,:id,:name, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
setup_limos_path, :class => 'btn' %>
</div>
<% end %>
My Model is:
class SetupLimo < ActiveRecord::Base
belongs_to :user
belongs_to :car_type
belongs_to :car
belongs_to :driver
validates :user,:car_type,:car, :driver, :presence => :true
validates_uniqueness_of :driver, :scope =>[:user, :car_type, :car]
end
My controller permitted params:
def setup_limo_params
params.require(:setup_limo).permit(:car_type_id, :car_id, :driver_id, :user_id)
end
Create action:
def create
#setup_limo = SetupLimo.new(setup_limo_params)
respond_to do |format|
if #setup_limo.save
format.html { redirect_to #setup_limo, notice: 'Setup limo was successfully created.' }
format.json { render action: 'show', status: :created, location: #setup_limo }
else
format.html { render action: 'new' }
format.json { render json: #setup_limo.errors, status: :unprocessable_entity }
end
end
end
But on create i got error: The log is:
Started POST "/setup_limos" for 127.0.0.1 at 2013-08-09 21:02:05 +0530
Processing by SetupLimosController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"######=", "setup_limo"=>{"car_type_id"=>"3", "driver_id"=>""}, "commit"=>"Create Setup limo"}
[1m[35mCACHE (0.0ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 [["id", 1]]
[1m[36mCarType Load (0.4ms)[0m [1mSELECT `car_types`.* FROM `car_types` WHERE `car_types`.`id` = 3 ORDER BY `car_types`.`id` ASC LIMIT 1[0m
[1m[35m (0.3ms)[0m ROLLBACK
Completed 500 Internal Server Error in 482ms
NoMethodError (undefined method `attributes' for nil:NilClass):
app/controllers/setup_limos_controller.rb:35:in `block in create'
app/controllers/setup_limos_controller.rb:34:in `create'
Rendered /Users/MohammedSha/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.1ms)
Rendered /Users/MohammedSha/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
Rendered /Users/MohammedSha/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms)
Rendered /Users/MohammedSha/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (42.6ms)
I'm using Rails 4
On submitting i got the above error,
I want to validate uniqueness of driver for user, car_type and car
Thanks for any help..
Maybe I'm missing something in the description, but you have a presence validation on driver and it is empty in the passed params.
If a driver can be empty, then drop it from that validation on SetupLimo:
validates :user, :car_type, :car, :presence => :true