Questions about @dhh code on Writing Software Well for Controllers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Questions about @dhh code on Writing Software Well for Controllers

David McDonald
I was struck recently by how simplified the code is that @dhh was showing off in his youtube videos recently.  I'm trying to learn from it and get my code refactored properly.  The questions I'm finding are more of how his code lines up to what the rails generator creates. Below is an example of both of a #create action.

I realize that I probably shouldn't be comparing these two as they're made for different purposes, but from viewing both I can't help but see that one is much more code.

Rails Generated Controller #create

def create
  @post = Post.new(post_params)

  respond_to do |format|
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
      format.json { render :show, status: :created, location: @post }
    else
      format.html { render :new }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

DHH code from (MessagesController#create)

def create
  @recording = @bucket.record new_message, parent: @parent_recording, status: status_param, 
    subscribers: find_subscribers, category: find_category

  if guided_setup?
    complete_guided_setup
  else
    respond_to do |format|
      format.any(:html, :js) { redirect_to edit_subscriptions_or_guided_recordable_url(@recording) }
      format.json { render :show, status: :created }
    end
  end
end

By looking at the two samples, I think the part I really feel I can improve upon is perhaps the "respond_to" blocks.  I began thinking through trimming down my code, but quickly noticed that there isn't a "format.html { render :new }" or a "if @post.save" example for this instance.  Anyone have thoughts on this? Maybe if he were to have shown a block that more closely represented the PostsController#Create, I would've seen something closer to the generated code.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/79058998-8327-4885-922c-3b8a4407d98a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Questions about @dhh code on Writing Software Well for Controllers

Rob Jonson
the top example looks really clear to me.

I can see immediately what it is doing and why.

there is almost no duplication and it is doing 'controller-type' work.

what don't you like about it?

On Sunday, 4 March 2018 15:12:23 UTC, David McDonald wrote:
I was struck recently by how simplified the code is that @dhh was showing off in his youtube videos recently.  I'm trying to learn from it and get my code refactored properly.  The questions I'm finding are more of how his code lines up to what the rails generator creates. Below is an example of both of a #create action.

I realize that I probably shouldn't be comparing these two as they're made for different purposes, but from viewing both I can't help but see that one is much more code.

Rails Generated Controller #create

def create
  @post = Post.new(post_params)

  respond_to do |format|
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
      format.json { render :show, status: :created, location: @post }
    else
      format.html { render :new }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

DHH code from (MessagesController#create)

def create
  @recording = @bucket.record new_message, parent: @parent_recording, status: status_param, 
    subscribers: find_subscribers, category: find_category

  if guided_setup?
    complete_guided_setup
  else
    respond_to do |format|
      format.any(:html, :js) { redirect_to edit_subscriptions_or_guided_recordable_url(@recording) }
      format.json { render :show, status: :created }
    end
  end
end

By looking at the two samples, I think the part I really feel I can improve upon is perhaps the "respond_to" blocks.  I began thinking through trimming down my code, but quickly noticed that there isn't a "format.html { render :new }" or a "if @post.save" example for this instance.  Anyone have thoughts on this? Maybe if he were to have shown a block that more closely represented the PostsController#Create, I would've seen something closer to the generated code.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/920d5f49-2545-47e7-aafd-ead503330f09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Questions about @dhh code on Writing Software Well for Controllers

David McDonald
I guess I'm just looking at it from the idea of "less code is usually better code".  And also I'm trying to learn from others that have a better skill set and/or experience than I do.  But as I mentioned, perhaps I'm taking a snippet of code from one of his controllers that do not relate to the piece of generated code.

On Monday, March 5, 2018 at 4:16:46 AM UTC-5, Rob Jonson wrote:
the top example looks really clear to me.

I can see immediately what it is doing and why.

there is almost no duplication and it is doing 'controller-type' work.

what don't you like about it?

On Sunday, 4 March 2018 15:12:23 UTC, David McDonald wrote:
I was struck recently by how simplified the code is that @dhh was showing off in his youtube videos recently.  I'm trying to learn from it and get my code refactored properly.  The questions I'm finding are more of how his code lines up to what the rails generator creates. Below is an example of both of a #create action.

I realize that I probably shouldn't be comparing these two as they're made for different purposes, but from viewing both I can't help but see that one is much more code.

Rails Generated Controller #create

def create
  @post = Post.new(post_params)

  respond_to do |format|
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
      format.json { render :show, status: :created, location: @post }
    else
      format.html { render :new }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

DHH code from (MessagesController#create)

def create
  @recording = @bucket.record new_message, parent: @parent_recording, status: status_param, 
    subscribers: find_subscribers, category: find_category

  if guided_setup?
    complete_guided_setup
  else
    respond_to do |format|
      format.any(:html, :js) { redirect_to edit_subscriptions_or_guided_recordable_url(@recording) }
      format.json { render :show, status: :created }
    end
  end
end

By looking at the two samples, I think the part I really feel I can improve upon is perhaps the "respond_to" blocks.  I began thinking through trimming down my code, but quickly noticed that there isn't a "format.html { render :new }" or a "if @post.save" example for this instance.  Anyone have thoughts on this? Maybe if he were to have shown a block that more closely represented the PostsController#Create, I would've seen something closer to the generated code.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d942cceb-85d0-479f-8d23-6b277a243b60%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.