我的名字叫卡洛斯(Carlos),我在写作,也是Rails 6的新手。
我正在尝试在应用程序上显示视图中从表单获取的变量,但看不到它。
我正在使用form_with将参数(不带模型)发送到Controller的URL。
Meteo View上的表单(app \ views \ meteo \ form.html.erb):
将参数“:condition_met”发送到“简报显示控制器URL”:
<div class="form-group text-center">
<%= form_with url: briefing_show_path, id: "met" do |fo| %>
<div class="field">
<%= fo.label "Condición Actual" %><br>
<%= fo.text_area(:condition_met) %>
</div>
<%= fo.submit "Enviar" %>
<% end %>
</div>
简报控制器(app \ controllers \ briefing_controller.rb):
尝试在“ @condition_met”上保存“ [:condition_met]”的值:
def show
if params.has_key?(:condition_met)
@condition_met = params[:condition_met]
end
end
简介节目视图(app \ views \ briefing \ show.html.erb):
尝试在视图上显示“ [:condition_met]”的值:
<div>
<%= "#{@condition_met} " %>
</div>
当我单击提交按钮时,控制台实际上显示哈希已发送到URL:
Started POST "/briefing/show" for 127.0.0.1 at 2020-02-22 10:24:42 -0300
Processing by B**riefingController#show** as JS
**Parameters: {"condition_met"=>"Test"**, "commit"=>"Enviar"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 5], ["LIMIT", 1]]
↳ app/controllers/briefing_controller.rb:4:in `show'
Rendering briefing/show.html.erb within layouts/application
Rendered briefing/show.html.erb within layouts/application (Duration: 1.1ms | Allocations: 211)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered partials/_nav.html.erb (Duration: 2.7ms | Allocations: 5906)
Completed 200 OK in 68ms (Views: 64.3ms | ActiveRecord: 0.1ms | Allocations: 25974)
在以下视图上未显示任何内容:<%=“#{@ condition_met}”%>。
我在网络上尝试了一些解决方案,但没有任何效果。
问候!
UPADTE:
这是我的route.rb:
get "meteo", to:"meteo#show"
get "meteo/form", to:"meteo#form"
post "meteo/form", to:"meteo#form"
get 'briefing/index'
get 'briefing/show'
post "briefing/show", to:"briefing#show"
我使用了浏览器检查器,并且看到在进行POST时,检查器会在答案中显示变量,但是当我转到视图页面时,它没有显示:
如果我使用<%= @ condition_met.inspect%>,我会得到“无”
form_with
默认情况ajax
下是您未处理的请求,请show
添加local: true
以解决该问题
<%= form_with url: briefing_show_path, local: true, id: 'met' do |fo| %>
试试看。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句