代码之家  ›  专栏  ›  技术社区  ›  Hemanth

Rails3:如何在没有用户交互的情况下向列插入值?

  •  0
  • Hemanth  · 技术社区  · 15 年前

    我有帖子和用户模型,我有很多( 帖子 一) 用户 协会。 我只想显示该用户(当前用户)创建的文章。 因此,我必须将当前登录的用户的ID注入到“用户ID”外键中。 创建过程中的后期模型。我用DEVICE作为我的认证系统。 如何实现这一目标的任何解决方案? 我使用的是Rails 3.0版。

    事先谢谢。

    2 回复  |  直到 15 年前
        1
  •  0
  •   ipsum    15 年前

    使用device,只需使用“当前用户”助手;)

    <% if user_signed_in? %> 
      <% current_user.posts.each do |post| %> 
        <%= post.title %>
      <% end %>
    <% end %>
    
        2
  •  0
  •   Samuel    15 年前

    在你 PostsController#create 方法,可以设置 Post user current_user (因为你用的是计谋)。

    def create
      @post = Post.create(params[:post]) do |p|
        p.user = current_user
      end
      #respond_with(@post) or Rails 2 equivalent
    end