Andrea Fomera

Joined

50,780 Experience
468 Lessons Completed
5 Questions Solved

Activity

Posted in how to setup elasticsearch on Heroku?

Here's an article on Bonsai which let's you do Elasticsearch on Heroku.

https://devcenter.heroku.com/articles/bonsai & https://docs.bonsai.io/docs/ruby-on-rails

I don't have any experience with running elasticsearch on heroku but give that article a shot and the docs on bonsai's site and see if that gets you to a better spot to troubleshoot with.

Posted in Is anyone using Rails 5 yet?

I'm running a few apps on Rails 5 now, and all new projects I've started are on Rails 5. Never noticed any issues with the Sprockets stuff but your mileage may vary.

Posted in Group Chat with ActionCable: Part 1 Discussion

https://github.com/gorails-... You can find the code for the layouts from the episode there.

If you're wondering short_name is probably a method on the user.rb file in the model folder and it's probably setting short_name to whatever the users first name is. But like @ineptsoftware:disqus said, if you change it to current_user.username or current_user.email it should work :)

Posted in GoRails Markdown and Preview

I don't mean to bring up a sort of old post, but wanted to post this here in case someone else was running into similar issues.

I was working on a form where I wanted a live preview of the parsed markdown on the same page and I like to see things in real time so I wanted to update the preview any time a key is pressed or I unfocus on the text field.. here's my coffeescript that I added to from what Chris posted.

Oh and I for whatever reason could not get @previewArea.html html to actually update the preview area so I changed it to select the ID of the div which I had set to preview.

class PostPreview
  constructor: (element) ->
    @element = $(element)
    @commentField = @element.find("[data-behavior~='post-content']")
    @previewArea = @element.find("[data-behavior='post-preview']")
    @setEvents()

  setEvents: ->
    @commentField.on "focusout", @handlePreview
    @commentField.on "change", @handlePreview
    @commentField.on "keyup", @handlePreview

  handlePreview: =>
    html = marked @commentField.val()
    document.getElementById('preview').innerHTML = html;

jQuery ->
  $.each $("[data-behavior='post-form']"), (i, element)->
    new PostPreview(element)

All in all basically the same as Chris. If there's a better way to do this or reasons I shouldn't I'd love to hear em.

Posted in Rich text format editor in rails 4

Check out this gem if you'd like an 'easy' way to integrate the editor.

https://github.com/maclover7/trix

I've used it in a few of my projects.

Posted in Rails Console

This page on the rails guide shows more things you can do

http://guides.rubyonrails.org/command_line.html

It looks like you have access to the app and helper methods as well.

One way you could think about handling this is with multi-tenancy,

You could set it up in a way that schools have a subdomain on your app, like school1.example.com/ and then they have forum threads nested in that like normal. and the way to differentiate between the data would be based off the subdomain or the URL.

https://rubygems.org/gems/apartment could help

:+1: on the notifications system.

Would also be interested in seeing how it'd tie into for example emailing a user the notification as well as adding it to their feed. Would you want to make it so that if you click the link to a forum post in the email it'd mark that notification as read? How could this be done efficiently?

Posted in What do you think about this ?

I agree with Maxime, sometimes with YT I'll click on a video and just pause it, then close my browser out and it'll have marked it as played already and I get confused.

Posted in I'm lost and can't find the way out

Is there a way to make the uuid validation NOT happen on the devise edit registration? Any time I enter the field it clears it or when I wrap the setting in an if it doesn't actually the api lookup

Posted in I'm lost and can't find the way out

Just kidding... I thought it was done, but I appear to hit another bug ;) Just as I was ready to deploy I found out existing users / any users can't actually update their profile because the UUID is required as a validation. But because it's something I don't want to be updatable by users after the first time I run into an issue here.

I had it on the create action, but because I don't ask for it at registration that failed, so I moved it to :update and it worked on the links/minecraft page

So I removed the presence required and figured it was ok, was able to edit my user's profile... then found out it's still broken because when I save the profile I have

before_validation :set_uuid, on: :update

which because the minecraft_uuid form doesn't exist completely breaks it (makes it vanish).

So I fixed it mostly by wrapping it in an if statement


  if :minecraft_uuid.nil?
    before_validation :set_uuid, on: :update
  end

  def set_uuid
    begin
      self.minecraft_uuid = MojangApi.get_profile_from_name(minecraft_uuid).uuid        
     rescue Exception => e
     end  
  end

But now folks can if they go to the page and submit the links/ form again clear out their UUID... Would the best solution to this be to just not show them the field/button to submit the form again if the UUID is not null?

I realize this is really app specific here, but I feel like my user model is an absolute trainwreck now.

Or is there a way to do validations (that the minecraft_uuid field is filled out) only on my links controller?

edit: okay I need to slow down here. I'm asking questions before thinking things through. Sorry... I actually stopped and thought about what the if statement does and realized that then if the UUID was somehow updated it wouldn't actually set it... so I removed the if statement. I think my plan now is to just disable the form if there's a UUID set and have a button for them to contact support to fix it.

Sorry <3

Posted in I'm lost and can't find the way out

So I attempted it that way with the hidden field, but could not get it to render the error messages. Some more poorly typed google questions later and I decided to go back to the controller and play around with it until I was able to come up with a solution...

Here's my new controller method

    def create
        if current_user.update(user_params)
            redirect_to root_path, notice: "Successfully saved your Minecraft account!"
        else
            if !current_user.update(user_params)
                redirect_to links_minecraft_path, alert: "An error occurred while looking up your Minecraft UUID, please try again!  Make sure you double check your spelling."
            end
        end
    end

aka, I found out after the else statement when I rendered the user_params to plain text it prints out false whenever there's a bad response. So I decided... if it's false it just redirects and displays a generic error message... may not be the 'best' way to solve it and heck I'll probably find something that breaks it after deployment... but it works... sort of :) Thanks guys!

Now to build the next half of the feature... where they click a button and an application gets created ;)

Posted in I'm lost and can't find the way out

Now that I'm home... I realize that the @users.error thing wasn't even functioning but it was left in from a previous attempt

Posted in I'm lost and can't find the way out

Okay I'm on my phone but was able to pull it via my gitlab repo. Hopefully this looks ok

<% page_title "Linking Minecraft Account" %>
<div class="well">
  <div class="page-header">
    <h1>Link Minecraft Account</h1>
  </div>

  <div class="row">
    <div class="col-md-12">
      <p>
        It is easy to link your Minecraft account to your AthensMC account! We don't require any passwords, and you'll just need to enter your <strong>current Minecraft username</strong>
        below and we will handle the rest.
      </p>

      <%= form_for @user, url: links_path, action: 'create', method: :post do |f| %>
          <% if @user.errors.any? %>
            <div id="error_explanation">
              <h2><%= @user.errors.count %> Error(s) prohibited this from being saved:</h2>
              <ul>
                <% @user.errors.full_messages.each do |msg| %>
                  <li><%= msg %></li>
                <% end %>
              </ul>
            </div>
          <% end %>

        <div class="form-group">
            <%= f.label :minecraft_uuid, "Minecraft Username"%><br />
            <%= f.text_field :minecraft_uuid, autofocus: false, class: "form-control" %>
          </div>


          <div class="form-group">
            <%= f.submit "Link your account!", class: "btn btn-primary btn-lg" %>
          </div>

      <% end %>

Posted in I'm lost and can't find the way out

So the way I was able to get it to work previously was to use before_update instead of before_create. My understanding was this would allow users to be able to update it as well.

Am I trying to put the error handling in the wrong place? Should it go in the controller? When I enter in a failed name I don''t even see the flash for it not updating, it just don't show up. Here's the relevant controller:

class LinksController < ApplicationController
    def minecraft
        @user = current_user
    end

    def create
        if current_user.update(user_params)
            redirect_to links_minecraft_path, notice: "Successfully saved your Minecraft UUID"
        else
            redirect_to links_minecraft_path, error: "An error occurred and we could not save your Minecraft UUID"
        end
    end

    private
        def user_params
            params.require(:user).permit(:minecraft_uuid)
        end
end

Posted in I'm lost and can't find the way out

Thanks guys once again.. I got much closer and was able to get the UUID and save it in the DB.

Now my problem is if a user enters a username but it's not found in the system, aka they made a typo or just don't have an account.

I've been trying to work out how to I ensure that it's valid and not nil, because if the user doesn't exist it currently just saves it as nil
I'd also like to be able to show the user an error message when that happens but was unable to get it to work (I can tell when it's null, but trying to do a flash notice gave me an error)

By just doing under in the load_profile

    if profile.uuid.nil?
        raise "an error occured"
    end

I was able to get it to show in my dev environment but I feel like I should put it in the controller somehow and I don't know where/how?

Once again thanks for the help, I'm so close to 'done' for this feature I can smell it, just need to get this error thing figured out

Posted in I'm lost and can't find the way out

Hey Chris, I've been working on this for a bit now and I want to say thanks for all the help so far.

I was able to do a little finagling and get it to actually store the stuff that's submitted via the form to the minecraft_uuid field in the db but I cannot figure out how to handle the validations / conversion from the input to the actual UUID that's a response from the API.

The gem I'm using to handle the API stuff via the command line currently is https://rubygems.org/gems/mojang_api

Basically I can get it to work in the IRB by doing

require 'mojang_api'

then I can make my call via IRB

MojangApi.get_profile_from_name('king601').uuid 

where king601 should be whatever the UUID was entered as via the form. Any advice or help here? I need to save the response via the api but I'm not entirely sure how to take it from IRB into the rails app. I tried googling how to do validations in this fashion but I wasn't googling the right thing.

P.S: I had to change the form_for from current_user to

    <%= form_for @user, url: links_path, action: 'create', method: :post do |f| %>

hopefully that's okay still?

edit: also in case you're wondering i'm using Devise for my User model.

the User.rb currently looks like

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  validates :username, presence: true, uniqueness: { case_sensitive: false }

  has_many :revisions
  has_many :casts
end

I do need to also add a validation so Users can't have spaces in their username on my site, but that's not high on my to-do list

Posted in I'm lost and can't find the way out

Thanks for this, I took a bit of a break from the Rails sides of things to go pick up more ruby knowledge and I think I'm going to attempt implementing this stuff soon. Will just need to verify the API calls return successfully with the UUID (after the user submits a username via my site)... and build in some error handling for that

Posted in I'm lost and can't find the way out

So I'm in the middle of this project and I thought I could figure it out on my own, but it appears I am in over my head here. So any advice/help you can give me would be great.

Basically I'm trying to do the following:

  • User goes to /link/minecraft on my website and if they haven't done it before shows them a form to enter their Minecraft username.
  • On the backend once that field is submitted then it checks against their api and returns a UUID which is then stored in the Users table (from devise) in a minecraft_uuid field.

I can handle the api lookup already (it's working via CLI). My problem appears to be my lack of understanding the get/post and forms in rails. I'm not sure what my route is supposed to be for this. I tried doing

get 'link/minecraft' => "link#minecraft"

but I don't know in my controller how my code would work inside of the minecraft action/method?

Any advice or help on how to handle this? I need to be able to pull the value they submit and work with it in the backend before saving the result to the database...

Thanks <3

Posted in Updating Rails Applications across Rails versions?

Thanks for the fast reply!

If I'm being honest I thought the OneMonth acquisition was going to end up badly for GoRails etc, but it seems my fears were unfounded. So that's great! I found OneMonth's rails app didn't really help me much in my attempts to learn Rails (which made me sad).

I've just recently felt like I've started to get the concepts/what the code actually is doing which is great because I've spent over a year or two trying to learn Rails. GoRails has been a big help for me and I'd be super interested to see the screencast on the upgrade process. I find it INCREDIBLY hard to find good videos on Rails development that are clear enough to understand and in-depth enough to not bore me.

So thanks for the clear and indepth videos! I'll get plugging away at the update stuff again, it should be mostly simple for my app, I was only using a few Gems (mostly things like bootstrap-sass and font-awesome) and there wasn't much actual code there until this past week... it was mostly just a glorified static website with one or two controllers/models.