UserFriendshipsController #index中的语法错误

时间:2014-07-23 18:59:55

标签: ruby-on-rails ruby-on-rails-4 syntax controller syntax-error

我在UserFriendshipsController#index中获取了一个SyntaxError:

@user_friendship = current_user.user_friendships.all

我不确定我错过了什么。任何帮助将不胜感激!我还是铁杆新手。提前谢谢!

控制器

 class UserFriendshipsController < ApplicationController
   before_filter :authenticate_user!

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

 end
 end

索引

     <% @user_Friendships.each do |friendship| %>
          <% friend = friendship.friend %>
      <div id="<%= dom_id(friendship) %>" class="friend row">
       <div class="span1">
        <center><%= link_to image_tag(user.avatar.url(:thumb)), user %></center>
       </div>
       <div class="span7">
         <strong><%= friend.name %></strong><br />
            <%if friendship.pending? %>
            <em>Frienship is pending.</em> <%=link_to "Delete request", edit_user_friendship_path(friendship) %>.
            <% end %>
            <% if friendship.requested? %>
                <em>Friendship requested.</em> <%= link_to "Accept Friendship", edit_user_friendship_path(friendship) %>.
            <% end %>
            <% if friendship.accepted? %>
                <em>Friendship started <%= friendship.updated_at %>.</em> <%= link_to "Update friendship", edit_user_friendship_path(friendship) %>.
            <% end %>
           </div>
       </div>
   <% end %>

模型

     class UserFriendship < ActiveRecord::Base
     belongs_to :user
     belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'

     attr_accessible :user_id, :friend_id, :user, :friend :state

     state_machine :state, initial: :pending do

    after_transition on: :accept, do: :send_acceptance_email

    state :requested

    event :accept do
        transition any => :accepted
    end
   end

   def self.request(user1, user2)
    transaction do
        friendship1 = create!(user: user1, friend: user2, state: 'pending')
        friendship2 = create!(user: user2, friend: user1, state: 'requested')

        friendship1.send_request_email
     end

1 个答案:

答案 0 :(得分:0)

在索引文件中,使用@user_Friendships,它与控制器@user_friendship(extra s和capital F)中定义的变量不同。