NOVA PC
NOVA PC

Reputation: 1

Ruby On Rails - Controller doesn't open page while been called

calculate.html.erb of "calc" controller:

<h2>
<%= @count%>
</h2>

Form, which do the get request:

            <%= form_with url: "/calc", method: :get do |form|%>
                <%= form.label :count, "Введите количество чисел" %>
                <%= form.number_field :count %>
                <%= form.submit "Начать"%>
            <% end %>

routes.rb:

Rails.application.routes.draw do
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
  root 'pages#index'
  get '/calc', to: 'calc#calculate'
end

In terminal, when I submit the form, I see this:

Started GET "/calc?count=5&commit=%D0%9D%D0%B0%D1%87%D0%B0%D1%82%D1%8C" for ::1 at 2023-01-23 16:51:04 +0300
Processing by CalcController#calculate as HTML
  Parameters: {"count"=>"5", "commit"=>"Начать"}
  Rendering text template
  Rendered text template (Duration: 0.0ms | Allocations: 4)
Completed 200 OK in 2ms (Views: 0.5ms | Allocations: 206)

calc_controller.rb:

 def calculate
    render plain: params[:count].inspect
    @count = params[:count]
  end

P.s The URL doesn't change when I submit form

Upvotes: 0

Views: 38

Answers (1)

spickermann
spickermann

Reputation: 106802

This line

render plain: params[:count].inspect

in your controller causes Rails to not render your view file but a plain text response.

Just remove that line and Rails should automatically render the view found in app/views/calc/calculate.html.erb

Upvotes: 1

Related Questions