lundi 2 octobre 2017

Program which will add items to a "grocerylist" array?

Here's the problem I'm working with:

The add_item method should take two arguments. The first argument is the item you want to add to the list and the second argument is the array (i.e. list) that the item will be added to. Don't add an item to the list if it already exists in the list.

Here is the code I have so far, and it keeps giving me an 'undefined local variable' error.

list = Array.new
list = [milk, eggs, oj, bacon]

def add_item(item, list)
  if list.include?(item)
    puts "item is already on list"
  else list.push(item)

  end
end

add_item(milk, list)

Newbie RoR - routes

Don't know exactly how to do this in rails, i'm just learning and trying to get this app up and running. Problem is I can't get the routes right to edit users. Below are the _header.html.erb which has edit users link, and routes.rb, if you need more info please let me know.

<li><%= link_to "Settings", edit_user_path(current_user) %></li>

and

Rails.application.routes.draw do

root 'static_pages#home'

get 'help'  => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'



get '/all_users', :to => 'users#index'

get '/users/:id', :to => 'users#show', :as => :user

resources :users do
get user
end
resources :account_activations, only: [:edit]
end

devise rails application redirecting to users/sign_in

all - New to rails and web development. I started a new rails application and am using Devise. Whenever I hit Log In on the users/sign_in page, the page just refreshes.

Here is my terminal log after clicking Log In. It's showing the POST, I'm not sure why it's ultimately rendering devise/sessions/new.html....:

    Started POST "/users/sign_in" for ::1 at 2017-10-02 09:19:20 -0400
Processing by Devise::SessionsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ot4EJmxLvXrPpa6WYyqCXHxAUT3DcHimrIfw8HGyu5j7yuXvWArEkWzx59Dj3GZrlVDpgS/xSgXFSIt+mQqQnw==", "user"=>{"email"=>"bob@good.company", "password"=>"[FILTERED]"}, "commit"=>"Log in!"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."email" = ? ORDER BY "users"."id" ASC LIMIT ?  [["email", "bob@good.company"], ["LIMIT", 1]]
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Completed 401 Unauthorized in 162ms (ActiveRecord: 0.3ms)


Started GET "/users/sign_in" for ::1 at 2017-10-02 09:19:21 -0400
Processing by Devise::SessionsController#new as HTML
  Rendering devise/sessions/new.html.erb within layouts/application
  Rendered devise/shared/_links.html.erb (1.2ms)
  Rendered devise/sessions/new.html.erb within layouts/application (4.6ms)
Completed 200 OK in 45ms (Views: 43.5ms | ActiveRecord: 0.0ms)

My routes.rb

Rails.application.routes.draw do
  get 'charges/create'
  devise_for :users
  get 'welcome/index'
  get 'welcome/about'
  resources :wikis
  resources :charges, only: [:new, :create]
end

and my User.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable
  has_many :wikis

  before_save { self.email = email.downcase }

  enum role: [:standard, :admin, :premium]
  after_initialize { self.role ||= :standard }

end

Thank you for any direction and please let me know if I can provide any more details / code samples etc.

dimanche 1 octobre 2017

What is going wrong with my code? It outputs not even no matter what i do

print "input a number please. " TestNumber = gets if TestNumber % 2 == 0 print "The number is even" else print "The number is not even" end

Ruby on Rails (hartl) invalid css

This should probably be obvious but why am I getting this error?

Invalid CSS after "bundle ": expected selector, was "@import "bootst..."

I don't see anything wrong with this file. I also HAVE installed bootstrap-sass gem

Here is where the error points to:

bundle @import "bootstrap";

/* mixins, variables, etc. */

$grayMediumLight: #eaeaea;

I don't see what could be wrong here. Anyone with more experience with CSS, bootstrap and rail?

Ruby on Rails beginner: Unitialized constant SessionsHelper::User

I am just starting out learning Ruby on Rails and am trying to design a microposts site where users can post and follow other users. I keep getting this error "uninitialized constant SessionsHelper::User. I have set up the controller SessionsHelper.rb as follows and am not sure why I am getting this error - do I have to initialize "user" some other way or somewhere else?

SessionsHelper.rb file:

class UsersController < ApplicationController
  before_filter :signed_in_user,
            only: [:index, :edit, :update, :destroy, :following, :followers]
before_filter :correct_user,   only: [:edit, :update]
before_filter :admin_user,     only: :destroy

def index
 @users = User.paginate(page: params[:page])
end

def show
  @user = User.find(params[:id])
  @microposts = @user.microposts.paginate(page: params[:page])
end

def new
  @user = User.new
end

def create
  @user = User.new(params[:user])
  if @user.save
    sign_in @user
    flash[:success] = "Welcome to the Sample App!"
    redirect_to @user
  else
    render 'new'
  end
end

def edit
end

def update
if @user.update_attributes(params[:user])
  sign_in @user
  flash[:success] = "Profile updated"
  redirect_to @user
else
  render 'edit'
end
end

def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed"
redirect_to users_path
end

def following
@title = "Following"
@user = User.find(params[:id])
@users = @user.followed_users.paginate(page: params[:page])
render 'show_follow'
end

def followers
@title = "Followers"
@user = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end

private

  def correct_user
  @user = User.find(params[:id])
  redirect_to root_path unless current_user?(@user)
  end

  def admin_user
    redirect_to root_path unless current_user.admin?
  end
end

One line custom validation

In a Proc I would like to validate that the provided statement returns true.

I tried the following:

validate :home, Proc.new { |c| c.home.kitchen.present? }

It does not work. It does not show validations errors when no kitchen is present.

Do I have to write it like this:

validate :home_kitchen_must_be_present

def home_kitchen_must_be_present
   ....

Or is there an available syntax to write it in one line? Thanks