Jim Miller

Joined

9,080 Experience
72 Lessons Completed
2 Questions Solved

Activity

Posted in How to Deploy Rails to Render Discussion

Thank you, sir!

Posted in Not Rendering with Stimulus & ActionCable

In case someone stumbles across this and needs an answer. Modify message_list_controller:

app/javascript/controllers/message_list_controller.js

_cableReceived(data) {
    this.messagesTarget.innerHTML += data.message;
  }

Posted in Not Rendering with Stimulus & ActionCable

Hi,
I'm having some issues with Stimulus and I was hoping that someone could review this code and see if they see anything that I missed.
The Rails part is working and saving to the db. I'm sure that I have a typo somewhere...

Here is what I have:

app/channels/message_channel.rb

class MessageChannel < ApplicationCable::Channel
  def subscribed
    stream_from 'message_channel'
  end

  def unsubscribed
    stop_all_streams
  end
end

app/javascript/controllers/message_list_controller.js

import { Controller } from "stimulus";
import consumer from "../channels/consumer";

export default class extends Controller {
  static targets = ["input", "messages"];
  connect() {
    this.channel = consumer.subscriptions.create("MessageChannel", {
      connected: this._cableConnected.bind(this),
      disconnected: this._cableDisconnected.bind(this),
      received: this._cableReceived.bind(this),
    });
  }

  clearInput() {
    this.inputTarget.value = "";
  }

  _cableConnected() {}

  _cableDisconnected() {}

  _cableReceived() {
    this.messagesTarget.innerHTML += data.message;
  }
}

app/controllers/messages_controller.rb

class MessagesController < ApplicationController
  before_action :set_message, only: [:show, :edit, :update, :destroy]

  def index
    @messages = Message.all
  end

  def create
    @message = Message.new(params.require(:message).permit(:content))
    @message.save!
    ActionCable.server.broadcast('message_channel', message: (render @message))
    head :ok
  end


  private
  def set_message
    @message = Message.find(params[:id])
  end

  def message_params
    params.require(:message).permit(:content)
  end
end

app/views/messages/index.html.erb

<p id="notice"><%= notice %></p>

<h1>Messages</h1>

<div data-controller='message-list'>
  <div data-target='message-list.messages'>
    <%= render @messages %>
  </div>

  <%= form_with(model: Message.new, 
                data: { action: 'ajax:success->message-list#clearInput' }) do |form| %>
                <%= form.text_area :content, 
                  data: { target: 'message-list.input' }, rows: 1, autofocus: true %>
                <%= form.submit class: "btn btn-default" %>
              <% end %>
</div>

Gist of Logfile

Thanks!

Updated my controller but still no go:

def create
  message = @hangout.messages.new(message_params)
  message.user = current_user
  respond_to do |format|
    if message.save
      format.html { redirect_to @hangout, notice: 'Success' }
      format.js
    else
      format.html { render action: 'new' }
  end
end

BTW, I am working on this Lesson:
Group Chat with ActionCable: Part 3

I am using Rails 6. I have a form that I need to pass remote: true so I get POST to process as JS:

LOG: MessagesController#create as JS

Here is what I have tried:

<%= form_for [@hangout, Message.new] do |f| %>

The result is a good save to the DB but processes as HTML:

LOG: MessagesController#create as HTML

So I tried:

<%= form_for [@hangout, Message.new], remote: true do |f| %>

I learned that this would give an InvalidAuthenticityToken error:

LOG: ActionController::InvalidAuthenticityToken - ActionController::InvalidAuthenticityToken:

Tried this:

<%= form_for [@hangout, Message.new], authenticity_token: true do |f| %>

It passes as HTML, not JS

I read that for Rails 6, the best way to do this was with form_with because it passes remote:true:

<%= form_with(model: [@hangout, Message.new]) do |f| %>

Unfortunately, this never reaches the controller so I get no response. I know that my model and controller are set up properly since the first try with form_for works, so it has to be with the way I am writing my form_with, right?

Does anyone have any advice?

Thanks!

#model
class Message < ApplicationRecord
  belongs_to :hangout
  belongs_to :user
end

#controller
class MessagesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_hangout
  def create
    message = @hangout.messages.new(message_params)
    message.user = current_user
    message.save
    redirect_to @hangout
  end
  private
    def set_hangout
      @hangout = Hangout.find(params[:hangout_id])
    end
    def message_params
      params.require(:message).permit(:body)
    end
end

#routes
require 'sidekiq/web'

Rails.application.routes.draw do
  resources :hangouts do
    resource :hangout_users
    resources :messages
  end

  resources :notes

  authenticate :user, lambda { |u| u.admin? } do
    mount Sidekiq::Web => '/sidekiq'
  end

  devise_for :users, controllers: { registrations: 'users/registrations' }

  get 'mine', to: 'notes#mine'

  root to: 'application#root'

  mount Shrine.presign_endpoint(:cache) => '/images/upload'
end

Posted in Help with Debugging

Hi all,
I am trying to help out an open source project that I use. I found an issue and the developer has asked me to help debug the javascript but I'm a javascript hack and not sure how to do it.

Background:

  1. The project is coc-tailwindcss
  2. The plugin works in one project but not the other. Here's the issue: https://github.com/iamcco/coc-tailwindcss/issues/22
  3. The developer says the LSP init fail, debug the error at line: https://github.com/iamcco/coc-tailwindcss/blob/master/lsp/tailwindcss-language-server/dist/index.js#L7134
  4. The developer asked me "Use console.log to print info to output channel or follow https://github.com/neoclide/coc.nvim/wiki/Debug-language-server"

I have at least 2 questions:

  1. The developer wants the console.log() on 7134. Exactly where in the code should I put it and what variable should I use?
  2. I'm assuming that the output would be in :CocCommand workspace.showOutput but I'm not sure. Am I right? Is there another spot to output?

I appreciate any help!!

Jim

Its only "Very cool" if it works! LOL

So you are basically saying that I control everything in Rails and just use Stripe as the final payment method, right?

So like this:

  1. The deposit would be one payment thru Stripe, say $200 out of $400 (I'm assuming that I can use the same JavaScript as I have been with your tutorial?)
  2. Thru Rails I would set that as "deposit". Maybe using Enums? enum status: [:paid_in_full, :deposit]
  3. Set up Cron to send an email to all depositors every month as a reminder
  4. The rest of the payment would be a seperate payment thru Stripe
  5. If full payment is made, update status: :paid_in_full
  6. If status: :paid_in_full, then show advertisement on WordPress site

I plan on pulling json generated from the users controller to populate the WordPress site. I'm sure I will post questions when I get to that stage!

Thanks for the help, Chris!

Posted in How to split devise edit form in rails app?

Hi Kasper,

The first thing that I notice is that you don't have any actions defined in your controller. Rails is trying to process UserprofilesController#update but the action empty. So that would be a start, define your actions and see what happens.

A couple of things that you should try after you resolve the first one:

  1. Your UsersController should inherit from Devise. See Devise github page: https://github.com/heartcombo/devise#configuring-controllers
  2. Make sure that you sanitize any additonal attribute from the Devise standard: https://github.com/heartcombo/devise#strong-parameters

My personal opinion is to not overly complicate your Rails code to make the UI better for the user. You can order attributes into groups on the front-end to make it easier for the user to focus, you could make tabs to seperate the groups. What I would do is use something like the Wicked gem. It helps you create a wizard type of experience for your user. Breaks the profile creation into bit-sized pieces. Richard Schneeman is the maintainer. Check out his screen cast, I think that its what you are looking for:
https://schneems.com/post/18437886598/wizard-ify-your-rails-controllers-with-wicked

I hope that my comments are helpful! Good luck!

Its for a Tattoo Convention. These are artists putting a deposit down for a booth or multiple booths. The deposit is "no refund". Which is the industry accepted practice.

The system I am creating will:

  • have the artist register with the normal contact info plus other info like tattoing style, Instagram and/or Facebook urls and their picture which will be in their profile/account info in Devise.
  • Then they can pay full price for a booth(s) or they can put a deposit down.
  • The deposit holds a booth for them until the determined due date. If the date passes, they loose the spot for the booth and deposit.
  • Once the artist pays in full, their info and picture will be posted on the site, not before. This is for advertisement on the Convention site so its incentive for them to pay in full so they get there advertisement on the site. So I need to record when they complete payment so then I will have the advertisment posted dynamically to the Convention site, which is a Wordpress site.

I also found that you can set the Stripe API to send the invoice to the customer by email, which is cool:
https://stripe.com/docs/billing/invoices/sending#dunning

Posted in How to split devise edit form in rails app?

Kasper, can you tell us why you want to split the user profile into 3 different pages?

Posted in How to split devise edit form in rails app?

Kasper, does your log files give you any indication as to what is going on?

I am creating a Stripe product that gives the buyer an option to put a deposit down then they are given x amount of months to pay. Or they can pay in full. I'm sure that I would use scheduled subscriptions to do that. How would I set up that Subscription so that the customer only has x months to pay and requires the balance paid at the end?

Thanks!

Jim

Posted in uncaught syntaxerror unexpected token ' var'

Hi all,

I am getting an error "uncaught syntaxerror unexpected token ' var'" . I'm using this JQuery plugin:
https://www.jqueryscript.net/time-clock/Attractive-jQuery-Circular-Countdown-Timer-Plugin-TimeCircles.html
The error is showing up on "var updateTime.
Here is the page that I am adding it to:
https://www.battlefieldtattooexpo.com/

Can anyone see what I am doing wrong?

Thanks ahead of time!

Jim

<script>
 var cl = cloudinary.Cloudinary.new({  cloud_name: 'downtown' });
 cl.responsive();
 //Script for CountDownTimer
 $("#DateCountdown").TimeCircles();
 $("#CountDownTimer").TimeCircles({ time: { Days: { show: false }, Hours: { show: false } }});
 $("#PageOpenTimer").TimeCircles();
 ------------
  var updateTime = function(){
   var date = $("#date").val();
   var time = $("#time").val();
   var datetime = date + ' ' + time + ':00';
   $("#DateCountdown").data('date', datetime).TimeCircles().start();
  }
 $("#date").change(updateTime).keyup(updateTime);
 $("#time").change(updateTime).keyup(updateTime);

// Start and stop are methods applied on the public TimeCircles instance
 $(".startTimer").click(function() {
  $("#CountDownTimer").TimeCircles().start();
 });
 $(".stopTimer").click(function() {
  $("#CountDownTimer").TimeCircles().stop();
 });

// Fade in and fade out are examples of how chaining can be done with TimeCircles
 $(".fadeIn").click(function() {
    $("#PageOpenTimer").fadeIn();
  });
 $(".fadeOut").click(function() {
   $("#PageOpenTimer").fadeOut();
  });
</script>

Posted in How do I resolve this syntax error

That's it! Thanks!

Posted in How do I resolve this syntax error

Hi guys,

I have this error and I don't understand what the error is. I'm using Rails 6

syntax error, unexpected tLABEL
stripe_id: customer.id,
~~~~~~~~~
/vagrant/Rails/stripe-test/app/controllers/subscriptions_controller.rb:34: syntax error, unexpected tLABEL, expecting '='

My code is:
options = (
stripe_id: customer.id,
stripe_subscription_id: subscription.id
)

current_user.update(options)

Thanks,
Jim

that sounds good! live and learn. luckily this is only a tutorial.

thanks Chris

lol. that sucks. I'm in Fla and my computer is in PA. I guess it defeats the purpose, but can it be reset locally so I can finish my tutorial? ":)

Hi all,

I cloned my project to my laptop but when I try to access credentials.yml.enc with rails credentials:edit, I get this error:

Couldn't decrypt config/credentials.yml.enc. Perhaps you passed the wrong key?

Can anyone give me the steps to properly reset my credentials.yml in a cloned repo?

Thanks,

Jim