开发者

Rails - Refresh view after submit from different controller

开发者 https://www.devze.com 2023-02-17 21:18 出处:网络
I\'m having trouble refreshing a view after a form submit creates an Item on a different controller than the current view.

I'm having trouble refreshing a view after a form submit creates an Item on a different controller than the current view.

I have 2 models, Users and Items. The Users have multiple Items.

The Users 'show' view lists all of its Items. In the view I have a partial for rendering all the items into separate table rows:

<table class="items">
          <%= render @items%>
</table>

In the same view I have a div at the bottom that is used for creating new Items, this div contains a Items partial (which contains a form for creating new Items):

<div id="new_item_dialog">
    <%= render :partial => '/items/new_items' %>
</div>

This 'new_item_dialog' div is popped up into a dialog via jquery, and has :remote => true to perform the Item 'create' async:

$('#open-dialog').click(function(){
    $item_dialog.dialog('open');
});

$('.item_submit').click(function(){
    $item_dialog.dialog('close');       
});

The problem I'm running into is refreshing the User's 'show' page after the new Item has been submitted.

A simple redirect_to the User's show page from the Item's 'create' controller doesnt refresh the data

  def create
    @user = cur_user
    @item = @user.items.build(params[:ite开发者_运维知识库m])
    if @item.save
      redirect_to @user
    else 
      render 'new'
    end

I've also tried to use jQuery to load the new data:

$('.new_item').bind('ajax:success', function()
{
    $.get('/items/new_item', function (html) {
               $('.items').html(html);
           });
});

But I get the error message:

ActionView::MissingTemplate (Missing template items/show with 

Any help would be greatly appreciated!

Note: I've also tried a 'render @user' after the Item gets saved in the Item's 'create' controller, to re-render the User's 'show' view, but I get the same "MissingTemplate" error as above.

Updated: Also tried:

def create
    @user = cur_user
    @task = @user.item.build(params[:item])
    if @item.save
      render :partial => "items/item"

And (replacing the render in the above controller)

render :partial => @item

And

render :partial => 'views/items/item', :collection => @user.items

And

render :partial => 'views/items/item', :locals => { :variable => @item}

And

render :action => 'users/show', :layout => 'users'

Everyone of these returned the error:

ActionView::MissingTemplate (Missing template ..... with {:locale=>[:en, :en], :formats=>[:js, :"*/*"], :handlers=>[:rhtml, :rxml, :builder, :erb, :rjs]} in view paths "test/app/views"):
  app/controllers/items_controller.rb:11:in `create'

With Ajax like this: (doesnt ever get used though, everything fails on the rendering of the partial)

$('.new_item').bind('ajax:success', function()
    {
        $.ajax({
              success: function(html){
                $(".items").html(html);
              }
        });
    });


Either you refresh the whole page by redirecting(not rendering) using redirect_to user_path(@user)

OR

you could return the items partial when you create an item. what happens is this:

  1. create an item by submitting form through ajax
  2. item is created in your controller, render :partial => "/path/to/item"
  3. you will receive the html so in your jquery's ajax call, put a success function that will replace your current partial

    $.ajax({... ... success: function(html) { $(".items").html(html); //you could also use replaceWith(html) if you want to just render the whole table again, but put the table in the partial });

0

精彩评论

暂无评论...
验证码 换一张
取 消