Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Andre' Jones
Andre' Jones
26,671 Points

integration test friendships

On the last test of this video I'm getting failure ( Expected response to be <:redirect>, but was 200.

Been on 2 videos for about 5-6 hours and brain is hurting. does anyone know whats going on. TEST

require 'test_helper'

class AddAFriendTest < ActionDispatch::IntegrationTest def sign_in_as (user, password) post signin_path, user: {email: user.email, password: password} end

test "that adding a friend works" do sign_in_as users(:andre), "testing"

get "/user_friendships/new?friend_id=#{users(:mike).profile_name}"
assert_response :success

assert_difference 'UserFriendship.count' do
  post "/user_friendships", user_friendship: {friend_id: users(:mike).profile_name} 
  assert_response :redirect
  assert_equal "You are now friends with #{users(:mike).full_name}", flash[:success]
end

end end

NEW HTML

<% if @friend %> <h1> <%= @friend.full_name %></h1>

<p> Do you really want to friend <%= @friend.full_name %>?</p>

<%= form_for @user_friendship, method: :post do |form|%>
    <div class="form form-actions">
        <%= form.hidden_field :friend_id, value: @friend.profile_name %>
        <%= submit_tag "Yes, Add Friend", class: 'btn btn-primary'%>
        <%= link_to "Cancel", profile_path(@friend), class: 'btn' %>
    </div>
<%end%>

<% end %>

controller

class UserFriendshipsController < ApplicationController before_filter :authenticate_user!, only: [:new]

def new if params[:friend_id] @friend = User.where(profile_name: params[:friend_id]).first raise ActiveRecord::RecordNotFound if @friend.nil? @user_friendship = current_user.user_friendships.new(friend: @friend) else flash[:error] = "Friend required" end rescue ActiveRecord::RecordNotFound render file: 'public/404', status: :not_found end end

def create if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id) @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first @user_friendship = current_user.user_friendships.new(friend: @friend) @user_friendship.save flash[:success] = "You are now friends with #{@friend.full_name}" redirect_to profile_path(@friend) else flash[:error]="Friend required" redirect_to root_path end

end

1 Answer

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hi Andre' Jones I think this is the same question posted to support. Try removing the "create.html.erb" from app/views/user_friendships and give it another go.

Andre' Jones
Andre' Jones
26,671 Points

Yea that was me. Removing that didnt work now it says The action 'create' could not be found for UserFriendshipsController

Andre' Jones
Andre' Jones
26,671 Points

Im just going to scrap my files and use the project files and start the 2nd stage in building the UI over.