我叫卡洛斯,我正在写作,我是Rails 6上的新手。
我正在开发我的应用程序,试图在视图上显示一个变量,从表单中获取,但我看不到它。
我使用form_with将params(没有模型)发送到Controller的URL。
流星视图(app\views\meteo\form.html.erb):上的表单
将params ":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>控制台实际显示,当我单击submit按钮:时,Hash被发送到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 %>,就会得到“0”。
发布于 2020-02-23 06:01:50
默认情况下,form_with是在show操作中未处理的ajax请求,请添加local: true以解决问题
<%= form_with url: briefing_show_path, local: true, id: 'met' do |fo| %>试试看。
https://stackoverflow.com/questions/60352694
复制相似问题