未定义的方法`请求?'为零:NilClass

时间:2014-07-29 02:12:29

标签: ruby-on-rails ruby ruby-on-rails-4 undefined nomethoderror

我一直收到错误:

UserFriendships#edit

中的NoMethodError

未定义的方法`请求?'为零:NilClass。

我正在努力完成“接受友谊”活动。关于树屋的教程,我正走向视频的最后。当我点击我的删除请求时#39;应该把我带到我的user_friendship / edit页面的链接我得到上面的错误。

请参阅下面的代码。提前谢谢。

user_friendships /修改

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

 <% if @user_friendship.requested? %>
     <h3> Do you really want to be friends with <%= @friend.name %>?</h3>
 <% end %>


 <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 %>
 </div> 

User_friendship /控制器

 class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!

def new
  if params[:friend_id]
    @friend = User.find(params[:friend_id])
    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


def create
  if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
    @friend = User.find(params[:user_friendship][:friend_id])
    @user_friendship = current_user.user_friendships.new(friend:  @friend)
    if @user_friendship.save
      flash[:success] = "You are now friends!"
    else
      flash[:error] = "There was a problem."
    end
    redirect_to user_path(@friend)
  else
    flash[:error] = "Friend required"
    redirect_to root_path
  end
end


def index
  @user_Friendships = current_user.user_friendships.all
end

 def accept
   @user_friendship = current_user.user_friendships.find(params [:id])
   if @user_friendship.accept!
    flash[:success] = "You are now friends with #  {@user_friendship.friend.name}"
  else
    flash[:error] = "That friendship could not be accepted"
   redirect_to user_friendships_path
end


 def edit
   @user_friendship = current_user.user_friendships.find(params [:id])
   @friend = @user_friendship.friend
 end

 def user_friendship
 params.require(:user_friendship).permit(:user_id, :friend_id, :user, :friend, :state, :user_friendship)
 end

端   端

2 个答案:

答案 0 :(得分:1)

感谢所有评论的人。我只需将请求的一起删除即可显示我的user_friendships / edit页面。我一定错过了一些东西。 现在它只是@user_friendship而不是@ user_friendship.requested。

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

<% if @user_friendship %>
    <h3> Do you really want to be friends with <%= @friend.name %>?</h3>
<% end %>


<div class="form-actions">
    <% if @user_friendship %>
        <%= 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 %>
</div>  

答案 1 :(得分:-5)

解决方案是定义

def nil.requested?
  nil
end