【问题标题】:Getting user ID when calling an action调用操作时获取用户 ID
【发布时间】:2015-05-05 23:54:32
【问题描述】:

我有一个表用于对书籍进行 CRUD 操作,用户也可以签入书籍(签出是由我的不同控制器完成的)

目前为 book 表签到,但我无法更新用户数据 - 他们借出多少本书的整数

这是我桌子的一部分:

    <% @books.each do |book| %>
    <tr>
        <td><%= book.title %></td>
        <td><%= book.author %></td>
        <td class ="onloan"><%= book.onloan %></td>
        <td><%= link_to 'show', book %> </td>
        <td><%= link_to 'edit', edit_book_path(book) %></td>
        <td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        <td><%=  link_to 'Check in', check_in_path(book)%></td>
    </tr>
    <% end %>

我想添加的是类似

@user = User.find(params[:id])
@user.books_on_loan - 1

这是我的控制器的def签入:

def check_in
 Book.find(params[:id]).update_attribute(:onloan, 0)
 redirect_to root_path
end

【问题讨论】:

  • 应该是update_attribute(onloan: 0),你应该考虑可能找不到记录。
  • 你的参数是什么?

标签: ruby-on-rails ruby


【解决方案1】:
def check_in
  @book = Book.find params[:id]

  if @book.update_attribute :onloan, 0
    @user = User.find params[:user_id] # Or change to get `@user.id` attribute
    @user.books_on_loan = @user.books_on_loan - 1 # Current value - 1
    @user.save
  end

  redirect_to root_path
end

这样可以,但是如果你想获取用户id,你需要指定你的params值。在路径或嵌套路由中都可以。

根据您的情况,我认为您没有嵌套路由。

所以隐藏属性或简单的check_in_path(book, user_id: your_desired_user_id) 都可以

【讨论】:

    【解决方案2】:

    如果您想在每次执行check_in 操作时更新@userbooks_on_loan 列/属性减去1,则需要更改方法以执行以下操作:

    def check_in
      @book = Book.find params[:id]
    
      if @book.update_attribute :onloan, 0
        @user = User.find params[:user_id] # Or change to get `@user.id` attribute
        @user.books_on_loan = @user.books_on_loan - 1 # Current value - 1
        @user.save
      end
    
      redirect_to root_path
    end
    

    编辑:

    您需要通过某种方式知道用户 ID,正如我在代码中评论的那样,您需要将 params[:user_id] 更改为包含用户 ID 的变量。

    如果您没有 HTTP 参数上的 ID,则需要在内部找到它。在您的示例中,您通过params[:id] 查找用户,但这实际上是此操作中图书 ID 的参数,它不能与用户相同。

    您的示例不足以确定如何实现 User ID,但我建议您将其存储在 Book 记录中。如果 Book 是唯一的并且只有一个 User 可以获取,例如,您可以在 Books 中添加一列 user_id,然后您可以从 Book 中获取 User ID。示例:

    def check_in
      @book = Book.find params[:id]
    
      if @book.update_attribute :onloan, 0
        @user = User.find @book.user_id
        @user.books_on_loan = @user.books_on_loan - 1 # Current value - 1
        @user.save
      end
    
      redirect_to root_path
    end
    

    这样,您还可以在 Book 模型上放置 belongs_to :user,并通过控制器上的 @book.user 获取用户。

    更好的是:如果您的用户模型上有has_many :books,您可以通过简单地执行@user.books.count 来获得用户的“借书”。这样做您可以使用更新方法删除您 Book 的 books_on_loan 属性。

    无论如何,您可能需要在控制器的 check_out 操作(我假设您有一个)上设置 Book 的 user_id

    如果您这样做,您的check_in 操作将如下所示:

    def check_in
      @book = Book.find params[:id]
    
      @book.onloan = 0
      @book.user_id = nil # There is no User with the Book
      @book.save
    
      redirect_to root_path
    end
    

    如果这仍然很复杂,您可以查看 Active Record 关联 指南:http://guides.rubyonrails.org/association_basics.html

    【讨论】:

    • 当我这样做时,我收到一个错误 - '找不到具有 'id'=' 的用户。问题是我无法获取用户参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    相关资源
    最近更新 更多