令人惊讶的是,我无法通过这个简单的form_tag向我发送一段时间......这对我的上一个项目起作用了。我做了它的副本,使用了不同的Ruby版本,现在它不起作用。试过很多东西,但它不起作用。
我有这个块:
<div class="row">
<div class="col-md-8">
<div id="light-pagination" class="pagination"></div>
</div>
<div class="col-md-4" style='float:right'>
<%= form_tag("/go_to_show_question", class: "form-inline", method: "get") do %>
<%= hidden_field_tag(:id, @question_group.id) %>
<%= search_field_tag :question, '', size: 5, class: 'form-control' %>
<%= submit_tag r("tests.go_to_question"), class: 'btn btn-default' %>
<% end %>
</div>
</div><br>
我正在输入一个数字,然后点击“转到问题”。它只发送:
{"controller"=>"question_groups", "action"=>"go_to_show_question", "locale"=>"en"}
&#39; id&#39;和&#39;问题&#39;?他们为什么不一起被送到一起?
我相信这是一个愚蠢的东西,但我无法找到它...之前有效。我相信我没有改变它。
这是生成的HTML
<div class="row">
<div class="col-md-8">
<div id="light-pagination" class="pagination"></div>
</div>
<div class="col-md-4" style='float:right'>
<form class="form-inline" action="/go_to_show_question" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓" />
<input type="hidden" name="id" id="id" value="28" />
<input type="search" name="question" id="question" value="" size="5" class="form-control" />
<input type="submit" name="commit" value="Go to question" class="btn btn-default" />
</form>
</div>
</div><br>
路线:
Rails.application.routes.draw do
... other routes ...
scope ":locale", locale: /#{I18n.config.available_locales.join("|")}/ do
... other routes ...
get 'go_to_show_question' => 'question_groups#go_to_show_question'
... other routes ...
end
get '*path', to: redirect("/#{(I18n.locale)}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.locale}/" }
match '', to: redirect("/#{(I18n.locale)}"), via: [:get, :post, :put, :delete]
end
控制器:
class QuestionGroupsController < ApplicationController
def go_to_show_question
redirect_to show_question_group_path(:id => params[:id], :question => (params[:question].to_i - 1))
end
end
此请求到达我的控制器。但没有身份证,问题参数。
这是Log
Processing by QuestionGroupsController#go_to_show_question as HTML
Parameters: {"locale"=>"en"}
User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
{"controller"=>"question_groups", "action"=>"go_to_show_question", "locale"=>"en"}
Redirected to http://localhost:3000/en/show_question_group?question=-1
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)
答案 0 :(得分:1)
找到答案:
<%= form_tag("/en/go_to_show_question", class: "form-inline", method: "get") do %>
<%= hidden_field_tag(:id, @question_group.id) %>
<%= search_field_tag :question, '', size: 5, class: 'form-control' %>
<%= submit_tag r("tests.go_to_question"), class: 'btn btn-default' %>
<% end %>
我只是没有在我的表单URL前面添加语言环境。 即使通过我的路线工作正常,但是:locale范围解析了请求并删除了我的参数。
就像
Started GET "/go_to_show_question?utf8=%E2%9C%93&id=28&question=88&commit=Go+to+question" for 127.0.0.1 at 2017-01-07 16:41:08 +0100
解析为
Started GET "/en/go_to_show_question" for 127.0.0.1 at 2017-01-07 16:41:08 +0100
Processing by QuestionGroupsController#go_to_show_question as HTML
Parameters: {"locale"=>"en"}