SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem redirecting using nested resources

    I've successfully created part of a RESTful application that uses nested resources. In my app, comments are nested in discussions and discussions are nested in topics. I altered routes.rb to reflect this:

    Code Ruby:
    map.resources :topics do |topics|
          topics.resources :discussions do |discussions|
            discussions.resources :comments
         end
      end

    This works well for discussions. I can view a list at index.rhtml, create a new discussion, destroy, show, etc.

    It works less well for comments. Most actions -- such as index, show, and destroy -- work fine. But the create action doesn't. That is, it successfully saves the comment and correctly saves it to the right discussion, but it fails to redirect a user to the show page for the saved comment.

    Following is the analogous (and working) CREATE action from my DiscussionController. Upon save, it redirects user to a url like "http://localhost:3000/topics/2/discussions/6/":

    Code Ruby:
    def create
        @discussion = Discussion.new(params[:discussion])
        @discussion.topic = Topic.find(params[:topic_id])
     
        respond_to do |format|
          if @discussion.save
            flash[:notice] = 'Discussion was successfully created.'
            format.html { redirect_to discussion_url(@discussion.topic, @discussion) }
            format.xml  { head :created, :location => discussion_url(@discussion.topic, @discussion) }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @discussion.errors.to_xml }
          end
        end
      end

    Here is the CREATE action from my CommentsController:
    Code Ruby:
    def create
        @comment = Comment.new(params[:comment])
        @comment.discussion = Discussion.find(params[:discussion_id])
     
        respond_to do |format|
         if @comment.save
            flash[:notice] = 'Comment was successfully created.'
            format.html { redirect_to (@comment.discussion, @comment) }
            format.xml  { head :created, :location => comment_url(@comment.discussion, @comment) }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @comment.errors.to_xml }
          end
        end
      end

    If I replace the format.html line to say "redirect_to comments_url" it works fine -- in that it saves and redirects to the list of comments for the given discussion. Why can't I redirect to show just the saved comment?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you not use comment_url(@comment.discussion, @comment)?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •