Redirect to after successful ajax form

后端 未结 3 1917
执笔经年
执笔经年 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: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.

提交回复
热议问题