Params not getting from submit form using javascript

I have a controller like this

class HomesController < ApplicationController
  def index
  end

  def cheking
    @result = params[:type]

    respond_to do |format|
      format.js
    end
  end
end

      

And the form on index.html.erb

<%= form_tag(checking_path, :remote => true, :format => :js, :method => :get)   do %>
    <%= select_tag "type", options_for_select([ "admin", "customer" ], "admin") %>
    <%= submit_tag "check now" %>
<% end %>
<div id="result"></div>

      

checking.js.erb

file

$("#result").html("<%= escape_javascript(render("result")) %>");

      

I want to put @result

in a render file_result.html.erb

<%= @result %>

      

but @result

not visible in browser when submitting form with type admin

.

log:

Started GET "/checking?utf8=%E2%9C%93&type=admin&commit=check+now" for 127.0.0.1 at 2014-09-14 01:26:13 +0400
Processing by HomesController#checking as JS
  Parameters: {"utf8"=>"βœ“", "type"=>"admin", "commit"=>"check now"}
  Rendered homes/_result.html.erb (0.4ms)
  Rendered homes/checking.js.erb (1.1ms)
Completed 200 OK in 5.0ms (Views: 4.8ms | ActiveRecord: 0.0ms)

      

+3


source to share


2 answers


In your check.js.erb file, you need to pass your variable so that your partial access can access it . You can do:

$("#result").html("<%=j render partial: 'result', locals: {result: @result} %>");

      



and in partial you need to use the result

#_result.html.erb
<%= result %>

      

+1


source


For further effort, @mandeep

let me give you some debugging ideas

To debug this application, there are 3 things to consider:

  • Are your parameters going to your controller?
  • Is your data being analyzed and your response displayed correctly?
  • Is your answer firing as needed?

Debug

To help you debug, you have provided us with great information:

Started GET "/checking?utf8=%E2%9C%93&type=admin&commit=check+now"

      



This is very good, as it means that your parameter :type

must be set (which would be the main problem for such an error). You can check this further using the following:

#app/controllers/homes_controller.rb
class HomesController < ApplicationController
   def checking
      Rails.logger.info params[:type]
   end
end

      

Assuming your data gets to your controller, the next thing you want to do is make sure it is parsed and your response is processed correctly. To do this, you need to check that your JS is actually called and if it fires:

#app/views/homes/checking.js.erb
alert("Firing");
$("#result").html("<%=j render partial: 'result', locals: { render: @render } %>");

#app/views/homes/_render.html.erb
<%= render %>

      

The above should raise an alert when a request is sent to your controller. This will give you a chance to check if the JS is actually working and you can see where the problem might be.

Finally, you will want to consider the response you are calling. Is it structured correctly? Perhaps your syntax is incorrect, so you cannot name the functionality you require. The above code should work for you in this regard

+1


source







All Articles