RoR - Cannot see top menu bar in production - html

I created a rails app, added a top menu bar with position fixed. It works fine in development but when I deploy it online, I can't see my menu bar. Also I added glyph icons using FontAwesome but it doesn't show that either.
My production.rb
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.serve_static_assets = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
config.assets.compile = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
My application.html.haml
!!!
%html
%head
%title Blogger
= stylesheet_link_tag 'application', media: 'all'
= javascript_include_tag 'application'
%link{:rel => "stylesheet", :href => "http://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css"}
%link{:rel => "stylesheet", :href => "http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css"}
= csrf_meta_tags
%body
%header
.wrapper.clearfix
#logo= link_to "Blogger", root_path
%nav
- if user_signed_in?
= link_to current_user.name, edit_user_registration_path
= link_to "Logout", destroy_user_session_path, :method => :delete
= link_to "Add New Post", new_post_path, class: "button"
- else
= link_to "Sign in", new_user_session_path
= link_to "Sign Up", new_user_registration_path
= link_to t('Contact'), new_contact_path
%p.notice= notice
%p.alert= alert
= yield
To be specific, it doesn't load anything under the %header

Related

Rails - Save client model into the database

Hey here! I'm kinda new to Rails and I've been trying to find some answers but no luck yet so here we go.
I've set up a basic Rails app and just trying to save a Client to my database with a validation but nothing seems to be coming together. Anyone could point me to the right direction please or let me know what I've been doing wrong in my code.
I keep getting errors like this:
NoMethodError in Clients#new
Showing /Users/******/Documents/******/*****/app/views/clients/_form.html.erb where line #1 raised:
undefined method `clients_path' for #<ActionView::Base:0x00000000064a50>
Did you mean? clients_new_path
Even if I remove #client = Client.new from the new method I can view the page but nothing gets saved.
I'm stuck really now so any help much appreciated!
Thanks!
My Routes.rb file:
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
get 'dashboard/index'
root to: "home#index"
devise_for :users, controllers: {
sessions: 'users/sessions',
passwords: 'users/passwords',
registrations: 'users/registrations'
}
get '/clients/index'
get '/clients/new'
get '/clients/edit'
get '/clients/delete'
get '/clients/:id', to: 'clients#show'
post '/clients/new', to: 'clients#create'
end
My Dashboard file:
<% if user_signed_in? %>
<nav class="subnav">
<ul>
<li><%= link_to('My Clients', clients_index_path) %></li>
<li><%= link_to('Add Client', clients_new_path) %></li>
<li><%= link_to('Edit registration', edit_user_registration_path) %></li>
<li><%= link_to('Logout', destroy_user_session_path, :method => :delete) %></li>
</ul>
</nav>
<% else %>
<%= link_to('Register', new_user_registration_path) %>
<%= link_to('Login', new_user_session_path) %>
<% end %>
My ClientsController file:
class ClientsController < ApplicationController
def index
#clients = Client.all
end
def new
#client = Client.new
end
def show
#client = Client.find(params[:id])
end
def create
#client = Client.new(client_params)
if #client.save
redirect_to #client
else
render :new
end
end
def edit
end
def delete
end
private
def client_params
params.require(:client).permit(:name, :provider)
end
end
My form:
<%= form_with model: #client do |form| %>
<div>
<%= form.label :name %><br>
<%= form.text_field :name %>
<% client.errors.full_messages_for(:name).each do |message| %>
<div><%= message %></div>
<% end %>
</div>
<div>
<%= form.label :provider %><br>
<%= form.text_field :provider %>
</div>
<div>
<%= form.label :business_type %><br>
<%= form.select :business_type, ["Partnership", "Sole Trader", "Limited Company"] %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>
Finally my new.html.erb file:
<h1>Clients#new</h1>
<%= render 'form', client: #client %>
clients_path is generated by resources :clients, only: :index or you probably need to give your route the name you want. Try this
get '/clients/index', as: :clients
or, if you want to specify non default paths as you're doing, your index is probably called clients_index_path, but you can check that with a rake routes or rails routes, because I'm not sure.
That said, I suggest you to go with the resources method in your routes file and use the default paths as you're trying to do. Something like
resources :clients
but now you don't have a path like /clients/index no more, just /clients for the index action.
If you're in doubts with routes try to read the guide about routing
The Rails way to declare the routes to Create, Update, Read and Destroy (CRUD) a resource is just:
Rails.application.routes.draw do
mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
get 'dashboard/index'
root to: "home#index"
devise_for :users, controllers: {
sessions: 'users/sessions',
passwords: 'users/passwords',
registrations: 'users/registrations'
}
resources :clients
end
As you can see by the annotions above each method Rails does not add the "action" to the path except for the new and edit routes:
class ClientsController < ApplicationController
before_action :set_client, only: [:show, :edit, :update, :destroy]
# GET /clients
def index
#clients = Client.all
end
# GET /clients/1
def show
end
# GET /clients/new
def new
#client = Client.new
end
# POST /clients
def create
#client = Client.new(client_params)
if #client.save
redirect_to #client
else
render :new
end
end
# GET /clients/edit
def edit
end
# PATCH /clients/1
def update
if #client.update(client_params)
redirect_to #client
else
render :edit
end
end
# DELETE /clients/1
def delete
#client.destroy
redirect_to action: :index,
notice: 'Client deleted'
end
private
def set_client
#client = Client.find(params[:id])
end
def client_params
params.require(:client).permit(:name, :provider)
end
end
Thus you don't create resources with post '/clients/new' - You use POST /clients. Also when you use the "bare-bones" routing methods such as match, get, post etc Rails does not automatically add routing helper. If you actually wanted to generate the equivilent routes you would need to use:
post '/clients',
to: 'clients#create',
as: :clients
But you're much better off embracing the conventions and learning to use them to be productive.

What is the correct way to upload files (image, video, audio, text, ...) to Cloudinary through a unique input with simple_form_for on Rails?

I am trying to implement a simple_form_for in my Rails app to attach many files on a ruby model. I want to upload several types of files (image, PDF, video, audio, .doc, ...). I use the ruby gem Cloudinary (with a free account) to store files in the cloud and Active Storage to link files with model instances. I use Rails 5.2.4.1 and Ruby 2.6.5 (see below my actual code)
Here is my model file:
class Substep < ApplicationRecord
has_many_attached :documents
end
Strong params controller :
class SubstepsController < ApplicationController
[...]
private
def substep_params
params.require(:substep).permit(documents: [])
end
end
Gemfile.rb :
gem 'cloudinary', '~> 1.12.0'
gem 'simple_form'
My API key in .env file :
CLOUDINARY_URL=............
config/storage.yml :
cloudinary:
service: Cloudinary
config/environments/development.rb :
config.active_storage.service = :cloudinary
app/views/substeps/_form.html.erb :
<%= simple_form_for(#substep) do |f| %>
<%= f.input :documents, as: :file, input_html: { multiple: true }, label: "Select files" %>
<%= f.submit "Send documents" %>
<% end %>
With this setup, I can manage images & PDF files easily.
However, I have read Cloudinary documentation (https://cloudinary.com/documentation/rails_integration) and simple_form_for documentation (https://github.com/heartcombo/simple_form) to find the right way to manage also video, audio & text files with the same setup, but I have failed so far.
I've find in the Cloudinary documentation that we can use an option ":resource_type => :auto" to manage different types of files automatically. I have already tried several configurations which have given nothing.
<%= f.input :documents, as: :file, input_html: { multiple: true, resource_type: :auto },... %>
<%= f.input :documents, as: :file, input_html: { multiple: true }, resource_type: :auto,... %>
<%= f.input :documents, as: :file, input_html: { multiple: true, resource_type: "auto" },... %>
<%= f.input :documents, as: :file, input_html: { multiple: true }, resource_type: "auto",... %>
<%= simple_form_for(#substep, resource_type: :auto) do |f| %>
<%= simple_form_for(#substep), resource_type: :auto do |f| %>
I don't success to find how I can add this option to my simple_form_for input. Someone can help me, please ?
EDIT 2020/07/13 :
I have found the solution thanks to another Dev. Here is his answer below.
"Hi! I just had the same problem as you and my solution was to update the cloudinary gem. In previous versions the cloudinary gem had "image" as the default for resource_type and it looks like you can't pass that option through active storage to cloudinary.
As soon as I removed that '~> 1.12.0' from the Gemfile and updated the gem to version 1.16, it started accepting the upload of other media types.
Probably the resource_type option changed to auto somewhere along the line."

Nav bar logo out of place HAML

I'm creating a website called NoteIt (using HAML), where users can create notes, and only view the notes they've created. How do you select a link_to part to style it?
Here is my application.html.haml file:
!!!
%html
%head
%title NoteIt | Your online Notebook
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag 'application', 'data-turbolinks-track' => true
= csrf_meta_tags
%body
%header
.header_inner
%h3 NoteIt << Here is where the NoteIt sign is placed
%nav
-if model_signed_in?
= link_to "New Note", new_note_path
= link_to "Sign Out", destroy_model_session_path, method: :delete
- else
= link_to "Log In", new_model_session_path
%p.notice= notice
%p.alert= alert
= yield
I solved it. All you need to do is change the margin-top and margin-bottom, and set it to the desired amount of px. For the logo, I wrote NoteIt with an h1 tag.

Rails POST params not accessed in controller

I'm trying to write an application that will have to interact with POST but I'm having issues accessing the parameters. This is not meant to be useful right now but I'm just trying to flash the result of the form. According to google dev tools, the POST parameter is set to 1.
Here's my routes.rb file
devise_scope :user do
put 'users/toggle_activation' => 'users/sessions#toggle_activation'
get 'users/sign_out' => 'users/sessions#destroy'
post 'pages/home' => 'users/sessions#activities'
end
This is the controller in question
def activities
params.permit(:tennis)
current_user.save
flash[:notice] = params[:tennis]
redirect_to root_path
end
This is the form code
<%= form_for :user do |f| %>
<%= f.check_box :tennis %>
<%= f.submit %>
<% end %>
Any help is appreciated.
If you're using "form_for", then the check_box name will result as "user[tennis]", not just "tennis". View source in your browser and you should see this.
Do something like the following in your controller method (although I'm not sure how it will be called with "form_for :user" because your "activities" route isn't in your routes.rb in the code above):
user_params = params.require(:user).permit(:tennis)
flash[:notice] = user_params[:tennis]

"No route matches" : Nightmare with routing rails namespace

I'm geting crazy with a namespace URL that leads to incorrect action 'show' instead of 'new'.
When I'm using this URL : admin/admin_utilisateurs/new
I get this error :
Routing Error
No route matches {:action=>"show", :controller=>"admin/admin_utilisateurs"}
Try running rake routes for more information on available routes.
This is the link_to I'm using in my index page :
link_to 'Nouveau', new_admin_admin_utilisateur_path, :class => 'btn-text btn-dimensions btn-encrusted metal'
These are my rake routes :
root / welcome#index
pub_responsables GET /catalogs/managers(.:format) pub_responsables#index
POST /catalogs/managers(.:format) pub_responsables#create
new_pub_responsable GET /catalogs/managers/new(.:format) pub_responsables#new
edit_pub_responsable GET /catalogs/managers/:id/edit(.:format) pub_responsables#edit
pub_responsable GET /catalogs/managers/:id(.:format) pub_responsables#show
PUT /catalogs/managers/:id(.:format) pub_responsables#update
DELETE /catalogs/managers/:id(.:format) pub_responsables#destroy
new_admin_utilisateur_session GET /admin_utilisateurs/sign_in(.:format) devise/sessions#new
admin_utilisateur_session POST /admin_utilisateurs/sign_in(.:format) devise/sessions#create
destroy_admin_utilisateur_session DELETE /admin_utilisateurs/sign_out(.:format) devise/sessions#destroy
admin_utilisateur_password POST /admin_utilisateurs/password(.:format) devise/passwords#create
new_admin_utilisateur_password GET /admin_utilisateurs/password/new(.:format) devise/passwords#new
edit_admin_utilisateur_password GET /admin_utilisateurs/password/edit(.:format) devise/passwords#edit
PUT /admin_utilisateurs/password(.:format) devise/passwords#update
cancel_admin_utilisateur_registration GET /admin_utilisateurs/cancel(.:format) admin_utilisateurs/registrations#cancel
admin_utilisateur_registration POST /admin_utilisateurs(.:format) admin_utilisateurs/registrations#create
new_admin_utilisateur_registration GET /admin_utilisateurs/sign_up(.:format) admin_utilisateurs/registrations#new
edit_admin_utilisateur_registration GET /admin_utilisateurs/edit(.:format) admin_utilisateurs/registrations#edit
PUT /admin_utilisateurs(.:format) admin_utilisateurs/registrations#update
DELETE /admin_utilisateurs(.:format) admin_utilisateurs/registrations#destroy
admin_utilisateur_confirmation POST /admin_utilisateurs/confirmation(.:format) devise/confirmations#create
new_admin_utilisateur_confirmation GET /admin_utilisateurs/confirmation/new(.:format) devise/confirmations#new
GET /admin_utilisateurs/confirmation(.:format) devise/confirmations#show
admin_utilisateur_unlock POST /admin_utilisateurs/unlock(.:format) devise/unlocks#create
new_admin_utilisateur_unlock GET /admin_utilisateurs/unlock/new(.:format) devise/unlocks#new
GET /admin_utilisateurs/unlock(.:format) devise/unlocks#show
admin_admin_utilisateurs GET /admin/admin_utilisateurs(.:format) admin/admin_utilisateurs#index
POST /admin/admin_utilisateurs(.:format) admin/admin_utilisateurs#create
new_admin_admin_utilisateur GET /admin/admin_utilisateurs/new(.:format) admin/admin_utilisateurs#new
edit_admin_admin_utilisateur GET /admin/admin_utilisateurs/:id/edit(.:format) admin/admin_utilisateurs#edit
admin_admin_utilisateur GET /admin/admin_utilisateurs/:id(.:format) admin/admin_utilisateurs#show
PUT /admin/admin_utilisateurs/:id(.:format) admin/admin_utilisateurs#update
DELETE /admin/admin_utilisateurs/:id(.:format) admin/admin_utilisateurs#destroy
For info, I'm using Devise on a users table which I called "admin_utilisateurs".
Devise is working great with options : :database_authenticatable, :confirmable, :recoverable, :registerable, :trackable, :timeoutable, :validatable, :lockable
The point is that I setup another controller for admin purpose on the admin_utilisateurs table.
So here it is my config/routes.rb
root :to => 'welcome#index'
resources :pub_responsables, :path =>'/catalogs/managers'
devise_for :admin_utilisateurs, :controllers => {:registrations => 'admin_utilisateurs/registrations'}
namespace :admin do
resources :admin_utilisateurs
end
So my admin controller is located in app/controllers/admin/admin_utilisateurs_controller.rb
Here is the action of my 'new' controller's action :
class Admin::AdminUtilisateursController < ApplicationController
before_filter :authenticate_admin_utilisateur!
...
def new
#admin_utilisateur = AdminUtilisateur.new
respond_with(#admin_utilisateur)
end
...
end
The view for that controller are located in app/view/admin/admin_utilisateurs/
But the issue is really connected to the route because the other path for 'show', 'edit' and 'update' are working properly.
And if I delete my controller app/controllers/admin/admin_utilisateurs_controller.rb, Rails is not complaining about missing controller, she's still complaining about the "no route for show action".
I'm really lost. Could anyone advise please ?
Thanks in advance
===== UPDATE 1 =====
Here it is the log related to my URL request :
Started GET "/admin/admin_utilisateurs/new" for 127.0.0.1 at 2012-10-25 12:55:05 +0200
Processing by Admin::AdminUtilisateursController#new as HTML
Rendered shared/_main_title.html.haml (0.4ms)
Rendered admin/admin_utilisateurs/_form.html.haml (17.2ms)
Rendered admin/admin_utilisateurs/new.html.haml within layouts/application (31.7ms)
Completed 500 Internal Server Error in 45ms
ActionController::RoutingError (No route matches {:action=>"show", :controller=>"admin/admin_utilisateurs"}):
app/views/admin/admin_utilisateurs/_form.html.haml:1:in `_app_views_admin_admin_utilisateurs__form_html_haml___780348754_85631840'
app/views/admin/admin_utilisateurs/new.html.haml:9:in `_app_views_admin_admin_utilisateurs_new_html_haml__296364877_85537950'
app/controllers/admin/admin_utilisateurs_controller.rb:51:in `new'
It seems that it is properly routed but then there is a problem in the view.
I retried to remove the controller file admin_utilisateurs_controller.rb but this time restart my web server (sudo service apache2 restart) and the error was different.
I got this time a "Routing error uninitialized constant Admin::AdminUtilisateursController".
So this confrim there is an issue in my view...
digging deeper...
OK, I found the issue.
The issue was coming from my partial form view which I use for my 'edit' action and for my 'new' action : /app/views/admin/admin_utilisateurs/_form.html.haml
= form_for #admin_utilisateur, :url => admin_admin_utilisateur_path, :html => {:class => "formulaire-standard"} do |f|
= render :partial => 'shared/error_message', :locals => { :element => #admin_utilisateur, :debut_erreur => 'Cet utilisateur ne peut ĂȘtre enregistrĂ©'}
.groupe-champs
.champ
= f.label :nom
= f.text_field :nom, :class => 'input-width-8-col', :required => 'required'
.champ
= f.label :prenom
= f.text_field :prenom, :class => 'input-width-5-col', :required => 'required'
.champ
= f.label :telephone
= f.telephone_field :telephone, :class => 'input-width-5-col', :required => 'required'
.champ
= f.label :mobile
= f.telephone_field :mobile, :class => 'input-width-5-col'
.champ
= f.label :email
= f.email_field :email, :class => 'input-width-8-col', :required => 'required'
.groupe-champs
= render :partial => 'shared/checkboxes_admin_utilisateur', :locals => { :resource => #admin_utilisateur }
.groupe-champs
.champ-1
= f.check_box :approved
.champ-5
= f.label :approved
.checkbox-explication
= t('activerecord.attributes.admin_utilisateur.explanations.active')
.separator
.groupe-actions
= f.submit 'Enregistrer', :class => 'btn-text btn-dimensions btn-encrusted metal'
= link_to 'Annuler', admin_admin_utilisateur_path, :class => 'btn-text btn-dimensions btn-encrusted metal'
I was using the form_for tag incorrectly. Because my form is used within a namespace, I must add the namespace into its arguments, and remove the :url option because otherwise this form will only work with the 'edit' action :
= form_for [:admin, #admin_utilisateur], :html => {:class => "formulaire-standard"} do |f|
This code lets Rails guess what path to use wether it's for a new record or to edit an existing record. So I don't need to specify the :url and the :method.
The second mistake was (a classical one) the link_to at the bottom of the form.
I forgot the 's' at the end of the route helper (admin_admin_utilisateurSSSS_path):
= link_to 'Annuler', admin_admin_utilisateurs_path, :class => 'btn-text btn-dimensions btn-encrusted metal'
The moral of the story :
ALLWAYS CHECK YOUR /log/development.log FILE ! ;-)