导轨 - 添加记录到连接表从控制器 [英] Rails - Add Record to join table from controller

查看:146
本文介绍了导轨 - 添加记录到连接表从控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从按钮的操作创建一个连接表中的记录。我有一个事件模型,并想跟踪每个用户选择的事件。

I'm trying to create a record within a join table from the action of a button. I would have an events model and would like to track selected events from each user.

我用HABTM关系,因为我并不需要任何额外的字段。

I used the HABTM relationship since I don't really need any extra fields.

User.rb:

has_to_and_belongs_to_many :events

Event.rb:

Event.rb:

has_to_and_belongs_to_many :users

Events_Users迁移:

Events_Users Migration:

[user_id, event_id, id=>false]

我被陷在记录的实际创建。早些时候有人帮我在控制台中添加记录:

I'm getting stuck on the actual creation of the record. Someone helped me earlier with adding the record in within the console:

u = User.find(1)
u.events << Event.find(1) 

现在我想执行的操作,只需点击某个链接的结果......这是朝着正确的方向?

Now I would like to perform the action as a result of clicking a link... Is this in the right direction?

def add
  @user = User.find(session[:user_id])
  @event = Event.find(params[:id])
  if @user.events.save(params[:user][:event])
    flash[:notice] = 'Event was saved.'
  end
end

我应该添加 @ user.events.new 的地方,如果是这样我在哪里把PARAMS哪些用户,哪些事件?

Should I add a @user.events.new somewhere and if so where do I put the params of which user and which event?

推荐答案

以下code应该工作(假定你传递与对应事件对象的ID名称ID的参数):

The following code should work (assuming that you pass in an parameter with the name id that corresponds to the id of an event object):

   def add
     @user = User.find(session[:user_id])
     @event = Event.find(params[:id])
     @user.events << @event
     flash[:notice] = 'Event was saved.'
   end

我在你的code看到的问题是:

The problems I see in your code are:

  1. 正在传递一个哈希.save。保存只能采取相应的验证是否应运行,并默认为true一个布尔值。然而.create和。新的可以接受的值的哈希值。 (.save会后。新的使用)。

  1. You are passing a hash to .save. Save should only take a boolean value corresponding whether validations should be run and is true by default. However .create and .new can accept a hash of values. (.save would be used after .new).

您通过PARAMS加载事件,但随后试图创建通过PARAMS事件[:用户] [:事件]。哪个你想干什么?创建或加载? (我的例子假设负载)

You load an event through params[:id] but then you attempt to create an event through params[:user][:event]. Which do you want to do? Create or load? (my example assumes load)

这是有效果的行动,如这一个当用户点击一个按钮,提交表单,而不是点击链接应该发生。这code可能受到跨站点请求伪造(有人可能会诱使人去点击其他网站上跑这个动作的链接)。导轨形式,如果正确实施,是防止这一点,因为它们使用伪造请求保护令牌。

Actions that have an effect such as this one should happen when a user clicks a button and submits a form rather than 'clicking a link'. This code may be vulnerable to cross site request forgery (Someone could trick someone into clicking a link on another site that ran this action). Rails forms, if correctly implemented, are protected against this because they use a request forgery protection token.

最有可能要在此操作后,将用户重定向。渲染执行这样的行动(而不是重定向)后的网页被认为是不好的做法。

Most likely you want to redirect the user after this action. Rendering pages after executing actions like this (rather than redirecting) is considered bad practice.

这篇关于导轨 - 添加记录到连接表从控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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