Save 36% for Black Friday! Learn more

Alan Reid

Joined

42,400 Experience
332 Lessons Completed
8 Questions Solved

Activity

Posted in How do i save a field in a separate table?

sorted it :D
so its actually @brand = current_user.brands.create(brand_params) instead of new

Posted in How do i save a field in a separate table?

i get this...

(byebug) @brand.user_brands
#<ActiveRecord::Associations::CollectionProxy []>
(byebug)

Posted in How do i save a field in a separate table?

na its not saving in the user_brands table but is saving the brand lol

I have reloaded the server too using rails s

Posted in How do i save a field in a separate table?

I have this...

class User < ApplicationRecord
  has_many :user_brands
  has_many :brands, through: :user_brands
class UserBrand < ApplicationRecord
  belongs_to :brand
  belongs_to :user
class Brand < ApplicationRecord
  has_many :user_brands
  has_many :users
  has_many :products
  mount_uploader :logo, PictureUploader

My migration was like this...

class CreateUserBrands < ActiveRecord::Migration[5.0]
  def change
    create_table :user_brands do |t|
      t.references :user, foreign_key: true
      t.references :brand, foreign_key: true

      t.timestamps
    end
  end
end

My create method

def create
    @brand = current_user.brands.new(brand_params)
    if @brand.save
      flash[:success] = "Brand added!! :D"
      redirect_to root_url
    else
      render 'new'
    end
  end

Posted in How do i save a field in a separate table?

Ok I had created my model wrong Userbrands instead of UserBrands.

Its now creating the brand still, but not adding in the user_id and brand_id to the user_brands table

Posted in How do i save a field in a separate table?

I am getting this error, what could i be missing?

NameError in BrandsController#create
uninitialized constant User::UserBrand

def create
    @brand = current_user.brands.new(brand_params)  <--- this line?
    if @brand.save
      flash[:success] = "Brand added!! :D"
      redirect_to root_url

Posted in How do i save a field in a separate table?

Know what mate, i am sure you said about this before when i was discussing the structure of the DB lol Thanks again buddy.

Posted in How do i save a field in a separate table?

Just thought that maybe I should use a branduser table so a user can have multiple brands. Would this be better? and how would i join these together?

Posted in How do i save a field in a separate table?

Hi all,
I want to save the brand_id i am creating in my users table how would i go about doing this? I currently have my code saving the brand.

def create
    @brand = Brand.new(brand_params)
    if @brand.save
      flash[:success] = "Brand added!! :D"
      redirect_to root_url
    else
      render 'new'
    end
  end

Posted in Has any installed macOS Sierra yet?

Reminder... If you do choose to install macOS Sierra, make sure you have Xcode 8 installed too. You can get this from https://developer.apple.com/xcode/downloads/ - and don't forget to install the command line tools for Xcode 8 too ;)

Posted in FAO: Chris... URL Errors on pretty much any Rails site

My point is that while the W3 Spec states that mixed case is how it should be, but this is the first time I have experienced an issue with mixed case URL's. - I can understand the need for them, but for pure usability it is a pain.

I only found this because I had my caps lock button on by accident. There are many cases where users could type a URL, for example a promo page for a site. I think I am just trying to cover all eventualities to remove potential user error. ;)

Posted in Has any installed macOS Sierra yet?

I was gonna post this earlier haha.

So tempted to install the final GM on my MacBook and see how it goes. I was running the beta but had massive issues due to brew.

Luckily my main Mac will stick on El Cap until I'm happy there will be no issues ;)

Posted in Controller naming...

Cheers Andrew,
Orders makes much more sense :D thanks

Posted in FAO: Chris... URL Errors on pretty much any Rails site

Hi I've come across a really weird bug, not just in my project but on quite a few rails sites.

https://gorails.com/users/sign_in?redirect_to=%2F - works fine
https://gorails.com/users/SIGN_IN?redirect_to=%2F - Errors

Both URL's are basically the same. However they are actually quite different

This site provides one work around...
http://gehling.dk/2010/02/how-to-make-rails-routing-case-insensitive/

Posted in Controller naming...

Evening all,
So i have a question about naming controllers. Which would be best to use? MyOrder or MyOrders

Is it just the DB which get pluralised?

Chris has done some awesome casts. Some of the best I have seen, well worth the subs we pay. Plus the community is pretty cool too.

I've specialised in front most of my time, with .net backend. It got to the stage where I had really lost my passion for backend development. Rails really sparked that passion again, and now I am really loving the full stack again.

Cheers Jacob, much appreciated haha There is so much to pick up haha wish i had of started sooner, and not wasted time on .net lol

Posted in Devise: Add a select to my signup form

lol i mentioned them before in another post.

I wanted to change things like user/edit to account but when you post the form, if there is an error it sends you to a different address.

I have the below set up for registrations, but when you post on /account and there is an error it redirects to /signup, ideally i want it to go back to /account. I have debugged and seen that its the PUT that is redirecting it - this is set in the url: registration_path(resource_name) part of the form.

# Registration
    get 'signup' => 'devise/registrations#new', :as => :new_user_registration
    post 'signup' => 'devise/registrations#create', :as => :user_registration
    get 'signup' => 'devise/registrations#cancel', :as => :cancel_user_registration
    get 'account' => 'devise/registrations#edit', :as => :edit_user_registration
    put 'signup' => 'devise/registrations#update'
    patch 'signup' => 'devise/registrations#update'
    delete 'signup' => 'devise/registrations#destroy'

Cheers buddy

Posted in Devise: Add a select to my signup form

I am also using 4.2.0, I'm not sure why but i had to move it :/ i am also using rails 5.0.0.1 maybe this is why?

I am having issues with devise personally. Yeah its fantastic but its a pain cause i want to set the URL's to something different and i don't like how it deals with things. Might need to roll my own version of Auth soon i think.

Also I notice you have contact_attributes maybe you could shed some light on another post of mine? I need to save data into a different table. - https://gorails.com/forum/how-do-i-save-a-field-in-a-separate-table-when-using-devise
Maybe even let me know if there is a better way, maybe using joins?

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.