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

Daniel Meusburger
Daniel Meusburger
2,200 Points

Ruby problem: Draper/Decorator

Hello Community,

I have a problem finishing the last videos of Ruby concerning AJAX. I already checked in the project files provided by treehouse and also tried it with some web research.

I get the following error when testing my file "user_friendships_controller_test.rb"

3) Error: test: #edit when logged in should get edit and return success. (UserFriendshipsC ontrollerTest): ActionView::Template::Error: undefined method `user' for #<UserFriendshipDecorat or:0x5ae4210>

and this on the webpage:

Showing C:/Sites/treebook/app/views/user_friendships/edit.html.erb where line #7 raised:

undefined method `user' for #<UserFriendshipDecorator:0x5cf8bf0>

Can anybody identify the problem with this code, I did everything like in the videos, but changed

class UserFriendshipDecorator < Draper::Base to class UserFriendshipDecorator < Draper::Decorator because I received a different error.

Thank you, please inform me if you need more information!

Daniel

7 Answers

Daniel Meusburger
Daniel Meusburger
2,200 Points

Any ideas? Didn't find my mistake in the meantime!

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Can you paste your edit.html.erb file here? That will help us to figure out the issue.

Daniel Meusburger
Daniel Meusburger
2,200 Points

Thank you for responding: Here is the code! It should be the exact same as in the video I think!

<div class="page-header"> <h1>Viewing Friendship</h1> </div>

<div class="row"> <div class="avatar span2"> <%= image_tag @user_friendship.user.gravatar_url %><br /> <%= @user_friendship.user.first_name %> </div> <div class="avatar span2"> <%= image_tag @user_friendship.friend.gravatar_url %><br /> <%= @user_friendship.friend.first_name %> </div> </div> <hr />

<h3><%= @user_friendship.sub_message %></h3>

<div class="form-actions"> <% if @user_friendship.requested? %> <%= form_for @user_friendship, url: accept_user_friendship_path(@user_friendship), method: :put do |form| %> <%= submit_tag "Accept Friendship", class: 'btn btn-primary' %> <% end %> <% end %>

<%= form_for @user_friendship, url: user_friendship_path(@user_friendship), method: :delete do |form| %> <%= submit_tag "Delete Friendship", class: 'btn btn-danger' %> <% end %> </div>

Any luck with this Daniel I'm having a similar issue

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Try putting the following in just below the first line in the file app/decorators/user_friendship_decorator.rb:

delegate_all
Dave Schneider
Dave Schneider
7,403 Points

I had a similar problem, however, Jason's solution fixed mine as well.

My error was "undefined method 'friend' for #<UserFriendshipDecorator:0x000..." the test that failed was "#edit when logged in should assign to friend. (UserFriendshipsControllerTest):"

And the app was failing when attempting to edit a user friendship. It was failing on this line: "@friend = @user_friendship.friend" according to my browser error message.

Jason - can you explain why adding "delegate_all" fixed both my problem and Daniel's? Thanks! Dave

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Dave,

The reason that "delegate_all" fixes the problems is because a Ruby class only knows what methods it has available to it in its own class. When you're using Draper, by saying "delegate_all", you ask the decorator to ask the model it's decorating for any methods it doesn't know how to execute.