How to Use Sessions to walk user through Sign Up? - ruby-on-rails

On the home page:
sessions/_goal.html.erb
<%= simple_form_for(:session, url: new_goal_path) do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
# ERROR MESSAGE:
Routing Error
No route matches [POST] "/goals/new"
Once the user submit's that he should be redirected to:
sessions/_habit.html.erb
<%= simple_form_for(:session, url: new_habit_path) do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
Once the user submit's that he should be redirected to:
<%= link_to "Sign Up via Facebook", "/auth/facebook" %> or
<%= link_to "Sign Up via Email", signup_path %>
The information they put in those two partials should then be stored so that when they are signed in they will see it as part of their profile.
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def goal
end
def facebook
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to root_url
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
if user.activated?
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_to root_url
else
message = "Account not activated. "
message += "Check your email for the activation link."
flash[:warning] = message
redirect_to root_url
end
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
routes
Rails.application.routes.draw do
put '/mark_completed/:id', to: 'habits#mark_completed', as: 'mark_completed'
put '/mark_accomplished/:id', to: 'goals#mark_accomplished', as: 'mark_accomplished'
get 'notes/index'
get 'notes/new'
get 'notifications/index'
get 'auth/:provider/callback', to: 'sessions#facebook'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
get 'password_resets/new'
get 'password_resets/edit'
get "/users/:user_id/goals", to: "goals#user_goals", as: "user_goals"
shallow do
resources :habits do
resources :comments
resources :notes
resources :notifications
end
resources :valuations do
resources :comments
resources :notes
resources :notifications
end
resources :goals do
resources :comments
resources :notes
resources :notifications
end
resources :stats do
resources :comments
resources :notes
resources :notifications
end
end
resources :notes
resources :habits do
collection { post :sort }
resources :notes
resources :notifications
resources :comments do
resources :likes
end
resources :likes
member do
post :like
post :notifications
end
resources :levels do
# we'll use this route to increment and decrement the missed days
resources :days_missed, only: [:create, :destroy]
end
end
resources :goals do
resources :notes
resources :comments
member do
post :like
end
end
resources :valuations do
resources :notes
resources :comments
resources :notifications
member do
post :like
post :notifications
end
end
resources :stats do
resources :notes
resources :comments
resources :notifications
member do
post :like
end
end
resources :results
resources :users
resources :account_activations, only: [:edit]
resources :activities do
resources :valuations
resources :habits
resources :stats
resources :goals
end
resources :notifications do
resources :valuations
resources :habits
resources :stats
resources :goals
resources :comments
end
resources :comments do
resources :comments
resources :notifications
member do
post :like
end
end
resources :password_resets, only: [:new, :create, :edit, :update]
resources :relationships, only: [:create, :destroy]
get 'tags/:tag', to: 'pages#home', as: :tag
resources :users do
member do
get :following, :followers
end
end
get 'about' => 'pages#about'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
root 'pages#home'
I have no idea what I'm doing here. Any help would greatly be appreciated :]

That will take some work.
First, fix your routes (post them here as you was rightfully asked).
Next, change your partials into action templates (remove underscore _ from their names).
Then in your controller actions use params[...] to retrieve data (e.g. params[:session][:name] for the first step) and save them in session (session[:name] = params[:session][:name]).
At the end of action call redirect_to :another_action (change :another_action to the concrete action you want next – for example in goal method that would be redirect_to :habit_url – and you should add that habit method to your controller).
Then, in your facebook-related methods you will have session[:name] and other stuff available, so you just fetch it from there and push into database (user.update(name: session[:name])).
Reading material:
Action Controller Overview – you need to attentively read this first of all
Active Record Basics – how to save user details to database
Rails Routing from the Outside In – just look through that, paying attention to places which are relevant to your current setup

Related

Having trouble using unless current_page? to hide a button

I'm trying to hide this button from all the show pages of the Rooms model. I have an array of the rooms id stored in #extra but whenever I try to use current_page?(room_path(#extra)) it doesn't work, but when I specify the id, for example, current_page?(room_path(6)) it works and hides the button. What am I doing wrong?
<% unless current_page?(controller: 'rooms') || current_page?(room_path(#extra))%>
<button onclick="myFunction()" class="button btn btn-light bg-white rounded-pill shadow-sm px-4 mb-4"
style="vertical-align:middle">
<span>
<small class="text-uppercase font-weight-bold"> Nav-bar Toggle</small>
</span>
</button>
<% end %>
routes/rb
require 'sidekiq/web'
Rails.application.routes.draw do
resources :messages
resources :rooms
# Routing for budget section
resources :budgets do
resources :groups do
resources :categories
end
end
resources :posts do
resources :likes
resources :comments
end
resources :socials
resources :mentions, only: [:index]
resources :mentioned_posts, only: [:index]
resources :follows
resources :followers, only: [:index]
resources :followings, only: [:index]
resources :accounts
resources :goals
resources :retirements
get '/users', to: 'users#index'
get '/user/:id', to: 'users#show', as: 'user'
resources :calculate_debts
get '/privacy', to: 'home#privacy'
get '/terms', to: 'home#terms'
authenticate :user, lambda { |u| u.admin? } do
mount Sidekiq::Web => '/sidekiq'
end
resources :notifications, only: [:index]
resources :announcements, only: [:index]
devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
root to: 'home#index'
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end
Based on the information provided in the comments. The show path needs an id to be complete. You can check it with,
current_page?(controller: 'rooms', action: 'show', id: room_id)
instead of
current_page?(controller: 'rooms', action: 'show')
If you want to check against a particular room id, you can use
current_page?(controller: 'rooms', action: 'show', id: room_id)
If you want to check just the controller and action, irrespective of the room id, you'll have to use another strategy. You can use,
if params[:controller] == :rooms && params[:action] == :show
or match against a regex
if request.fullpath =~ /\/rooms\/\d+/

NoMethod Error with on a `favorites' join model Rails 5.2

I'm trying to allow a User to favorite a coffee_roast. However on trying to load the show page I get a NoMethod Error
undefined method `favorite_coffee_roast_path'
which comes from this line of code:
<%= link_to "Add to favorites", favorite_coffee_roast_path(#coffee_roast, type: "favorite"), method: :put %><br />
I have tried a number of variations such as:
favorite_roast_coffee_roast_path
favorite_coffeeroast_coffee_roast_path
favorite_coffee_roast_coffee_roast_path
None obviously work.
My Models
class User < ApplicationRecord
has_merit
has_one :drink
has_many :coffeeshops
has_many :coffee_roasts
has_many :favorite_coffeeroasts
has_many :favorite_roasts, through: :favorite_coffeeroasts, source: :coffee_roast
class CoffeeRoast < ApplicationRecord
has_many :favorite_coffeeroasts
has_many :favorited_by, through: :favorite_coffeeroasts, source: :user
class FavouriteCoffeeroast < ApplicationRecord
belongs_to :coffee_roast
belongs_to :user
My Controller
class CoffeeRoastsController < ApplicationController
...
def favorite_coffeeroast
#coffee_roast = CoffeeRoast.find(params[:id])
type = params[:type]
if type == "favorite"
current_user.favorite_roasts << #coffee_roast
redirect_to #coffee_roast, notice: "You favorited #{#coffee_roast.name}"
elsif type == "unfavorite"
current_user.favorite_roasts.delete(#coffee_roast)
redirect_to #coffee_roast, notice: "Unfavorited #{#coffee_roast.name}"
else
# Type missing, nothing happens
redirect_to #coffee_roast, notice: "Nothing happened."
end
end
routes.rb
Rails.application.routes.draw do
#core root
get 'home/index' => 'home#index'
root 'home#index'
#roasts redirect
get '/roasts', to: redirect('/coffee_roasts', status: 302)
namespace :api do
namespace :v1 do
resources :roasts
end
end
resources :blends
resources :roasters
resources :countries
resources :regions
resources :comments
resources :coffee_flavours
resources :flavours
resources :drinks
devise_for :users
devise_for :admins
resources :varietals
resources :tags
resources :coffee_beans
resources :coffee_roasts
resources :processings
get 'contact-me', to: 'messages#new', as: 'new_message'
post 'contact-me', to: 'messages#create', as: 'create_message'
#static pages
get 'about', to: 'pages#about'
get 'cookiepolicy', to: 'pages#cookiepolicy'
get 'map', to: 'pages#map'
get 'longblack', to: 'longblack#index'
get 'prices', to: 'prices#new'
post 'prices', to: 'prices#create'
#db resources
resources :roasters do
resources :comments
end
resources :articles do
resources :comments
end
resources :coffeeshops do
resources :comments
end
resources :roasts do
resources :comments
end
resources :coffee_roasts do
resources :comments
end
resources :coffeeshops do
put :favorite, on: :member
end
resources :coffeeshops do
put :bookmarked, on: :member
end
Why can't I find the right path? What am I missing?
Looks like you need to read more about routing in ROR. Add the new action to existing resources :coffee_roasts:
resources :coffee_roasts do
put :favorite_coffeeroast, on: :member
resources :comments
end
Also you are duplicating routes when you write them with and without nesting. Since you want to nest everywhere comments routes you can remove corresponding routes without nesting

ActionController::UrlGenerationError: No route matches

I am receiving a no route matches error from the line <%= link_to "Ask User Out", askout_user_message_path(#user), :class => "button" %>.
This used to work before I added a gem but now it stopped working. I tried moving under collection but I get no luck with that as that's where it used to be.
Routes:
resources :users do |user|
resources :messages do
member do
post :new
get 'askout', action: 'askout'
end
end
collection do
get :trashbin
post :empty_trash
end
end
resources :conversations do
member do
post :reply
post :trash
post :untrash
end
end
Old Routes:
resources :users do |user|
resources :messages do
collection do
post 'delete_multiple'
get 'askout', action: 'askout'
get 'reply', action: 'reply'
end
end
end
My routes changed as I added mailboxer gem.
You'd be better doing this:
#config/routes.rb
resources :users do
resources :messages do
member do
post :new
get :askout
end
end
collection do
get :trashbin
post :empty_trash
end
end
This will give you:
users/1/messages/5/askout
What I think you want:
#config/routes.rb
resources :users do
resources :messages do
post :new
collection do
get :askout
end
end
collection do
get :trashbin
post :empty_trash
end
end
This will give you:
users/2/messages/askout
The path helper will be as determined in the rake routes view -- you should look at it to get an idea as to what your route is called (allowing you to write it accordingly)

Rails: Routing Issue, Button to Bring up partial

I have a school page that has tabs when clicked upon are suppose to bring up microposts. The issue is I don't think I am routing it correctly and I feel like an idiot trying to figure this out and not succeeding. If anyone has suggestions please feel free to help me out! Thank you so much!
Routes.rb
get "/schools/:id/mostrecent_schools" => "users#microposts", :as => "mostrecent_schools"
School Controller
def mostrecent
#school = School.find_by_slug(request.referer.gsub('http://localhost:3000/','')).id
#microposts = #user.microposts.paginate(:per_page => 10, :page => params[:page])
respond_to do |format|
format.html
format.js
end
end
Tab HTML
li class='StreamTab StreamTabRecent active'>
<%= link_to 'Most Recent', mostrecent_schools_path, :remote => true, :class => 'TabText' %>
</li>
<div id='ContentBody'>
<div id='ajax'></div>
<%= render 'users/microposts', :microposts => #microposts %>
</div>
mostrecent.js
$("#ajax").hide();
$("#ContentBody").html('<%= escape_javascript(render :partial => "users/microposts" )%>');
EDIT
*Routes.rb*
Projects::Application.routes.draw do
resources :pages
resources :application
resources :schools
resources :microposts
resources :comments
resources :users
resources :sessions
resources :password_resets
resources :relationships, only: [:create, :destroy]
resources :users do
member do
get :following, :followers
end
end
resources :microposts do
member do
post :vote_up, :unvote
end
end
resources :microposts do
member do
post :upview
end
end
resources :microposts do
resources :comments
end
get "schools/:page/mostrecent" => "schools#mostrecent", :as => "mostrecent_schools"
root to: "pages#index"
From what I can understand, Your routes.rb should look something like this
My final try
Change your routes.rb to this
get "schools/mostrecent/new/:page" => "schools#mostrecent", :as => "mostrecent_schools"
and in your controller edit this line. If this dosen't work then i give up
#school = School.find_by_slug(request.referer.gsub('http://localhost:3000/','')).params[:page]
Although this is not the restful way of doing stuff and as far as I know since users belong to schools and microposts belong to users you shouldn't define schools microposts and users as simple :resources.
Refer to [Rails Routing Guide ](Refer to http://guides.rubyonrails.org/routing.html)for more details.

Rails 3 routing from root show

Currently
Project101::Application.routes.draw do
match '/:id' => 'companies#show'
resources :companies do
resources :customers
resources :users
resources :categories
resources :addresses
end
devise_for :users
resources :users, :controller => "users"
root :to => "companies#index"
end
Everything belongs to a company. Trying to create routes like www.example.com/:id/customers where :id is always the company id.
At the moment www.example.com/:id works but all url's are generated as /companies/:id/cusotmers.
Saw Rails 3 Routing Resources with Variable Namespace.
Is this the right way of doing this?
EDIT
Kept :as => :company to help generate the URL's, Links, etc a little easier for me. Sure others could do cleaner or better method. Also had to manually create the edit, destroy, new with different urls so I could use them in links if user was admin.
Project101::Application.routes.draw do
match '/' => 'companies#index'
match '/companies' => 'companies#index'
match '/:company_id' => 'companies#show', :as => :show_company
match '/companies/:id/edit' => 'companies#edit', :as => :edit_company
match '/companies/:id/new' => 'companies#new', :as => :new_company
match '/companies/:id/destroy' => 'companies#destroy', :as => :delete_company
scope '/:company_id', :as => :company do
resources :customers
resources :users
resources :categories
resources :services
resources :addresses
end
devise_for :users
resources :users, :controller => "users"
root :to => "companies#index"
end
Then just used basic nested_resources for links, controllers and forms.
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_company
def current_company
if params[:company_id] != nil
#current_company ||= Company.find(params[:company_id])
else
#current_company = nil
end
return #current_company
end
end
Basic links
<%= link_to "Customers", company_customers_path(current_company) %>
links for specific customer
<%= link_to #customer.name, edit_company_customer_path(current_company, #customer) %>
Controllers look like
class CustomersController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource
def new
#company = current_company
#customer = #company.customers.new
end
def create
#customer = Customer.new(params[:customer])
if #customer.save
flash[:notice] = "Successfully created customer."
redirect_to company_customer_path(current_company, #customer)
else
render :action => 'new'
end
end
end
And finally my forms look like
<%= form_for [#company, #customer] do |f| %>
<%= f.error_messages %>
....
<% end %>
Yes, if you always want the routes to begin with the company id you can wrap them in a scope like this:
scope ":company_id" do
resources :customers
resources :users
resources :categories
resources :addresses
end

Resources