Nelson

Joined

3,230 Experience
1 Lesson Completed
4 Questions Solved

Activity

Posted in Routing for Admin area not working

Amazing! Thank yous very much! All working now... Initially It was only Creatint but not saving but I just removed the method: :post

Posted in Routing for Admin area not working

I've got an application that makes use of an admin area for managing Articles on the frontend.
Its a simple CRUD app where pretty much everything Admin related is namespaced and inside admin folders eg: 'views/admin/...', 'controllers/admin/...', etc

The problem I've got is that the form I implemented for creating and changing Articles is not working at all, it seems to try to use the regular /articles/#post route instead of /admin/articles/#post

Form:

<%= form_with model: @article, method: :post do |f| %>
  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control input-lg' %>
  </div>
    <%= f.submit 'Create Article', class: 'btn btn-success' %>
<% end %>

Controller:

class Admin::ArticlesController < Admin::BaseController
  def new
    @article = Article.new
  end

  def create
    @article = Article.new(article_params)
    if @article.save
      redirect_to admin_articles_path
    else
      render json: @article
    end
  end

  def update
    @article = Article.find(params[:id])
    if @article.update(article_params)
      redirect_to admin_articles_path
    else
      render json: @article
    end
  end

  def edit
    @article = Article.find(params[:id])
  end

  def destroy
    @article = Article.find(params[:id])
    @article.destroy
    redirect_to admin_articles_path
  end

  def index
    @page_title = 'Articles'
    @articles = Article.all.order(:created_at)
  end

  def show
    @article = Article.find(params[:id])
  end

  private
  def article_params
    params.require(:article).permit(:title, :excerpt, :content, :category_id, :tags_as_string)
  end
end

Routes:

Rails.application.routes.draw do
  devise_for :users

  root 'home#index'

  get 'admin' => 'admin/dashboard#index'
  get 'about' => 'home#about'
  get 'contact' => 'home#contact'

  resources :documents, only: [:index, :show]
  resources :questions, only: [:index]
  resources :categories, only: [:show]
  resources :articles, only: [:index, :show]

  namespace :admin do
    resources :documents
    resources :users
    resources :questions
    resources :categories
    resources :articles
  end
end

Rake routes (Grep article):

                 articles GET    /articles(.:format)                                                                      articles#index
                  article GET    /articles/:id(.:format)                                                                  articles#show
           admin_articles GET    /admin/articles(.:format)                                                                admin/articles#index
                          POST   /admin/articles(.:format)                                                                admin/articles#create
        new_admin_article GET    /admin/articles/new(.:format)                                                            admin/articles#new
       edit_admin_article GET    /admin/articles/:id/edit(.:format)                                                       admin/articles#edit
            admin_article GET    /admin/articles/:id(.:format)                                                            admin/articles#show
                          PATCH  /admin/articles/:id(.:format)                                                            admin/articles#update
                          PUT    /admin/articles/:id(.:format)                                                            admin/articles#update
                          DELETE /admin/articles/:id(.:format)                                                            admin/articles#destroy

Puma logs:


Started POST "/articles" for 127.0.0.1 at 2019-01-27 06:39:16 -0300

ActionController::RoutingError (No route matches [POST] "/articles"):

Any idea how to force the use of the admin controller?

Posted in Separate Asset Pipeline

Short answer: by using <%= javascript_include_tag 'admin', 'data-turbolinks-track': 'reload' %> and some conditional on the application.html. So if you're in the admin controller do display one specific css/js set and a different one if not.

Posted in Separate Asset Pipeline

Im building a webapp that has an admin panel... I separated this by creating a new folder in the controllers 'admin' and also on my views... This way I can have two completely different styles, one for FE and one for the BE... I wanted to use tinymce for creating posts so installed the gem but I can only see the JS and CSS files showing up on the FE, nothing get loaded when I'm on the admin side unless I specifically enter the script tags... How can I bring specific libraries from the application.js file into my admin area?

Posted in Stuck on creating new record

Thanks Jacob... I havent been using the scaffold generator, but I will do in the future to avoid these confusions.

Posted in Stuck on creating new record

Hi Jacob! Thanks for your reply... I just did and error mutated into:

**NoMethodError in Account#new
Showing /app/views/account/new.html.erb where line #7 raised:

undefined method 'accounts_path' for #<#< Class:0x00007fea9b19aa40 >:0x00007fea9944e478>
Did you mean? account_path**

(?)

Posted in Stuck on creating new record

I'm not sure why I'm so lost on this one, I dont rally have a ton of experience with Rails but I did a blog with basic CRUD functionality and that was all good but this new project its just giving me a massive headache!

The app is very small, it uses Devise for auth so the idea is that a user creates an account and then from that account it can have a dashboard display information... that bit is working fine... the problem comes when I want the user to add new records to an 'Accounts' table...

Database schema is like this:

  • Users (devise)
  • Accounts
  • Usage

So every user can create (and have) many accounts and every account can have many (Usage[s])

My routes:

Rails.application.routes.draw do
  devise_for :users
  root 'home#index'

  resources :account do
    resources :usage
  end
end

The accounts_controller.rb

class AccountController < ApplicationController
  def new
    @account = Accounts.new
  end

  def create
    @account = Account.new(
      :user_id => current_user.id,
      :user_name => account_params[:user_name],
      :password => account_params[:password])
    if @account.save
      render root_path
    end
  end

  private
  def account_params
    params.require(:account).permit(:user_name, :password)
  end
end

The new.html.erb for Accounts

<h1>Add account</h1>

<%= form_for @account, account_path do |f| %>
  <%= f.text_field :user_name, placeholder: 'Username' %>
  <%= f.password_field :password, placeholder: 'Password' %>
  <br /><br />
  <%= f.submit 'Create', class: 'waves-effect waves-light btn' %>
<% end %>

The error I'm getting:

***ActionController::UrlGenerationError in Account#new
Showing /app/views/account/new.html.erb where line #7 raised:

No route matches {:action=>"show", :controller=>"account"}, missing required keys: [:id]*

I tried to nest my routes for Accounts inside the Users one and then I can create the account but the URL looks ugly...

I dont want /users/1/accounts/new its just not right because why would I want to display my current user ID... i want /accounts/new that one makes sense...

Any help please.

Posted in "Controller action should call one model method..." (?)

Ok, I got rid of it... I refactored as follows:

application_controller.rb

class ApplicationController < ActionController::Base
  def check_user_login
    @user = User.find(current_user.id)

    @user.profile?
  end
end

user.rb

class User < ApplicationRecord
  has_one :user_profile

  def profile?
    create_profile unless UserProfile.exists?(user_id: id)
  end

  def create_profile
    UserProfile.create([{ new_profile }])
  end
end

Posted in "Controller action should call one model method..." (?)

Controller action should call one model method other than an initial find or new
This inspection warns if a controller action contains more than one model method call, after the initial .find or .new. It’s recommended that you implement all business logic inside the model class, and use a single method to access it.

I keep getting this warning on my IDE when I do stuff like:

application_controller.rb (everything inside the unless is flagged)

class ApplicationController < ActionController::Base
  helper_method :check_user_login

  def check_user_login
    @user = User.find(current_user.id)

    unless @user.has_profile(@user.id)
      @user.create_profile(@user.id)
    end
  end
end

user.rb

class User < ApplicationRecord
  has_one :user_profile

  def has_profile(id)
    UserProfile.exists?(user_id: id)
  end

  def create_profile(id)
    new_profile = UserProfile.new
    new_profile.user_id = id
    new_profile.first_name = 'New'
    new_profile.last_name = 'User'
    new_profile.avatar_url = 'default.png'
    new_profile.save
  end
end

Whats the correct way of doing this then?

Posted in Rails changing HTML tag possiton (?)

Nevermind... after a few hours of debugging I manage to find the source of my suffering... it was Turbolinks... I removed that and now thinks are back on track...

Posted in Rails changing HTML tag possiton (?)

Im using AdminLTE 2 to build a small dashboard and everything was working nicely until I started adding links to the controllers on my sidebar.

On first loading the dashboard everything works as expected (dropdowns, sidebars, etc) on going to another controller (/employee) the functionality of these names components its just not there, but the console logs no errors, on clicking on the menu to go back to the Dashboard the problem persists. The only way of bringing the missing functionality back is to refresh the page.

I did a DiffCheck of the dashboard index on first load (everything works) and then once I'm back from any controller (functionality missing) and there is some tags changing in possition and others just completely change its attributes. See pictures:

https://i.imgur.com/Mp31WFp.png

https://i.imgur.com/JtM2Q2t.png

https://i.imgur.com/8Y7r0tK.png

Posted in Creating reports in Rails

Such a primitive implementation tho...

Posted in Creating reports in Rails

Hi,
I used to develop software using the Microsoft stack for both desktop and web, now I'm trying Rails for web and I was just wondering how can I produce reports for the users to save/print. Say invoices, user lists, customer lists and in general any tipe of data I can produce from the database (psql).
I used to use Telerik reporting with an amazing editor that allowed me to work on the design and then query the database or just pick a table or view and then just import and make a call to the report from somewhere in my app code. I'm now a bit lost trying to find this sort of facility for Rails.

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.