Gustavo Garcia

Joined

1,440 Experience
14 Lessons Completed
0 Questions Solved

Activity

Thank you very much for this video, Chris! I just wanted to share here a little update I had to do in order to make it work:
1) the new postgres images require a password, so I had to add it as an env variable.
2) actions/setup-ruby is deprecated, now you have to use ruby/setup-ruby instead
3) the borales/actions-yarn is now at v2.3.0, the 2.0.0 is not working anymore

So the final version of this setup looks like this one:

# .github/workflows/ci.yml

name: CI
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    services:
      db:
        image: postgres
        env:
          password: secretpassword
        ports: ['5432:5432']
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      redis:
        image: redis
        ports: ['6379:6379']
        options: --entrypoint redis-server

    steps:
      - uses: actions/checkout@v1
      - name: Setup Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.6.x
      - uses: borales/actions-yarn@v2.3.0
        with:
          cmd: install
      - name: Build and run tests
        env:
          DATABASE_URL: postgres://postgres:secretpassword@localhost:5432/test
          REDIS_URL: redis://localhost:6379/0
          RAILS_ENV: test
          RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
        run: |
          sudo apt-get -yqq install libpq-dev
          gem install bundler
          bundle install --jobs 4 --retry 3
          bundle exec rails db:prepare
          bundle exec rails test

You can avoid that turbo_stream template by putting everything in the controller, like this:

respond_to do |format|
  format.html { redirect_to tweets_path, notice: "Your tweet was successfully created." }
  format.json { render :show, status: :created, location: @tweet }
  format.turbo_stream { render turbo_stream: turbo_stream.prepend('tweets', partial: 'tweets/tweet', locals: { tweet: @tweet })}
end

But I personally liked a lot this approach of adding a proper template for the turbo_stream response.

Thanks a lot for this video, Chris!

Posted in Hotwire Modal Forms Discussion

Thanks a lot for this video! I see it's becoming a trend to add a format.turbo_stream response in all my controllers only if it fails.

I'm having the same issue...
ruby: 2.7.2
rails: 6.1
turbo-rails: 0.5.3 (7.0.0-beta.3)

I'm using the turbo_frame_tag 'report_form' wrapping the form and in the controller I have

def create
    @report = Report.new(report_params)
    respond_to do |format|
      if @report.save
        format.html { redirect_to @report, notice: "Report was successfully created" }
        format.json { render :show, status: :created, location: @report }
      else
        # format.turbo_stream { render turbo_stream: turbo_stream.replace(@report, partial: 'reports/form', locals: { report: @report })}
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @report.errors, status: :unprocessable_entity }
      end
    end
  end

The commented line also works, in that case, the response for the error only renders the form partial, but I still have the same problem for the success... nothing happens.

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.