简体   繁体   中英

Rails dynamic form remote true controller not responding

I am trying to establish a dynamic form on a contact's page. I would like on this page to be a link that says "add an email address" and then when I click on it, a form appears to enter email address.

So I used a link_to with remote true :

  = link_to "Add an email", add_email_path, id:'link-remote-link', remote: true

In my controller i specified :

  def add_email
    render layout: false
  end 

But when I receive my response with listening on ajax:sucess, layout is still their in the variable. But I just want the form add_email.html.haml

In order to try to know if the code in my controller was executed, I tryed to put a creation of an object in it. Fact is that it was never created.

Never the less, rails console writes "Processing by ContactsController#add_email as JS "

So...why is it not executed ?

Thank you :)

Layout

We set the layout in the application_controller to manage the ajax responses:

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   layout Proc.new { |controller| !controller.request.xhr? }
end

You may wish to try this - to see if it's the call in your controller which is rendering the layout.

Personally, I think your layout: false call is being overridden with some other element / part of your controller. I'd recommend checking to make sure this is the case

--

Controller

As you've stated, the case may be that your controller isn't being called, or processed correctly.

This could be caused by a number of issues, most notably from having incorrect routes, or some other dependency preventing the method from firing properly.

To clarify, I would make sure I have the following set up:

#config/routes.rb
resources :contacts do
   get :add_email, on: :collection
end

#app/controllers/contacts_controller.rb
class ContactsController < ApplicationController
   def add_email
      ...
   end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM