Rails Tutorial Chapter 7 Failing Rspec - railstutorial.org

I'm going through the ROR tutorial and hit a roadblock on question 4.
Failures:
1) UserPages signup with valid information after saving the user
Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') }
expected css "div.alert.alert-success" with text "Welcome" to return something
# ./spec/requests/user_pages_spec.rb:54:in `block (5 levels) in <top (required)>'
Finished in 3.26 seconds
39 examples, 1 failure
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:54 # UserPages signup with valid information after saving the user
user_controllers.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def create
#user = User.new(params[:user])
if #user.save
# Handle a successful save.
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
def new
#user = User.new
end
end
user_pages_spec.rb
require 'spec_helper'
describe "UserPages" do
subject { page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
describe "signup page" do
before { visit signup_path }
it {should have_selector('h1', text: 'Sign up') }
it {should have_selector('title', text: full_title('Sign up')) }
end
describe "signup" do
before { visit signup_path }
let(:submit) { "Create my account" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_selector('title', text: 'Sign up') }
it { should have_content('error') }
it { should have_content('The form contains')}
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "user#example.com"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by_email('user#example.com') }
it { should have_selector('title', text: user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
end
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<%= render 'layouts/header' %>
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-"<%= key %>"><%= value %></div>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
<%= debug(params) if Rails.env.development? %>
</div>
</body>
</html>
I've tried manually hardcoding the
<div class="alert alert-success">Welcome</div>
That causes the test to pass, so I've ruled out the user_pages_spec file. Not sure where the error is since it prints the success message upon signing up.

Looks like you have an extra quote
<div class="alert alert-"<%= key %>"><%= value %></div>
should be
<div class="alert alert-<%= key %>"><%= value %></div>

Related

Rails 4.2 Creating Multiple New Records with One Form

I have three models: Lesson, Questions and Answers.
What I'm trying to do is on the show lesson view, display the questions and allow users to create answers for each answer. However, I'm not sure the best way to do this.
I tried this approach on my lesson#showview:
<% #questions.each do |question| %>
<%= question.content %><br /><br />
<%= simple_form_for :answers do |f| %>
<%= f.input :content %>
<%= f.hidden_field :question_id, :value => question.id %>
<%= f.button :submit %>
<% end %>
<% end %>
With this code, I receive the error param is missing or the value is empty: lesson
Answer has two fields: content, question_id.
My other concern is that I'd like to have this be user friendly, so if there are multiple questions, there should be multiple input boxes for the answers (one per each question) and one submit button (so multiple answers can be posted at one time).
I think that my approach my bad, but I'm not sure how else to do this, so any help would be greatly appreciated.
Here's what I have so far:
Models:
class Lesson < ActiveRecord::Base
has_many :questions, dependent: :destroy
has_many :answers, through: :questions
accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :answers, reject_if: :all_blank, allow_destroy: true
end
class Question < ActiveRecord::Base
belongs_to :lesson
has_many :answers, dependent: :destroy
end
class Answer < ActiveRecord::Base
belongs_to :question
end
Lessons Controller
class LessonsController < ApplicationController
def show
#questions = #lesson.questions
end
# PATCH/PUT /lessons/1
# PATCH/PUT /lessons/1.json
def update
respond_to do |format|
if #lesson.update(lesson_params)
format.html { redirect_to #lesson, notice: 'Lesson was successfully updated.' }
format.json { render :show, status: :ok, location: #lesson }
else
format.html { render :edit }
format.json { render json: #lesson.errors, status: :unprocessable_entity }
end
end
end
private
def lesson_params
params.require(:lesson).permit(:name,
answers_attributes: [:id, :content, :question_id]
)
end
end
routes.rb
resources :lessons
post '/lessons/:id', to: "lessons#update"
Add gem in Gemfile and run bundle install:-
gem "nested_form"
On lession show page:-
<%= nested_form_for #lession do |lession_form| %>
<%= #lession.content %>
<%= lession_form.fields_for :questions do |question_form| %>
<% #questions.each do |question| %>
<%= question.content %><br /><br />
<%= question_form.fields_for :answers do |answer_form| %>
<%= answer_form.text_field :content %>
<%= answer_form.link_to_remove "Remove this answer" %>
<% end %>
<%= question_form.link_to_add "Add more answer", :answers %>
<% end %>
<% end %>
<%= lession_form.submit 'Update' %>
<% end %>
I would have thought you are able to achieve without the use of a Gem.
You may need to specify inverse_of in your model. I have previously found this was required in nested attributes when dealing with forms.
class Question < ActiveRecord::Base
belongs_to :lesson
has_many :answers, dependent: :destroy, :inverse_of => :question
end
class Lesson < ActiveRecord::Base
has_many :questions, dependent: :destroy, :inverse_of => :lessons
has_many :answers, through: :questions
#etc.
end
In your lessons controller:
def show
#lesson = Question.find(params[:id])
#questions = #lesson.questions
x.times { #lesson.questions.answer.build }
end
In your views/lessons/show page:
<%= form_for #lesson do |lesson| %>
<%= #lesson.whatever_attribute %>
<%= lesson.fields_for :questions do |question| %>
<% #questions.each do |question| %>
<%= question.content %>
<% end %>
<div id="answers-div" class='form-group'>
<%= question_form.fields_for :answers do |answer| %>
<%= answer.text_field :content id:"answer-entry" %>
<% end %>
</div>
<% end %>
<% end %>
<%= lesson.submit 'Submit' %>
Below the form add some buttons to add further answers or remove:
<button class="btn btn-default" id="addNewAnswer">Add Another Answer Box</button><br>
<button class="btn btn-default" id="deleteNewAnswer">Delete Last Answer</button>
You can then add & remove answers on the fly with jQuery.
$(document).ready(function(){
$("#addNewAnswer").click(function() {
$("#answers-div").append(createNewInputElement($("#answers-div")));
});
});
function createNewInputElement(form) {
var newIndex = $("#answers-div").children('input#choice-entry').length;
var newInput = $("#answer-entry").clone().attr('name', generateNewInputName(newIndex));
newInput.val('');
return newInput;
};
function generateNewInputName(idx) {
return "question[answers_attributes][" + idx + "][content]"
};
$(document).ready(function(){
$("#deleteNewAnswer").click(function() {
if ($("#answers-div input").length > 1) {
$("#answers-div input:last-child").remove();
}
});
});
The use of a nested form is not an issue. You require a nested form to allow you to nest answers within your lessson.questions but you are only allowing the user to give input
If using Rails 4 (and the Strong params) you will also need to allow these with something along these lines (otherwise the params being passed will not be allowed through).
private (in your Lessons controller)
def lesson_params
params.require(:lesson).permit(:content, answer_attributes:[:content])
end
This may not be perfect but it's the start of some sort of solution to your question I would hope.
Try to testing this code, an email me again
in your lesson#show.html.erb
<% for question in #lesson.questions %>
<%= question.content %><br /><br />
<%= simple_form_for :answers do |f| %>
<%= f.input :content %>
<%= f.hidden_field :question_id, :value => question.id %>
<%= f.button :submit %>
<% end %>
<% end %>

Updating User password & email in separate forms but in same view

My goal is to create a profile page where the (logged in) user can choose to either update their email or their password (in separate forms so not both at the same time) without navigating away from the page.
Updating one's email is as simple as entering their new email and hitting the "Save Changes" submit button.
Updating one's password requires the user to enter their old password, their new password, and a confirmation of their new password then hitting the "Update Password" button. The problem must be in the update_pwd method.
users_controller.rb
# ...
def edit # template: edit.html.erb
#user = User.find(current_user)
end
def update_email
#user = User.find(current_user)
if params[:commit] == 'Save Changes'
if #user.update_attributes(user_params_email)
redirect_to me_url
flash[:notice] = "Your changes have been saved"
else # either invalid or taken
render :edit
end
else
render :edit
end
end
def update_pwd
#user = User.find(current_user)
if params[:commit] == 'Update Password'
if #user.update_attributes(user_params_pwd) && User.find_by(username: current_user.username).try(:authenticate, params[:current_password])
redirect_to me_url
flash[:notice] = "Password updated!"
else
flash.now[:alert] = #user.errors.full_messages[0]
render :edit
end
else
render :edit
end
end
private
#def user_params
#params.require(:user).permit(:id, :username, :email, :password, :password_confirmation)
#end
def user_params_email
params.require(:user).permit(:email)
end
def user_params_pwd
params.require(:user).permit(:password, :password_confirmation, :current_password)
end
edit.html.erb
<%= form_for(current_user, :url => {:controller => 'users', :action => 'update_email'}) do |f| %>
<% if #user.errors.any? %>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
<% end %>
<p> Current Email: <%= current_user.email %> </p>
<p> New Email: <%= f.text_field :email %> </p>
<%= f.submit "Save Changes" %>
<% end %>
<%= form_for(current_user, :url => {:controller => 'users', :action => 'update_pwd'}) do |g| %>
<p>Old Password<br>
<%= password_field_tag :current_password, params[:password] %></p>
<p>New Password<br>
<%= g.password_field :password %></p>
<p>Confirm New Password<br>
<%= g.password_field :password_confirmation %></p>
<%= g.submit "Update Password" %>
<% end %>
routes.rb
# ...
get '/me' => 'users#edit'
patch '/me' => 'users#update_email'
patch '/me' => 'users#update_pwd'
# ...
Now, I've been able to get the email updating to work as desired (with the necessary error messages/validations and so forth) but whenever the update password button is clicked, the view is rendered but nothing happens. Instead, it seems as if the update_email function is being called:
Started PATCH "/me" for ::1 at 2015-05-20 10:34:31 -0500
Processing by UsersController#update_email as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"msEsj6yxfdrbXjjdm6cH3JamrFU8R1EoZ5asmE831GSxLwpiiIW/wmGrr9HiQxFDySJtW5MKK6Ezq9hZaMNFtA==", "current_password"=>"[FILTERED]", "user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update Password"}
←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1←[0m [["id", 2]]
DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `find`. Please pass the id of the object by calling `.id`. (called from update_email ...)
user.rb
# ...
has_secure_password
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
validates_uniqueness_of :email
validates :password, length:{ minimum: 8 }, on: :create
validates :password_confirmation, presence: true, on: :create
# ...
application_controller.rb
# ...
helper_method :current_user
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
rescue ActiveRecord::RecordNotFound
end
Error log (as seen above)
Started PATCH "/me" for ::1 at 2015-05-20 10:34:31 -0500
Processing by UsersController#update_email as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"msEsj6yxfdrbXjjdm6cH3JamrFU8R1EoZ5asmE831GSxLwpiiIW/wmGrr9HiQxFDySJtW5MKK6Ezq9hZaMNFtA==", "current_password"=>"[FILTERED]", "user"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update Password"}
←[1m←[36mUser Load (0.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1←[0m [["id", 2]]
DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `find`. Please pass the id of the object by calling `.id`. (called from update_email ...)
You can try the following, I think this will help.
Please revert back if you face any issue.
routes.rb
get '/me' => 'users#edit'
patch '/me' => 'users#update_data'
edit.html.erb
<%= form_for(current_user, :url => {:controller => 'users', :action => 'update_data'}) do |f| %>
<% if #user.errors.any? %>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
<% end %>
<p> Current Email: <%= current_user.email %> </p>
<p> New Email: <%= f.text_field :email %> </p>
<%= f.submit 'Save Changes', name: 'update_email' %>
<% end %>
<%= form_for(current_user, :url => {:controller => 'users', :action => 'update_data'}) do |g| %>
<p>Old Password<br>
<%= password_field_tag :current_password, params[:password] %></p>
<p>New Password<br>
<%= g.password_field :password %></p>
<p>Confirm New Password<br>
<%= g.password_field :password_confirmation %></p>
<%= g.submit 'Update Password', name: 'update_password' %>
<% end %>
users_controller.rb
def edit
#user = User.find(current_user)
end
def update_data
#user = User.find(current_user)
if params[:commit] == "update_email"
if #user.update_attributes(user_params_email)
flash[:notice] = "Your changes have been saved"
redirect_to me_url
else
render :edit
end
elsif params[:commit] == "update_password"
if #user.update_attributes(user_params_pwd) && User.find_by(username: current_user.username).try(:authenticate, params[:current_password])
flash[:notice] = "Password updated!"
redirect_to me_url
else
flash.now[:alert] = #user.errors.full_messages[0]
render :edit
end
else
render :edit
end
end
private
def user_params_email
params.require(:user).permit(:email)
end
def user_params_pwd
params.require(:user).permit(:password, :password_confirmation, :current_password)
end

Rails, form_for is updating attribute on all model records, not just the attribute belonging to the instance

I have created a view for volunteers to sign up for specific shifts. I am able to write my view and controller for shifts so that, when the form_for submit button is clicked, the current_user.id is pushed onto the specific shift's user_ids attribute, which is an array. The problem is that it updates each of the shift's user_ids with the current user's id. What am I missing here? Any insight would be appreciated. Thanks.
volunteer.html.erb
<div class="container">
<div style="width:60%;margin:0 auto 0 auto;" class="inner-lower">
<div class="list-group">
<% #uniq_shifts.each do |title| %>
<% #shifts_by_title = #shifts.where(title: title) %>
<% #title_vols = #shifts_by_title.pluck(:vols_needed).sum %>
<% #times = #shifts_by_title.pluck(:time) %>
<% #time_vols = #shifts_by_title.pluck(:vols_needed) %>
<% if #title_vols > 0 %>
<!-- ACTIVITY TITLE -->
<div id=<%= "activity#{title}" %> class="activity">
<a href="#" class="list-group-item">
<%= title %>
<!-- ACTIVITY NUMBER VOLUNTEERS NEEDED-->
<span class="badge-volunteer"><%= #title_vols %></span>
</a>
</div>
<div class="sub" style="display:none;">
<% #shifts_by_title.each do |shift| %>
<!-- ACTIVITY SHIFT -->
<a href="#" class="list-group-item-sub">
<!-- ACTIVITY SHIFT TIME -->
<%= shift.time %>
<span class="badge">
<!-- ACTIVITY SHIFT NUMBER OF VOLUNTEERS NEEDED -->
<%= shift.vols_needed %>
</span>
</a>
<%= form_for shift, :method => :put do |f| %>
<%= f.hidden_field :user_ids, :value => shift.add_user_id(#user.id) %>
<%= f.submit "sign up", class: "btn btn-primary" %>
<% end %>
<% end %>
</div>
<% end %>
<% end %>
</div>
</div>
shift.rb
class Shift < ActiveRecord::Base
has_and_belongs_to_many :users
def add_user_id(user_id)
user_ids_will_change!
update_attributes user_ids: self.user_ids + [ user_id ]
self.save
end
end
shifts.controller.rb
class ShiftsController < ApplicationController
before_action :set_shift, only: [:show, :edit, :update, :destroy]
before_action :volunteer, only: [:show, :edit, :update, :destroy]
def index
#shifts = Shift.all
end
def volunteer
#shifts = Shift.all
#user = current_user
#shift_titles = #shifts.pluck(:title)
#uniq_shifts = #shift_titles.uniq
#vols_needed = #shifts.pluck(:vols_needed)
unless current_user
render action: 'new'
end
end
def show
end
def new
#shift = Shift.new
end
def edit
end
def create
#shift = Shift.new(shift_params)
if #shift.save
redirect_to #shift, notice: 'Shift was successfully created.'
else
render :new
end
end
def update
#user = current_user
if #shift.update(shift_params)
redirect_to #shift, notice: 'Shift was successfully updated.'
else
render :edit
end
end
def destroy
#shift.destroy
redirect_to pages_url, notice: 'Shift was successfully destroyed.'
end
private
# Use callbacks to share common setup or contraints between actions.
def set_shift
#shift = Shift.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def shift_params
params.require(:shift).permit(:title, :time, :vols_needed, :user_ids => [])
end
end

Determining if users are facebook friends (Parsing)

I'd like to find out if the current user is friends with the root node user, using Koala, and if so, print a statement saying: you are friends.
My problem is the parsing of data returned from facebook.
user.rb
def facebook
#facebook ||= Koala::Facebook::API.new(oauth_token)
block_given? ? yield(#facebook) : #facebook
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil # or consider a custom null object
end
def facebook_friend
facebook.get_connection("me", "friends/#user.user_id")
end
Profile.html.erb
<% if current_user.facebook_friend.include?(#user.uid)? %>
<p> you are friends</p>
<% else %>
<p> you are not friends</p>
<% end %>
Output:
<%= current_user.facebook_friend %>
will return: [{"name"=>"Elizabeth", "id"=>"100008217009369"}]
<%= #user.uid %>
will return: 100008217009369
Thanks!!!
It was much easier than I thought:
In the user model simple used the Koala method:
def facebook_friend(user)
facebook.get_connection("me", "friends/#{user.uid}")
end
def my_friends_list
facebook.get_connection("me", "friends")
end
Then in the Profile View, adding current_user and #user as arguments:
<% if #user.uid.present? %>
<% if signed_in? && current_user == #user %>
<p><%= current_user.my_friends_list.count %> friends</p>
<% elsif current_user.facebook_friend(#user).empty? %>
<p><%= current_user.mutual_friends.count %> friends in common</p>
<% else %>
<p>You are Facebook friends</p>
<% end %>
<% else %>
<p>Not connected</p>
<% end %>

What is wrong with my coffeescript in Stripe?

I've been working on integrating Stripe into my web application, and it doesn't seem to be working. To help me along, I've been using Ryan Bates's Rails Cast on integrating Stripe. Whenever I try to run the payment form, I get an error saying that "There was a problem with my credit card". I think the problem lies in my coffeescript file, but perhaps I'm wrong. I've included the stripe user token as a part of my user model instead of placing it into its own subscription model. Here is the coffeescript code I have:
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
subscription.setupForm()
user =
setupForm: ->
$('#new_user').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
user.processCard()
false
else
true
processCard: ->
card =
number: $('#card_number').val()
cvc: $('#card_code').val()
expMonth: $('#card_month').val()
expYear: $('#card_year').val()
Stripe.createToken(card, user.handleStripeResponse)
handleStripeResponse: (status, response) ->
if status == 500
$('#user_stripe_card_token').val(response.id)
$('#new_user')[0].submit()
else
$('#stripe_error').text(response.error.message)
$('input[type=submit]').attr('disabled', false)
I'm a beginner when it comes to programming, so any help you can give me would be great.
Here's the error I get in my terminal when I try to sign up:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xas+iA+a3op7jUi57qTr7XWQSClPscA7fR19rkclkEE=", "user"=>{"stripe_card_token"=>"", "name"=>"Jack", "email"=>"email#example.com", "phone_number"=>"203-xxx-xxxx", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create my account"}
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('jjets718#yahoo.com') LIMIT 1
Stripe error while creating customer: Invalid token id:
My view for the signup is this:
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.hidden_field :stripe_card_token %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :phone_number, "Your cell phone number" %>
<%= f.text_field :phone_number %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Password confirmation" %>
<%= f.password_field :password_confirmation %>
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %>
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"}%>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
My code for my controller is this for the create method:
def create
#user = User.new(params[:user])
if #user.save_with_payment
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to edit_user_path(current_user)
UserMailer.welcome_email(#user).deliver
else
render 'new'
end
end
My code for the database migration for the user token is this:
class AddStripeToUsers < ActiveRecord::Migration
def change
add_column :users, :stripe_customer_token, :string
end
end
And the code for the save_with_payment method in my model is this:
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, plan: 1, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
2 things that come to mind:
You should be doing a status check for 200, not 500
You may need to require the coffeescript file in your application.js
e.g. //= require users
I could be wrong, but at this point:
handleStripeResponse: (status, response) ->
if status == 500
$('#user_stripe_card_token').val(response.id)
In addition to changing if status == 500 to if status == 200, this line $('#user_stripe_card_token').val(response.id) may need to be $('#new_user_stripe_card_token').val(response.id). Make sure to check the input ID.