的Ruby / Rails:friend_id没有添加到友谊表 [英] Ruby/Rails: friend_id is not added to friendships table

查看:186
本文介绍了的Ruby / Rails:friend_id没有添加到友谊表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Rails 4.1.4框架。其加入了friend_id到user_friendships见下表一个问题...当程序在运行轨道,我重定向到根目录(如预期),并给出了友谊的创建成功消息。然而,当我破解打开数据库GUI,我看到只有一个user_id是保存在user_id列,而friend_id列总是留给'零',不管是谁的朋友是谁。我已经搜查高和低 - 我知道它必须是简单的东西,它的驾驶我疯狂。任何帮助深表AP preciated!

任何人都可以看到我可能会在code,将prevent这个被保存所做的错误呢?难道是一个缺少强有力的参数问题的friend_id,如果是这样,我怎么去纠正呢?

模型:

 类用户< ActiveRecord的::基地
  的has_many:user_friendships
  的has_many:朋友们,通过:user_friendships
类UserFriendship< ActiveRecord的::基地
    belongs_to的:用户
    belongs_to的:朋友,CLASS_NAME:用户,foreign_key:friend_id

控制器:

 类UserFriendshipsController< ApplicationController中
    的before_filter:的authenticate_user!只有:[新]    高清新
        如果PARAMS [:friend_id]
            @friend = User.where(PROFILE_NAME:PARAMS [:friend_id])。第一
            募集的ActiveRecord :: RecordNotFound如果@ friend.nil?
            @user_friendship = current_user.user_friendships.new(朋友:@friend)
        其他
            闪光[:错误] ='朋友必需的。
        结束        抢救的ActiveRecord :: RecordNotFound
            渲染文件:公共/ 404',状态:NOT_FOUND
    结束    打造高清
        如果PARAMS [:user_friendship&放大器;&安培; PARAMS [:user_friendship] .has_key?(:friend_id)
            @friend = User.where(PROFILE_NAME:PARAMS [:user_friendship] [:friend_id])。第一
            @user_friendship = current_user.user_friendships.new(朋友:@friend)
            @ user_friendship.save
            redirect_to的root_path
            闪光[:成功] =您现在是朋友。 #{@friend.full_name}
        其他
            闪光[:错误] ='朋友必需的。
            redirect_to的root_path
        结束
    结束

user_friendships查看文件--new.html.erb

 <%如果@friend%GT;    < H1> &所述;%= @ friend.full_name%GT; < / H1>    &所述p为H.;你真的想成为朋友以<%= @ friend.full_name%GT;< / P>    <%=的form_for @user_friendship,方法:做后期| F | %GT;
        < D​​IV CLASS =表格表单行动>
            <%= f.hidden_​​field:friend_id,值:@ friend.profile_name%GT;
            <%= submit_tag是的,添加好友,等级:BTN BTN-主'%GT;
            <%=的link_to取消,profile_path(@friend)类:BTN'%GT;
        < / DIV>    <%结束%GT;<%结束%GT;

- 表单提交按钮上方的个人资料页上

 < D​​IV CLASS =页面页眉>
    < H1> &所述;%= @ user.full_name%GT; < / H1>
    <%=的link_to添加好友,new_user_friendship_path(friend_id:@user)类:BTN'%GT;
< / DIV>


解决方案

您有 PROFILE_NAME 的一个问题。我不知道它是什么,但它应该是一个 ID 。因此,在控制器变化模型搜索:

  @friend = User.find_by_id(PARAMS [:user_friendship] [:USER_ID])

和模板<%= f.hidden_​​field:friend_id,值:@ friend.id%GT;

这对我的作品

不过,如果您使用的是friendly_id宝石,或强迫你使用另一件事 PROFILE_NAME 而不是 ID ,你也应该用在其他地方,比如链接:

  new_user_friendship_path(friend_id:@ user.profile_name)

也许这将有所帮助。

Using the Rails 4.1.4 framework. Having a problem adding a 'friend_id' to the user_friendships table below... when the program runs in rails, I am redirected to the root (as expected) and given the success message that the friendship was created. However, when I crack open the database GUI, I see that only a user_id is saved into the user_id column, and the friend_id column is always left 'nil', no matter who friends who. I've searched high and low - I know it must be something simple and it's driving me crazy. Any help is much appreciated!

Can anyone see the mistake I may have made in the code that would prevent this from being saved? Could it be a missing strong parameters issue for friend_id, and if so, how do I go about correcting it?

The Models:

class User < ActiveRecord::Base
  has_many :user_friendships
  has_many :friends, through: :user_friendships


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

The 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

    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
            redirect_to root_path
            flash[:success] = "You are now friends." #{@friend.full_name}
        else
            flash[:error] = 'Friend required.'
            redirect_to root_path
        end
    end

user_friendships view files --new.html.erb

<% if @friend %>

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

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

    <%= form_for @user_friendship, method: :post do |f| %>
        <div class="form form-actions">
            <%= f.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 %>

-- submission button for form above on profile page

<div class="page-header">
    <h1> <%= @user.full_name%> </h1>
    <%= link_to "Add Friend", new_user_friendship_path(friend_id: @user), class:'btn'%>
</div>

解决方案

You have a problem with profile_name. I don't know what is it, but it should be an id. So, in controller change model search to:

@friend = User.find_by_id(params[:user_friendship][:user_id])

And in template <%= f.hidden_field :friend_id, value: @friend.id %>

This works for me

However, if you are using friendly_id gem, or another thing that force you to use profile_name instead of id, you should also use it in other places, like links:

new_user_friendship_path(friend_id: @user.profile_name)

Maybe this will help.

这篇关于的Ruby / Rails:friend_id没有添加到友谊表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆