Reputation: 55
My name is Carlos, I'm writing and I'm new on Rails 6.
I'm working on my app trying to show a variable, got from a form, on a view but I can't see it.
I'm using form_with sending the params(without model) to an URL of a Controller.
Form on Meteo View (app\views\meteo\form.html.erb):
Sending the params ":condition_met" to "briefing show controller 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>
Briefing Controller (app\controllers\briefing_controller.rb):
Trying to save the value of "[:condition_met]" on "@condition_met":
def show
if params.has_key?(:condition_met)
@condition_met = params[:condition_met]
end
end
Briefing show view (app\views\briefing\show.html.erb):
Trying to show the value of "[:condition_met]" on the view:
<div>
<%= "#{@condition_met} " %>
</div>
The console actually shows that the Hash was sent to the URL when I click on the submit button:
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)
Nothing is shown on the view whith: <%= "#{@condition_met} " %>.
I tried some solutions on the web but nothing worked.
Regards!
UPADTE:
This is my 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"
I used the browser inspector and I saw that when the POST is made the inspector shows the variable in the answer but when I go to the view page it's not shown:
If I use <%= @condition_met.inspect %> I get "nil"
Upvotes: 4
Views: 541
Reputation: 17834
form_with
is by default ajax
request that you are not handling in show
action, add local: true
to solve the issue
<%= form_with url: briefing_show_path, local: true, id: 'met' do |fo| %>
Give it a try.
Upvotes: 3