[rspec-users] Could anyone please help with rspec/nested resource behavior checking?

Fischer, Daniel daniel at danielfischer.com
Wed May 30 20:15:18 EDT 2007


Chris,

class PostsController < ApplicationController
  before_filter(:get_user)
  before_filter :login_required
  def index
    @posts = @user.posts.find(:all)

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @posts.to_xml }
    end
  end

  def show

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @post.to_xml }
    end
  end

  def new
    @post = Post.new
  end

  def edit
  end

  def create
    @post = Post.new(params[:post])
    @post.user_id = @user.id
    respond_to do |format|
      if @post.save
        flash[:notice] = 'Post was successfully created.'
        format.html { redirect_to post_url(@user, @post) }
        format.xml  { head :created, :location => post_url(@user, @post) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @post.errors.to_xml }
      end
    end
  end

  def update

    respond_to do |format|
      if @post.update_attributes(params[:post])
        flash[:notice] = 'Post was successfully updated.'
        format.html { redirect_to post_url(@user, @post) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @post.errors.to_xml }
      end
    end
  end

  def destroy
    @post.destroy

    respond_to do |format|
      format.html { redirect_to posts_url }
      format.xml  { head :ok }
    end
  end

  private
  def get_user
    @user = User.find(params[:user_id])
    @post = @user.posts.find(params[:id]) if params[:id]
  end
end

Hope that clears it up :)

On 5/30/07, Chris Anderson <jchris at mfdz.com> wrote:
> Daniel,
> Usually the route specs have nothing to do with the controller's code.
> Your controller may not be issuing the proper find, but your route
> setup has no effect on the do_get method in your second spec.
>
> More helpful would be to see your controller code.
>
> Chris
>
> On 5/30/07, Fischer, Daniel <daniel at danielfischer.com> wrote:
> > My problem has been listed here:
> > http://railsforum.com/viewtopic.php?pid=25439#p25439
> >
> > Don't think it would be required to completely re-type it here :)
> >
> > Thanks!
> >
> > --
> > -Daniel Fischer
> >
> > http://danielfischer.com - Geek Blog
> > http://abigfisch.com - Portfolio
> > http://writersbeat.com - Writing Community
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
>
>
> --
> Chris Anderson
> http://jchris.mfdz.com
>


-- 
-Daniel Fischer

http://danielfischer.com - Geek Blog
http://abigfisch.com - Portfolio
http://writersbeat.com - Writing Community


More information about the rspec-users mailing list