Save 36% for Black Friday! Learn more

Samantha O

Joined

2,340 Experience
5 Lessons Completed
2 Questions Solved

Activity

Let's say I have an attribute :year_of_birth for example and another field :age (readonly), I fill in the year_of_birth. And I make a method in a model to calculate the age, how does the age get populated in the view?

Thanks in advance

Posted in First argument in form cannot contain nil or be empty

Hi,
I'm trying to create an account from a popup form. But I get the first argument in form cannot contain nil or be empty. What am I doing wrong?

class AccountsController < ApplicationController

    def index
  # @accounts = Account.all
end

def new
  @account = Account.new
  @account.users.build
end

def create
  @account = Account.new(account_params)

if @account.save
  sign_in @account.users.first, bypass: true
  redirect_to new_store_path
  # redirect_to dashboard_index_path
else
  render :new
  end
end

def destroy
  @account = Account.find(params[:id])
  @account.destroy
  redirect_to root_path, notice: "Account deleted."
end

private

def account_params
  params.require(:account).permit(users_attributes: [:email, :password, :password_confirmation, :role])
end
 end

<div class="modal modal--hidden">
<%= link_to root_path, class: "close-modal" do %>
<svg class="svg-icon">
  <%= svg "close" %>
</svg>
<% end %>
<header class="modal--header">
  <div></div>
  <div class="header_title">Register</div>
</header>
<div class="modal__content">

  <%= form_for @account, url: { controller: "account", action: "new" } do |f| %>
  <%= f.fields_for :users do |user| %>
  <div class="reg-form">

  <div class="inputfield">
    <%= user.hidden_field :role, value: :store_owner %>
  </div>

  <div class="inputfield">
    <%= user.email_field :email, autofocus: true, autocomplete: "email", placeholder: "[email protected]" %>
  </div>

  <div class="inputfield">
    <% if @minimum_password_length %>
      <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %>
    <%= user.password_field :password, autocomplete: "new-password", placeholder: "Password" %>
  </div>

  <div class="inputfield">
    <%= user.password_field :password_confirmation, autocomplete: "new-password", placeholder: "Password Confirmation" %>
  </div>

  <div class="submitinputfield1">
    <%= f.submit "Sign up" %>
  </div>
</div>
<% end %>
<% end %>


Posted in How do I show a modal popup from a link_to button

I got this working with javascript.

<%= link_to "Get started", { id: "store-sign-up" } %>

document.addEventListener("DOMContentLoaded", () => {
var modal = document.querySelector('.modal')
var el =  document.getElementById("store-sign-up")

if(el) {
    document.getElementById("store-sign-up").addEventListener('click', () => {
        modal.classList.remove('modal--hidden');
      });

      document.querySelector(".close-modal").addEventListener('click', () => {
        modal.classList.add('modal--hidden');
      });
}
  });

Posted in How do I show a modal popup from a link_to button

Hi,
I am trying to build a modal popup when pushing a link_to button. How do I accomplish this? The modal doesn't popup yet, but is underneath my link_to button.

<div class="button-container">
    <% if signed_in? && role == 1 %>
 <%= link_to "Add a listing", new_store_path, class: "submitinputfield input " %>
            <% else %>
              <%= link_to "Get started", root_path, { :remote => true, data: { target: "#id01", toggle: "modal" } } %>
              <div class="host-login-container">
                <h2>CSS Animated Modal</h2>
                  <p>Some text some text</p>
                  <button onclick="document.getElementById('id01').style.display='block'" class="add-listing-btn">Open animated modal</button>

                <div id="id01" class="modal hidden">
                  <div class="modal-content animate-top modal-card">
                    <header class="host-login-container teal">
                      <span onclick="document.getElementById('id01').style.display='none'" class="add-listing-btn display-top-right">&times;</span>
                      <h2>Modal header</h2>
                    </header>
                    <div class="host-login-container">
                      <p>Some text some text</p>
                      <p>Some text</p>
                    </div>
                    <footer class="host-login-container teal">
                      <p>Modal footer</p>
                    </footer>
                  </div>
                </div>
              </div>
            <% end %>
          </div>

Posted in Get started with Rolify for roles

@excid3 @Chris Oliver, I was wondering if you can still make this happen :D

Posted in What is the best way to work with user roles?

So the question is, how do I make separate signups for store_owner and end_user? An example of this would be something like airbnb...There you can be a host (someone who rents out their home, or someone who rents a home from a host).

Posted in What is the best way to work with user roles?

What is the best way to achieve this when people sign up? I have a user model and an account model. I want to have 3 roles in my application. The administrator, store_owner and end_user.

A store owner can add a store.
The end_user sees all the stores that a store_owner has added.
The administrator sees everything :)

For now I have:

  class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :validatable

  enum type: [:administrator, :store_owner, :end_user]
  enum gender: [:male, :female]
end


class Account < ApplicationRecord
  enum status: [:active, :inactive, :expired, :locked]
  has_many :users

  before_create :set_api_key
  accepts_nested_attributes_for :users


  private
  def self.generate_api_key
    SecureRandom.hex(36)
  end

  def set_api_key
    self.api_key = Account.generate_api_key
  end
end

And in my signup view:

<%= form_for @account do |f| %>
<%= f.fields_for :users do |user| %>
<%= f.label :email %>
<%= user.email_field :email, autofocus: true, autocomplete: "email", placeholder: "[email protected]" %>
<%= f.label :password %>
<% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %>
<%= user.password_field :password, autocomplete: "new-password", placeholder: "Password" %>
<%= f.label :password_confirmation %><br />
<%= user.password_field :password_confirmation, autocomplete: "new-password", placeholder: "Password" %>
<%= f.submit "Sign up" %>

In my accounts controller:

class AccountsController < ApplicationController
  def index
  end

  def new
    @account = Account.new
    @account.users.build
  end

  def create
    @account = Account.new(account_params)

    if @account.save
      sign_in @account.users.first, bypass: true
      redirect_to root_path
   else
      render :new
  end
end

  def destroy
    @account = Account.find(params[:id])
    @account.destroy
    redirect_to root_path, notice: "Account deleted."
  end

  private

  def account_params
    params.require(:account).permit(users_attributes: [:email, :password, :password_confirmation])
  end
end

It works!
I had to add a form_for block in the view and pass the pricingclassification object.

<%= form_for :pricing_classification do |f| %>
        <%= f.label :pricing_classification %>
        <%= select_tag nil, options_for_select(range_options, range_string(PricingClassification.new)), data: { action: "change->range#change" } %>
        <%= f.hidden_field :minimum, data: { target: "range.minimum" } %>
        <%= f.hidden_field :maximum, data: { target: "range.maximum" } %>
        <% end %>

Hi,
I have a Store model and a PricingClassification model. The PricingClassification belongs to Store. I have made a wizard for creating a store and I am trying to add a range in pricingClassification. But I get the undefined method `minimum' for #Store:0x00007fb25c749b20
What am I missing or doing wrong?

class Store < ApplicationRecord
  has_many :pricing_classifications, dependent: :destroy
end

class PricingClassification < ApplicationRecord
  belongs_to :store
end

module PricingClassificationsHelper
      def range_string(pricing_classification)
        "#{pricing_classification.minimum}-#{pricing_classification.maximum}"
      end
    end

While creating a new store I have this in my view.
<%= f.label :pricing_classification %>
<%= select_tag nil, options_for_select(range_options, range_string(f.object)) %>
<%= f.hidden_field :minimum %>
<%= f.hidden_field :maximum %>

Posted in How do I add ranges to my application?

Awesome! I also send you a slack private message for 2 screencast ideas :S Thank you

Posted in How do I add ranges to my application?

Do you have an example of number 2? I'm fairly new to programming :S

Posted in How do I add ranges to my application?

Hi Chris,
Thanks for the reply. How do I get this in a dropdown? Like this example: https://www.wix.com/corvid/forum/community-discussion/single-dropdown-filter-using-a-range

I sure hope Chris will do this soon :)

Posted in How do I add ranges to my application?

Hi in my app I want to have a price classification table where I want to add a range per person...
For example: range_per_person = 1-10, 11-20, 21-30 etc etc.
How can I achieve this? See my schema below.

create_table "pricing_classifications", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name"
t.integer "range_per_person"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

Hi Chris,
I added English and Dutch translations, but when I switch to Dutch(nl), I don't see the translations...Any idea where I might have gone wrong? In the nl.yml file the first line is nl:
Thanks in advance

Thanks Tim, I will have a look at it :)

Hi Tim,
Thanks for the reply, can you provide me with a link to the stimulusjs-components of Chris? Thanks!

Hi GoRails,

I would like to make a multistep wizard in ruby on rails without making use of the wicked gem. Just stimulusJS and ruby :)
Could you make a tutorial about that?

Thanks in advance!

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

© 2020 GoRails, LLC. All rights reserved.