Redirect to after successful ajax form

后端 未结 3 1911
执笔经年
执笔经年 2021-02-20 12:03

I\'ve got a form with remote => true. And right now my controller looks like:

  # POST /items
  # POST /items.json
  def create
    @item = @store.items.build(pa         


        
相关标签:
3条回答
  • 2021-02-20 12:11

    I use a combination of Rails responders to generate my response messages and some content in my <action>.js file.

    The content of — say update.js would look something like this:

    // Checks if the article slug has changed.
    // If it has the entire page should be reloaded at that new location.
    <%= reload_if_slug_changed @article, params[:id] %>
    
    // Displays the flash notices
    // See ApplicationHelper#js_flash_response
    <%= js_flash_response %>
    

    Where the different methods are defined in some helper (in my case my ApplicationHelper). The content of the different methods are as follows:

    def js_flash_response
      if flash.now[:notice].present?
        js = "$('#notice').html('#{flash.now[:notice]}').change();"
      elsif flash.now[:alert].present?
        js = "$('#alert').html('#{flash.now[:alert]}').change();"
      end
    end
    
    def reload_if_slug_changed object, expected_value
      "window.location.href = '#{url_for [:edit, object]}';" if object.slug != expected_value
    end
    

    The content of the flash messages are generated automatically by Rails responders and displayed with the now scope that deletes the from the flash hash, ensuring that if the user reloads (after the flash has been displayed) they will not reappear.

    I don't believe that you should ever make a form pointing to a restful create action a remote one, because you would always expect critical redirect, so in my case I only need to redirect if the url slug has changed.

    I hope that this helps. It's not a solution, but simply the way that I handled some of the same problems.

    Best regards.

    0 讨论(0)
  • 2021-02-20 12:12

    Under your scenario, here's how I would inject javascript into the page from a controller action. After you've completed the logic section of your action insert something like this:

    render :update do |page|
        page << "javascript_here"
    end
    

    This should allow you to insert you window.location or create a javascript flash method and call it when your create method executes correctly.

    If you're looking to DRY up your controller actions, I would recommend looking into this Railscast about make_resourceful. Make_resourceful automagically performs each core activity for each action. It also allows you to tap into the hooks that they've created such as before :create, after :create, response_for :create, and after :create_fails. By using this gem, you can run code based on the success or failure of your methods and have finer grained control over them.

    In addition to this, you should be able to initialize a create.js.erb and create_fails.js.erb in your view file, include a format.js without anything passed to it in your controller, and Rails will automagically run that file that contains javascript depending on if the controller action executed successfully.

    0 讨论(0)
  • 2021-02-20 12:30

    I think it might be impossible. The response to a Ajax request is processed by XMLHttpRequest. If a 3xx response is returned, XMLHttpRequest will follow the redirect itself, if the URL is of same origin. No matter how you set the headers, the browser cannot be aware of that. So the only way could be changing window.location with some Javascript.

    0 讨论(0)
提交回复
热议问题