简体   繁体   English

在Ruby on Rails中引用当前用户

[英]Referencing the current user in Ruby on Rails

So I've used the logic in the "authentication from scratch" railscast at http://railscasts.com/episodes/250-authentication-from-scratch and it seems to work and I can stick a "You are logged in as..." message at the top of the page. 所以我在http://railscasts.com/episodes/250-authentication-from-scratch上使用了“从头开始验证”railscast中的逻辑,它似乎工作,我可以坚持“你登录为。 ..“页面顶部的消息。

But if I want to do something like log who submitted a post I've hit a bit of a wall. 但是,如果我想做一些像提交帖子的日志这样的事情,我会碰到一点墙。

I don't want to submit it through a hidden field in the new post form, because I guess there are security issues with that. 我不想通过新帖子中的隐藏字段提交它,因为我猜这有安全问题。

I don't want to use the "belongs to" logic in the rails tutorial at http://ruby.railstutorial.org/ruby-on-rails-tutorial-book because although it would technically work here, I might in the future need to log who created an entry where the "belongs to" relationship doesn't exist. 我不想在http://ruby.railstutorial.org/ruby-on-rails-tutorial-book的rails教程中使用“属于”逻辑,因为虽然它在技术上可以在这里工作,但我可能在将来需要记录谁创建了一个“属于”关系不存在的条目。

What I tried to do was create a "before save" function call in my post model that assigns a "created_by" value, but I guess models can't access the current_user that was created as per the authentication railscast. 我试图做的是在我的post模型中创建一个“before save”函数调用,它指定一个“created_by”值,但我猜模型不能访问根据认证railscast创建的current_user。

So now I've got no idea how to do something like this. 所以现在我不知道怎么做这样的事情。

EDIT: New to Ruby, new to ERD, all that, but what I mean by the belongs to relationship doesn't exist is if there were, say, a rating system for posts, each rating would belong to a post. 编辑:Ruby的新手,ERD的新手,所有这些,但我所说的属于关系不存在的是,如果有一个评级系统的帖子,每个评级都属于一个帖子。 But I'd also want to log who submitted each rating. 但我也想记录提交每个评级的人。

If I understand well the problem you have is the fact you can't see which user is currently loggen using current_user helper method in your model, you can just set the created_by attribute of the post in the Post controller before to save it, something like: 如果我理解你的问题是你无法在模型中使用current_user帮助方法看到哪个用户当前是loggen,你可以在Post控制器中设置post的created_by属性然后保存它,类似于:

def create
  @post = Post.new(params[:post])
  @post.created_by = current_user.id
  if @post.save
    redirect_to whereyouwant_url, :notice => "Post successfully created"
  else
    render "new"
  end
end

(alternative to Aldo 'xoen' Giambelluca answer) (替代Aldo'xoen'Giambelluca回答)

I think you should use a belongs_to relationship as explained by loosecannon, but if you really don't want, add the following method in your User class: 我认为您应该使用loosecannon解释的belongs_to关系,但如果您真的不想要,请在User类中添加以下方法:

def new_post(params)
  post = Post.new(params)
  post.created_by = id 
  post
end

and in your controller: 并在您的控制器中:

def create
  @post = current_user.new_post(params[:post])
  if @post.save
    redirect_to @post, notice: 'Post successfully created'
  else
    render 'new'
  end
end

Ok, not sure what complications exist, but here goes: 好的,不确定存在哪些并发症,但这里有:

so addind a belongs_to makes the post have a field called user_id that is a foreign key for the user's id. 所以addind a belongs_to使帖子有一个名为user_id的字段,它是用户id的外键。 if you added a created by, this would do the same thing, except you would have to program the updating and derefreneces yourself. 如果你添加了一个创建的,这将做同样的事情,除了你必须编程更新和derefreneces自己。 I think that this is probably the best way to get this functionality, and you would just have to add it later to the parts that don't have it, but rails makes it easy. 我认为这可能是获得此功能的最佳方式,您只需稍后将其添加到没有它的部件中,但rails可以轻松实现。

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user, :as => :created_by
end



post.created_by
user.posts

would give you access to that user, and is this is equivalent to using what you talked about, except rails would do its magic and add methods for you. 会让你访问该用户,这相当于使用你所谈论的内容,除了rails会发挥其魔力并为你添加方法。

EDIT: if this doesn't work for you, sorry your question wasn't clear on why you needed certain things, but you can always add belongs to relationship pretty easy, thats what I would recommend. 编辑:如果这不适合你,抱歉你的问题不明确为什么你需要某些东西,但你总是可以添加属于关系很容易,这就是我的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM