【问题标题】:How to pass parameter through form_for when it involves more than models涉及模型多的时候,form_for如何传参
【发布时间】:2016-02-27 01:31:40
【问题描述】:

我对 Rails 很陌生,正在尝试自己为 ToDo 列表创建一个应用程序。

如何在下面的表格中传递参数“类别名称”和“任务描述”。

users/show/html.erb

<aside class="col-md-8">
  <section>
    <%= render 'tasks/new' %><br> 
  </section>
  <section> 
    <% if @user.tasks.any? %>
      <ol class="tasks">
        <%= render @tasks %>
      </ol>
    <% end %>
  </section>
</aside>

tasks/new.html.erb

    <div class="form-inline">
  <%= form_for([@user, @user.tasks.build]) do |f| %>      
      <select id="selectUser" class="form-control selectWidth">
        <option class="">Select Category</option>
          <% @user.categories.all.each do |category| %>
            <option class="">
              <%= category.name %>
            </option>
          <% end %>
      </select>            
        <%= f.label :description %>
        <%= f.text_area :description, class: 'form-control' %>    
        <%= f.submit %>
    <% end %>
</div>

控制器/tasks_controller

    class TasksController < ApplicationController

 def create
   @user = User.find(params[:id])
   @category = Category.find_by(name: params[:category])

   @task = @user.tasks.create(task_params)
   redirect_to user_path(@user)
 end 

 private

  def task_params
    params.require(:task).permit(@category.name, :description)
  end 

end

任务表中的列是

  1. 身份证
  2. 说明
  3. user_id(参考 users 表)
  4. category_id(参考类别表)

类别表中的列是:

  1. 身份证
  2. 姓名
  3. user_id(对用户表的引用)

我试图从新任务表单中传递的参数是来自category 表的name 和来自tasks 表的description

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    我猜你想对任务而不是用户进行分类。所以首先你应该去掉类别表中的user_id

    应该有 3 个表将类别连接到任务。 Task、Category 和 TaskCategories 将这些表与 has_many :through 关系连接成两种方式,例如 http://guides.rubyonrails.org/association_basics.html at 2.8(您选择 has_many through 版本)。

    然后您在控制台或通过表单手动创建类别。我猜你想确定类别,用户可以从中选择,所以我推荐 console 或 seed.rb。以下是创建类别的示例:

    Category.create!([{name: "IoT"}, {name: "AI"}, {name: "FinTech"}, {name: "Automotive"}, {name: "Health & Welness"}, {name: "IT & Data Science"}, {name: "FinTech"}, {name: "Education"}, {name: "Retail"}]) 
    

    一旦它们在数据库中并设置了表之间的连接(数据库中的外键和模型中的关联),您就可以在任务表单中使用它:

    <div class="form-group">
       <%= f.collection_select :category_ids, Category.all.order(name: :asc), :id, :name, {}, { multiple: true } %>  #with multiple you can choose more at once
    </div>
    

    不要忘记任务模型中的强参数:

     ....category_ids: []... #with plural you can choose more at once
    

    回答您的最后一句话:您在集合选择中显示名称,但您传递了category_id 因为TaskCategories 表没有category.name,因为它在类别表中。 task.description 很简单,因为这是一个任务表单,所以你不应该对此有任何问题,只是不要忘记在任务强参数中使用 :description

    附:您可以使用 simpy form_for @task 并将其放入您的控制器中:

    def new
      @task = Task.new
    end
    
    def create
      @task = current_user.tasks.new(task_params)
    end
    

    【讨论】:

    • 我想对任务进行分类,同时我想将该类别链接到用户(每个用户都应该能够创建自己的类别)。我将任务控制器中的创建操作更改为tasks/_new.html.erb&lt;%= form_for([@user, @user.tasks.build]) do |f| %&gt; 中的@task = current_user.tasks.new(task_params) 表单现在我没有收到任何错误,但它没有创建新任务
    • 您在日志中看到了什么?我仍然没有真正得到您想要通过类别实现的目标。可以举个例子吗?
    • 我发现我错过了@task.save 命令。我现在可以创建新任务了。我正在使用应用程序中的类别对“待办事项”或“购物”等任务进行分类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2012-01-12
    • 1970-01-01
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多