开发者

After deleting a record in Rails 3, the refreshed view isn't updated

I'm dealing with a basic one to many rel开发者_Python百科ation where I'm deleting a record on the many side. The models are "places" and "search_terms" where places has_many search_terms. When I create a new record, the view is updated and the new search_term appended to the list. However, when I delete a search_term record the view is not refreshed even though it deletes the record and runs the "show" method for Place.

I'm quite new to rails 3 so can't really figure out whats going on here...

Cheers, Gearoid.

Edit: the search_terms controller destroy method:

def destroy
    @search_term = SearchTerm.find(params[:id])
    @search_term.destroy

    @place = Place.find(params[:place_id])
    redirect_to place_path(@place)    
end

The places controller show method:

def show
    @place = Place.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @place }
     end
end


I might be misunderstanding you, could you post your controller's code?

Is this happening over ajax? If not, can you redirect to the Show instead of just re-rendering it? That's probably a preferred experience for the user anyway.

UPDATE

Ok, if this is going over ajax, then the problem is simple. Your destroy action is only expecting a normal browser event and doing a redirect_to call. The ajax call doesn't know how to handle it and just sits there. You can probably see the redirect code in something like Firebug.

I'm not super familiar with jquery-rails (I prefer to write all my js myself because I'm anal). You can have the destroy action return a js format like so:

def destroy
    @search_term = SearchTerm.find(params[:id])
    @search_term.destroy

    @place = Place.find(params[:place_id])
    respond_to do |format|
        format.html { redirect_to place_path(@place) }
        format.js { render :nothing => true }
    end
end

That will give the ajax caller the ok signal that it has done its thing. Your javascript will still have to intelligently handle this response though, like remove the element from the DOM.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜