mardi 4 août 2015

I'm having this issue and it looks like I am traversing the params hash correctly in the create method of the controller, but something is wrong.

user_friendships_controller.rb

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

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)
        @user_friendship.save
        flash[:success] = "You are now friends with #{@friend.first_name.titleize}!"
        redirect_to user_path(@friend)
    else
        flash[:error] = "Friend required"
        @user_friendship = current_user.user_friendships.new(friend: @friend)
    end
end
end

The error is on line 17 @friend = User.find(params[:user_friendship][:friend_id])

Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"p8hBlY50rLH4477juS4tAoo2aWiEaUD6oSzuCzxPTiU=",
 "user_friendship"=>{"friend_id"=>"#<User:0x007f88b1d4f9e0>"},
 "commit"=>"Yes,
 Add Friend"}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire