Michael Becco

Joined

170 Experience
1 Lesson Completed
0 Questions Solved

Activity

Evening Chris,

I will be using AJAX in the next refactor. The manner in which I itereated through my test for this project made me realize I didn't have a graceful fallback when JS is turned off. This lead to my controllers looking like a can of worms and hence my question.

Ultimately I'll put this in a respond_to block, I just wasn't sure if there is a way to stream line this.

So... I'm having a higher level conceptual gap on how to deal with a scenario in which a form errs out on a page that has two forms relating to two different controllers.

My first knowledge gap to address is using instance variables in the else. Is there a more efficient or accepted means of populating the page after errors then making another hit to the DB?

The second knowledge gap to address is routing after an error. When I render timelines_index_path my routing becomes /alerts or /microposts and I understand what Rails is doing behind the scenes, I just am unclear what to actually do about that.

Here are my three controllers. Timelines list all micropost and alerts as well as the two forms for micropost and alerts .

class TimelinesController < ApplicationController
  layout 'timeline'
  before_action :authenticate_user!

  def index
    @microposts = Micropost.by_most_recent
    @micropost = Micropost.new
      @alert = Alert.new
    @alerts = Alert.all
  end
end

Alerts:

class AlertsController < ApplicationController

  def create
    @alert = current_user.alerts.build(alert_params)
    if @alert.save
      flash[:notice] = 'Alert Posted Successfully.'
      redirect_to timelines_index_path
    else
      @micropost = Micropost.new
      @microposts = Micropost.all
      @alerts = Alert.all
      render 'timelines/index', layout: 'timeline'
    end
  end

  private
    def alert_params
      params.require(:alert).permit(:symbol, :entry, :stop, :target, :shares, :comment)
    end
end

Micropost

class MicropostsController < ApplicationController
  before_action :authenticate_user!
  after_action :verify_authorized

  def create
    @micropost = current_user.microposts.build(micropost_params)
    authorize @micropost
    if @micropost.save
      flash[:notice] = 'Posted Successfully.'
      redirect_to timelines_index_path
    else
      @microposts = Micropost.all
      @alert = Alert.new
      @alerts = Alert.all
      render 'timelines/index', layout: 'timeline'
    end
  end

  def destroy
    @micropost = Micropost.find(params[:id])
    authorize @micropost
    @micropost.destroy
    redirect_to timelines_index_path, notice: 'Post Deleted.'
  end

  private
    def micropost_params
      params.require(:micropost).permit(:content)
    end
end

Posted in Memoization Discussion

Since Twitter was brought up in this video I figured it would be a good place to check in and see if anyone knows of a solid resource or code base for testing against 3rd Party APIs. My specific situation is using the Twitter gem and trying to figure out what to actually test against and how to implement this in Minitest --preferably without mocks or relying upon gems. Any links would be appreciated.