I am new to rails and I used audited 4.7 gem for my rails application to track loggers. I have no idea how do I add a comment to audit table record. thank you
Gemfile
gem "audited", "~> 4.7"
Model
class Client < ApplicationRecord
audit
# add to your Gemfile, and run bundle install to install it
gem "audited"
# install table for audited gem operation
rails generate audited:install
rails db:migrate
# open your model that you want to audited
class Client < ApplicationRecord
audit
end
# restart rails server
# how to check the action
#client = Client.first
#audits = #client.audits
if #audits
#audits.each do |audit|
if audit.user
audit.user.username
audit.action
end
end
end
Related
After upgrading my application from rails 5.2 to rails 6 and I am getting this issue
This model adapter does not support fetching records from the database.
class PlayerArtController < ApplicationController
load_and_authorize_resource only: [:index, :create]
def index
end
def create
end
end
Version:
rails (6.0.3.4)
cancancan (2.3.0)
ruby 2.7.2
As suggested by #Eyeslandic I ended up updating cancancan from 2.3 to 3.0
CanCan::NotImplemented (This model adapter does not support fetching records from the database.):
Records were not fecthed from database, after updating the gem resolved the issue:
# gem 'cancancan', '2.3.0' ## update version to new version
gem 'cancancan', '~> 3.3.0'
The goal is for command...
bin/rails generate custom_scaffold Thing
... to generate the following 6 files:
db/migrate/201812031331_create_things.rb
app/models/thing.rb
app/controllers/things_controller.rb
app/serializers/thing_serializer.rb
test/fixtures/things.yml
test/integration/requests/things_request_test.rb
... using Rails 5.
My current setup does generate app/models/thing.rb but does not populate it with Thing.
Expected:
class Thing < ApplicationRecord
end
Currently:
class <%= class_name %> < ApplicationRecord
end
I have read through these Rails guides but to little avail.
Does anyone have a working example?
My setup:
# lib/generators/custom_scaffold/custom_scaffold_generator.rb
class CustomScaffoldGenerator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
def create_files
copy_file 'migration.rb', "db/migrate/#{timestamp}_create_#{plural_name}.rb"
copy_file 'model.rb', "app/models/#{file_name}.rb"
copy_file 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
copy_file 'serializer.rb', "app/serializers/#{file_name}_serializer.rb"
copy_file 'fixture.yml', "test/fixtures/#{plural_name}.yml"
copy_file 'request_test.rb', "test/integration/requests/#{plural_name}_request_test.rb"
end
private
def timestamp
Time.now.utc.strftime('%Y%m%d%H%M%S')
end
end
# lib/generators/custom_scaffold/templates/model.rb
class <%= class_name %> < ApplicationRecord
end
# lib/generators/custom_scaffold/templates/controller.rb
module V1
module Public
class <%= class_name.pluralize %>Controller < ApplicationController
end
end
end
# lib/generators/custom_scaffold/templates/migration.rb
# Ignore for now
# lib/generators/custom_scaffold/templates/serializer.rb
# Ignore for now
# lib/generators/custom_scaffold/templates/fixture.yml
# Ignore for now
# lib/generators/custom_scaffold/templates/request_test.rb
# Ignore for now
# Gemfile
source 'https://rubygems.org'
ruby '2.4.1'
gem 'rails', '~> 5.1.6'
gem 'puma', '~> 3.7'
gem 'pg'
gem 'rack-cors', require: 'rack/cors'
gem 'olive_branch'
gem 'fast_jsonapi'
gem 'awesome_print'
gem 'byebug', '~> 10.0', groups: %i[development test]
gem 'yaml_db'
group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'mina', '~> 1.2', require: false
gem 'mina-puma', require: false
gem 'rubocop', require: false
gem 'annotate', require: false
end
You need to specify the file as a Thor template. Rails uses Thor templates for generating templates with ERB style code inside them.
Replace:
copy_file 'model.rb', "app/models/#{file_name}.rb"
With:
template 'model.rb.tt', "app/models/#{file_name}.rb"
By adding the .tt extension you're telling the generator to handle the file as a Thor template, which will interpret the Ruby code (ERB style) inside the file and then create a file with that same name minus the .tt extension. Any file you have without the .tt extension the generator will copy wholesale, without executing any of the code inside.
A useful tip: Sometimes you want to leave some ERB code inside a Thor template file without it being executed. By default any ERB style tags inside a .tt file will be process and in it's place a string will be written to the output file. You can avoid the processing of ERB tags but using a double percent sign in the tag.
For example, lets say you have a file named foo.erb.tt, which will create the file foo.erb when your generator runs. Let's also say we have a article_name variable and it's value is Breaking News
If you put <%= article_name %> in the file it will write Breaking News to the foo.erb.
If you put <%%= article_name %> (notice the %%) it will write <%= article_name %>to the foo.erb.
I found the following reference handy when learning this stuff.
Rails Application Templates article at Rails Guides.
Creating and Customizing Rails Generators & Templates article at Rails Guides.
Thor Actions Docs these are the commands used in our template.rb file.
Thor comes with several actions that help with script and generator tasks. You might be familiar with them since some came from Rails Templates. They are: say, ask, yes?, no?, add_file, remove_file, copy_file, template, directory, inside, run, inject_into_file and a couple more.
I have an engine, that has gem dependency. This gem has rake task to install migrations:
rake acts_as_taggable_on_engine:install:migrations
What is the proper way to install migration? When I run this command from host app or my engine I getting
Don't know how to build task
Add the gem dependency to you gemspec:
Gem::Specification.new do |s|
# ...
s.add_dependency 'acts-as-taggable-on', '~> 6.0'
# ...
end
Then require the gem in your engine:
# lib/my_engine/engine.rb
require 'acts-as-taggable-on'
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace Chatty
end
end
ActsAsTaggableOn should be loaded through the main file which requires the engine as well unlike some gems where you require gemname/engine - and the file naming is not snake_case like most gems.
Then run bundle install and rake acts_as_taggable_on_engine:install:migrations in the folder of the dummy application (or the host).
max#MaxBook ~/p/c/t/dummy> rake acts_as_taggable_on_engine:install:migrations
Copied migration 20181030123059_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123060_add_missing_unique_indices.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123061_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123062_add_missing_taggable_index.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123063_change_collation_for_tag_names.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123064_add_missing_indexes_on_taggings.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
I don't know why but invoking the command through bundler (bundle exec ...) does not work. This may give problems with RVM if you are using shims.
You can also create a generator for your engine that invokes the task:
# lib/generators/my_engine/install/install_generator.rb
module MyEngine
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)
desc "Installs MyEngine"
def copy_initializer
# template 'my_engine.rb', 'config/initializers/my_engine.rb'
rake "acts_as_taggable_on_engine:install:migrations"
end
end
end
Which you can then run with rails g my_engine:install.
class DomaincheckerController < ApplicationController
def index
end
def store
r =Whois.whois(secure_params['domain'])
render :text => "#{r}"
end
private
def secure_params
params.require(:whois).permit(:domain)
end
end
This is my domainchecker controller. The index method renders a form. After submitting the form it goes to store method. Here I am trying to use the whois gem. I have installed whois gem by running gem install whois. But I am getting this error.
uninitialized constant DomaincheckerController::Whois
The problem is that you installed the gem directly and not using bundler, therefore the Rails app can't find the dependency.
In order to install a gem in a Rails project you need to edit the Gemfile file and add the gem there. Once added, run
$ bundle
in order to install the dependency. Check the documentation about the Gemfile.
I'm new to rails and i've tried to set up a new project. I'm running Ubuntu 14.
These are my steps:
rails new my_project -T this created my new project.
rake db:create this created my database
Then i edited my Gemfile and deleted gem 'sqlite3'
group :production do
gem 'pg'
gem 'rails_12factor'
end
group :development do
gem 'sqlite3'
end
bundle install --without production
rails g controller welcome index about to create controllers
As last step i edited config.rb:
root to: 'welcome#index'
Now, when i try to go to localhost:3000/welcome/index i get the following error:
Why? It should display /welcome/index with the placeholder HTML.
This is my Welcome Controller:
class WelcomeController < ApplicationController
def index
end
def about
end
end
In application.css -
Remove :
*= require_tree .
and try. I am sure this problem appears because of css.