Simon Cooper

Joined

3,420 Experience
5 Lessons Completed
3 Questions Solved

Activity

Posted in Add image attachment to user post and server via AWS S3

Hey Martin.  I ended up just using Paperclip, in a regular erb view.  Very much does what I need for now.

Posted in Why CKeditor is not working in production?!

Just a thought, but do you have to white-list your pdocution domain with them anywhere?

Hey Spohnz,

Assuming you have an appliance controller, you might want to add @client in appliance_controller.rb under the index or show methods. e.g.

class ApplianceController < ApplicationController
    def index
        @appliance = Appliance.all
        @client = Client.all
    end
end

But I'm also just learning too, and whilst the above works, someone else might have another suggestion.

Posted in Logic on nested forms/related tables

Hey Robert.

Thanks for the response. I've ended up just going with a triple nested form:

class Roast < ApplicationRecord
    has_many :countries
end

class Country < ApplicationRecord
    belongs_to :roast
    has_many :regions
end

class Region < ApplicationRecord
    belongs_to :country
end

This does allow me to show the countries and their regions (up to 3 times) for any given roast. Hopefully this gives me a better foundation for adding more sophistication to the Roast model as I move forwards.

Posted in Error with the Scheduled Posts Episode

Thanks James.

Unfortunately no change with the above addition of .present.

Posted in Error with the Scheduled Posts Episode

Hey James,

Checked that, and the migration has definately run.

Simons-MBP:gcl Simon$ rails g migration AddPublishedatToArticles published_at:datetime
Running via Spring preloader in process 22081
      invoke  active_record
      create    db/migrate/20180203182358_add_publishedat_to_articles.rb
Simons-MBP:gcl Simon$ rails db:migrate
== 20180203182358 AddPublishedatToArticles: migrating =========================
-- add_column(:articles, :published_at, :datetime)
   -> 0.0175s
== 20180203182358 AddPublishedatToArticles: migrated (0.0176s) ================

Posted in Error with the Scheduled Posts Episode

I've jsut been going through the https://gorails.com/episodes/scheduling-posts episode, but have ran into the issue of undefined method 'published_at?' in my helper, when I try to load the new form page. Did anyone else run into this? My helper is as below, my model is called articles and not posts

module ArticlesHelper
  def status_for(article)
    if article.published_at?
      if article.published_at > Time.zone.now
        "Scheduled"
      else
        "Published"
      end
    else
      "Draft"
    end
  end
end

Posted in Scheduling Posts Discussion

I've gone through this, but when trying to load the form. I get `undefined method `published_at?' for nil:NilClass`.

My helper to my eye, looks word for word as per the example: (my model is article, not post)

module ArticlesHelper
def status_for(article)
if article.published_at?
if article.published_at > Time.zone.now
"Scheduled"
else
"Published"
end
else
"Draft"
end
end
end

Posted in Logic on nested forms/related tables

I'm trying to improve my modles for an existing scenario, but I just can't get my head around it. The situation is that I want to list a range of coffee's and include the country and region of the beans. A coffee could be made from a single roast, but it could also be a blend of two or more.

At the moment I have just one model roasts that simply has fields for country1 region1, country2 region2 etc. Although, I'm led to believe it would be better to have a seperate model for countries. However, 1) would I add region field to the country model, or 2) would I have another model for regions - With country has_many :regions, and region belongs_to :country?

Absolutely Andrew. Here's what I have. An update from the above, is that I now have the 'distillery' field showing in the new form, but when I submit the form, i get the error:

Distillery must exist

views/gins/_form.html.erb

<%= form_for(@gin, local: true, multipart: true) do |form| %>
//some fields removed

  <div class="input-field">
    <%= form.fields_for :distillery do |distillery_form| %>
      <p>
        <%= distillery_form.label :distillery %>
        <%= distillery_form.text_field :name %>
      </p>
    <% end %>
  </div>
//some fields removed

models/gins.rb

class Gin < ApplicationRecord
  belongs_to :distillery

models/distilleries/distillery.rb

class Distillery < ApplicationRecord
  has_many :gins
  accepts_nested_attributes_for :gins

controllers/gin_controlller.rb

    def gin_params
      params.require(:gin).permit(:name, :text, :pic, :slug, distillery_attributes: [:id, :name])
    end

I have two models, Gins and Distillery. At the moment, these are two completely seperate models, but I have been thinking that as a user adds a a new Gin and inputs it's distillery, this could also create a new entry in the Distillary table. Obviously Gin would then belong_to distillery and Distillery would has_many Gins. Looking around it seems a nested form could be the solution, but would that actually add the distillery value into both forms/tables. I haven't actually been able to get this to work, as the Distillary field in thh Gins form, wouldn't render

Posted in Render a Rails resource with a React Component

I'm going round in circles trying to work out, what I think must be a fairly common scenario, of tyring to render a rails resource through a React front end component.

I'm seem to have got as far as understanding I need to be able to pass down my resources as a props from my controller. I have the code below attempting to do that.

class RoastsController < ApplicationController
def index
@roasts = Roast.all
render component: 'RoastsJson', props: {roasts: @roasts}

end

I then understand I need a React component that can receive this props, and they render it in my view.

I have the follow code attempting to do that:

import React from 'react'
import ReactDom from 'react-dom'

class RoastsJson extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      error: null,
      isLoaded: false,
      roasts: []
    };
  }

  componentDidMount() {
    fetch("roasts.json")
      //.then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            items: result.roasts
          });
        },
        // Note: it's important to handle errors here
        // instead of a catch() block so that we don't swallow
        // exceptions from actual bugs in components.
        (error) => {
          this.setState({
            isLoaded: true,
            error
          });
        }
      )
  }

  render() {
   let roasts = []
   this.props.roasts.map((roast) => {
      roasts.push(
        <tr key={roast.id}>
          <td>{roast.name}</td>
        </tr>
      )
    });
    return (
      <div>
        <h1>Roasts</h1>
        <div id="roasts">
          <table>
            <thead>
              <tr>
                <th>Name</th>
              </tr>
            </thead>
            <tbody>
              {roasts}
            </tbody>
          </table>
        </div>
      </div>
    );
  }
};



export default RoastsJson

However, I'm just not getting this to work out. Am I fundamentally missing something? I'm under this impression that this scenario is a fairly common and typical use case but I'm struggeling to find any articles that succinctly explain how to go about it.

Posted in Add image attachment to user post and server via AWS S3

Thanks Jack. I will have a watch of that.

Hello.

I have a RoR 5.1 app, with an existing form that allows users to add a post to my app. However I want them to be able to include an image as an attachment. I'd like to save the image to S3, and I'm thinking of using React to do this. Can anyone point me in the direction of a good tutorial? I'm very new to React.

Turned out it was actually a Heroku issue. I just needed to add a node buildback.

Thanks Chris.

I've been able to open my profile $ open .profile from the terminal, and now have the following:

PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH"

(No idea how that Postgres got there.)

However, the main issue I was trying to solve is still persisting. When trying to upload my app to heroku, it fails due to Webpacker requires Node.js >= 6.0.0 and you are using 0.10.30 I've got a fresh install of node.js and I have no problems running my app locally.

Should I be updating the $PATH somewhere else?

I've got into bit of a mess. Posted this over on SO too, but no answers of use over there.

I've added Webpacked so I can install React.js. However, I think I've somehow buggered up my PATH and bash profile and I can't seem to resolve it.

Simons-MBP:~ Simon$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
-bash: curl: command not found
Simons-MBP:~ Simon$ open .profile
-bash: open: command not found

Completely lost at this stage.

Posted in Create dynamic table for existing resource

Just front end really Abdul. Basically looking to have the user be able to filter the results without the page reloading.

Posted in Create dynamic table for existing resource

Happy New Year!

Just wondering if anyone had thoughts on this.

Posted in What are your goals for 2018?

Yes I guess it is that time of year for making list!

I'm new to the GoRails community so will use this as a way to start getting involved.

  • Continue to develop my site gourmetcoffee.london. In particular finish the coffee roasts user rating and library.
  • Build a blog site for my mate Steve to write about his love for gin!
  • Start learning some Data Science techniques and practices.
  • And write more!

Happy New Year!

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.