【问题标题】:How can I populate a user_id param using current_user id if Author and user_id from dropdown if not如果作者和 user_id 从下拉列表中填充,我如何使用 current_user id 填充 user_id 参数
【发布时间】:2023-03-13 20:28:01
【问题描述】:

我正在尝试以作者身份填充 user_id 参数。我想要做的基本上是,如果您是具有角色作者的登录用户并且您创建了一本书,我希望它使用您的登录 ID 来填充该 user_id 参数,如果您以管理员用户身份登录,您可以创建一个书,但使用选择的用户/作者列表添加到书。

这是我得到的:

图书控制器

def create
    # raise params.inspect
    @book = Book.create(book_params)
    if current_user.role_id == '2'
      @book.user_id = current_user.id
    else
      @book.user_id = params[:user_id]
    end

    #binding.pry

    respond_to do |format|
      if @book.save
        format.html { redirect_to @book, notice: 'Book was successfully created.' }
        format.json { render action: 'show', status: :created, location: @book }
      else
        format.html { render action: 'new' }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

def update
    @book.user_id = current_user.id
    respond_to do |format|
      if @book.update(book_params)
        format.html { redirect_to @book, notice: 'Book was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

然后在我的表单视图中我有这个设置:

<% if current_user.role.name == "Author" %>
      <div id="author" class="field">
        <!-- <h3>Hey <%= current_user.name %> your signed in as an <%= current_user.role.name %></h3> -->
      </div>
    <% elsif %>
      <div id="author" class="field">
        <h3>Select author of book</h3>
        <%#= debug Book.book_authors %>

        <%= f.collection_select :user_id, User.order(:name),:id,:name, {:include_blank => false} %>
      </div>
    <% end %>

我想要做的就是让作者使用他们的登录数据创建/更新书籍以填充参数 user_id 并让管理员通过使用下拉列表而不是填充参数 user_id 来创建/更新书籍。

【问题讨论】:

  • 你的问题是什么,什么不起作用?
  • 它没有填充 user_id 参数
  • 我希望params[:book][:user_id] 中的user_id 不仅仅是params[:user_id]
  • 是的,我也试过了,但它仍然没有填充它。

标签: ruby-on-rails ruby ruby-on-rails-4 devise cancan


【解决方案1】:

你试图一次做太多事情。简化问题并让小部分工作。例如,您在更新控制器中使用 @book 而不先定义它。作为另一个示例,您在创建控制器中引用了 role_id (int)。您的表单引用了 role.name(字符串)。

首先让控制器工作,然后添加您的功能。

【讨论】:

    【解决方案2】:

    可能的问题/解决方案:

    1)

    if current_user.role_id == '2'  
    

    我假设您有一个角色模型和一个用户 has_one :role 关联。如果是这种情况,您不应该使用 '2' 而是使用不带单引号的 2。 foreign_key 不是字符串而是整数。如果您没有这样的模型/关联,那么您以后不能使用 user.role.name。

    2) 当您使用 Book.create(book_params) 时,您不仅会实例化一本书,而且会将其保存(如果有效)到数据库。您可以创建没有 user_id 的书吗?如果不是,那你就错了,因为你试图在分配作者之前创建那本书(当作者是填写表单的人时,她在提交时没有传递 user_id)。

    3) 主要嫌疑人是您的 book_params。检查服务器日志以查看您是否传递了 user_id 参数,但它被阻止为未经许可。检查您是否在参数中包含 :user_id....permit(:user_id)

    其他cmets:

    a) 我建议您将 Book.create 替换为 Book.new。但如果保留 book.create,则不需要以下内容:

    ...
    else
      @book.user_id = params[:user_id]
    end
    

    (您已经使用该用户 ID 创建了图书。它也应该是 params[:book][:user_id]

    b) 正如马特所说,如果您没有在更新中使用 before_action 设置 @book,那么您还有另一个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      相关资源
      最近更新 更多