Ruby-on-rails: Get working but Post isn't

Viewed 43

In very short. When I have a form set as POST, the controller picks up the request, processes it and even starts to render the correct view. But the browser stays on the form page

When I switch the form to GET, it works (yes, I remembered to change the route from get to post and back)

Here is the log:

Started POST "/sooth/search" for 127.0.0.1 at 2022-07-02 13:43:40 -0700 Processing by SoothController#search as TURBO_STREAM Parameters: {"authenticity_token"=>"[FILTERED]", "search"=>{"name"=>"search keywords"}, "commit"=>"Save Search"} Rendering layout layouts/application.html.erb Rendering sooth/search.html.erb within layouts/application I am rendering the search html page

The line above is a log message in the search.html.erb page

Rendered sooth/search.html.erb within layouts/application (Duration: 1.0ms | Allocations: 148) Rendered layout layouts/application.html.erb (Duration: 6.6ms | Allocations: 2710) Completed 200 OK in 15ms (Views: 11.3ms | ActiveRecord: 0.0ms | Allocations: 4040)

BUT the search page is not displayed. Browser stays on the search form page.

Any hints deeply appreciated. (And as you have probably guessed, I am day 1 with rails)

EDIT:

class SoothController < ApplicationController
  include SoothHelper

  def index
    puts "sooth index"
    template = get_query_template('sooth_search')
    puts(template)
  end

  def search

    form_params = params[:search]
    puts 'searching' + form_params[:name].to_s
    render "sooth/search"
  end

end

ROUTES

Rails.application.routes.draw do
  Rails.application.routes.draw do
    get "/nlp_data", to: "nlp_data#index"
    get "/sooth", to: "sooth#index"
    post "/sooth/search", to: "sooth#search"

end
1 Answers

Your problem is you are trying to render the same page instead of redirecting to to the sooth page ,and secondly you cannot acces params directly in a post request, instead you must acces it from a strong param method

class SoothController < ApplicationController
  include SoothHelper

  def index
    puts "sooth index"
    template = get_query_template('sooth_search')
    puts(template)
  end

  def search

    form_params = sooth_params[:search]
    puts 'searching' + form_params[:name].to_s
    redirect_to "/sooth"
  end

  private 
  def sooth_params
   params.require(:sooth).permit(:search)
  end

end
Related