如何限制在 Ruby on Rails 中创建帖子的特定用户进行编辑?

发布于 2024-11-08 06:08:39 字数 588 浏览 0 评论 0原文

我已经使用 ruby​​ 创建了一个消息发布功能,我现在遇到的问题是编辑帖子的功能可用,但无论是谁发布了原始帖子,任何单个用户都可以访问它。我怎样才能使编辑“链接”只能由原始海报查看或访问或使用。如果需要任何代码请询问,谢谢。

这是我现在得到的错误:

undefined local variable or method `current_user' for #<ActionView::Base:0x4cdbb48>
Extracted source (around line #7):

4:     <dd>
5:     <%= car.name %><br />
6:     <%= car.description %><br />
7:     <%= link_to('edit', edit_post_path(@car)) if current_user.cars.include?(@car) %>
8:     <%= link_to "Delete", 
9:     :controller => :car, 
10:    :action => :delete,

I have created a message posting feature using ruby the problem I now have is the function editing the post is available but it is accessible by ever single user no matter who posted the original post. How can I make it so the edit 'link' is only viewable or accessible or usable by the original poster. If any code is required please ask, thanks.

This is the error I get now:

undefined local variable or method `current_user' for #<ActionView::Base:0x4cdbb48>
Extracted source (around line #7):

4:     <dd>
5:     <%= car.name %><br />
6:     <%= car.description %><br />
7:     <%= link_to('edit', edit_post_path(@car)) if current_user.cars.include?(@car) %>
8:     <%= link_to "Delete", 
9:     :controller => :car, 
10:    :action => :delete,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-11-15 06:08:39

如果您有办法获取用户对象,那么您应该与您的帖子建立如下关系:

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user
end

这可以让您获取并构建以用户为范围的帖子,如下所示:

User.find(1).posts #=> [<Post id:1>, <Post id:2>, <Post id:4>]

Enumerable#detect 可以循环遍历数组并返回第一个项目在块中评估为 true,如下所示:

@post = User.find(1).posts.detect do |post|
  post.id == 4
end
@post #=> <Post id:4>

大多数 Rails 应用程序都有某种帮助程序来访问当前登录的用户。如果没有,我建议您在 application_controller 中放置一个,如下所示:

def current_user
  @current_user ||= User.find(whatever_you_do_to_get_the_logged_in_user)
end

将编辑和更新操作中的查找范围限制为用户拥有的帖子,如下所示:

def edit
  @post = current_user.posts.detect{|p| p.id == params[:id] }
end

并在您的视图中:

<%= link_to('edit', edit_post_path(@post)) if current_user.posts.include?(@post) %>

If you have a way of getting at the user object, you should have relationships to your posts like this:

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :user
end

This lets you get and build posts scoped to the user like so:

User.find(1).posts #=> [<Post id:1>, <Post id:2>, <Post id:4>]

Enumerable#detect can loop over an array and return the first item that evalutates to true in a block like so:

@post = User.find(1).posts.detect do |post|
  post.id == 4
end
@post #=> <Post id:4>

Most Rails apps have some kind of helper to access the currently logged-in user. If not I suggest you put one in application_controller like this:

def current_user
  @current_user ||= User.find(whatever_you_do_to_get_the_logged_in_user)
end

Scope the find in edit and update actions to posts the user owns like this:

def edit
  @post = current_user.posts.detect{|p| p.id == params[:id] }
end

and in your view:

<%= link_to('edit', edit_post_path(@post)) if current_user.posts.include?(@post) %>
桃气十足 2024-11-15 06:08:39

您需要将帖子链接到模型中的创建用户,并检查编辑操作中的所有权和/或权限。

最好使用 cancan gem 来处理权限,并查看 这个railscast 使用它来实现与您的需求非常相似的保护。

Railscast 将 cancan 与 authlogic 结合使用,但它也可以与其他身份验证一起使用。

You need to have the post linked to the creating user in your model, and to check ownership and/or permission in the edit action.

It might be good to use the cancan gem for handling the permission, and to look at this railscast on using it to do a very similar protection to your need.

The railscast uses cancan with authlogic, but it can work with other authentication as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文