Rails-从表单选择下拉列表中将参数传递给控制器 [英] Rails - pass a parameter to controller from form select dropdown

查看:44
本文介绍了Rails-从表单选择下拉列表中将参数传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型: Team Quest .创建新团队时,我会下拉所有任务.选择任务后,我要显示有关该任务的信息.

I have 2 models: Team and Quest. When creating a new team, I have a drop-down of all the quests. When a quest is selected, I want to display information about the quest.

我的理解是,表单中的所有内容都在客户端,并且需要AJAX才能将选定的任务传递到服务器端.我的代码基于堆栈溢出答案.

My understanding is that everything in the form is on the client side and AJAX is required to pass the selected quest to the server side. My code is based on this Stack Overflow answer.

这是我构造表单的方式:

Here is how I constructed my form:

app/views/teams_form.html.erb

<%= form_for(@team) do |f| %>
  <fieldset>
    <ol>
      <li>
        <%= f.label :name %>
        <%= f.text_field :name %>
      </li>
      <li>
        <%= f.label :quest_id %>
        <%= f.select :quest_id, 
                     options_from_collection_for_select(@quests, :id, :name), 
                     {}, {remote: true, url: '/teams/new', method: 'get'} %>
      </li>
      <% if @x != nil && @x.id != nil %>
        <li><%= @x.id %></li>
      <% end %>
    </ol>
    <p>
      <%= f.submit %>
    </p>
  </fieldset>
<% end %>

app/controllers/team_controller.rb

def new
  @team = Team.new
  @quests = Quest.all
  respond_to do |format|
    if params[:quest_id] != nil
      @x = Quest.find(params[:quest_id])
    end
    format.html #new.html.erb
    format.json
    format.js
  end
end

我的目标是将:quest_id 参数从表单传递到 @x 变量,然后在表单中使用它.

My goal was to pass the :quest_id parameter from the form to the @x variable and use that in the form.

这什么也没产生.我没有在控制器中获取参数,也不确定要丢失什么.

This has produced nothing. I'm not getting the parameter in the controller and I'm not sure what I'm missing.

推荐答案

最终解决了这个问题,如果有人看到它并且遇到相同的问题,想发布它:

Finally got this working, wanted to post if anyone sees this and is having the same problem:

我被提出了一个单独的AJAX请求

I went with a separate AJAX request since that was being suggested

app/views/teams_form.html.erb

<script>
   $(document).ready(function() {
    $('#team_quest_id').change(function() {
       $.ajax({
         url: "/teams/new",
         data: {quest_id: $("#team_quest_id option:selected").val()},
         dataType: "script",
         method: "get",
         success: function(r){}
       });
     });
   });
</script>

我移动了参数分配的位置

I moved the location of the parameter assignment

app/controllers/team_controller.rb

def new
  @team = Team.new
  @quests = Quest.all
  if params[:quest_id] != nil
    @x = Quest.find(params[:quest_id])
  end
  respond_to do |format|
    format.html #new.html.erb
    format.json
    format.js
  end
end

最重要的是-我创建了一个js文件来呈现我的表单

And most importantly - I created a js file to render my form

app/views/new.js.erb

$('#new_team').html("<%= j (render 'form') %>");

此视频非常有用

这篇关于Rails-从表单选择下拉列表中将参数传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆