如何将params添加到form_for,但又不想将它们添加到模型中?

时间:2017-11-17 13:40:16

标签: ruby-on-rails

我有两个名为quizquestion

的模型

quiz has_many questions

我想在quizform_for添加参数,因此我可以在quizs_controller中使用它们。例如fromto,用户可以输入整数来获取from xx to xx范围内的问题。

但我不想在from模型中添加toquiz参数。

我想知道如何实现这一点。

感谢。

修改

测验/ new.html.erb

<%= form_for [:admin, @quiz] do |f| %>


  <div class="form-group">
    <%= f.label :title, "Title:" %>
    <%= f.text_field :title, :class => "form-control" %>
  </div>


  <div class="form-group">
    <%= f.label "Range:" %>

    <!-- I want to know how to add `from` and `to` params here -->

  </div>


  <%= f.submit "Submit", data: { disable_with: "Submitting..." }, class: "btn btn-primary" %>
  <%= link_to "Cancel", admin_quizzes_path %>

<% end %>

quizzes_controller.rb

def new
  @quiz = Quiz.new
end

def create
  @quiz = Quiz.new(quiz_params)
  @questions = @quiz.questions
  # And I want to gain the `from` and `to` here to **FILTER** THE questions

  if @quiz.save
    redirect_to admin_quizzes_path
  else
    render :new
  end
end

1 个答案:

答案 0 :(得分:0)

在表单内添加:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img  class = "<?php echo $i ?>" src="" alt="" height="22" width="30">
<br/>
<label> AD Mgr </label>
<br/>
  <select class="acc">
    <option value="1" selected>1</option>
    <option value="2">2</option>
    <option value="3">3</option>
  </select>

<div class="form-group"> <%= label_for :from %> <%= number_field_tag :from, :class => "form-control" %> </div> 相同。

在您的控制器中,您可以将它们引用为:

to

这对你有用吗?