Change the method name destroy to delete in controller.rb in Ruby on rails,
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "deba", password: "12345", except: [:index, :show]
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
@article.save
redirect_to @article
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render 'edit'
end
end
def delete
@article = Article.find(params[:id])
@article.delete
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
I have tried to do like this, but it's not deleting the item. If i change the delete to destroy it is working fine but I have to change the default method name
2 answers
-
answered 2022-05-04 10:28
Deepesh
Not sure which default method name you are pointing to but the default name for the controller action is
destroy
and notdelete
. If you wish to change the name of the action you can add a new route that takes the user to the delete action and use it instead of the default destroy action.Inside the action, you are trying to do
@article.delete
which is valid but would suggest always usingdestroy
instead as thedelete
method will just run the SQL DELETE statement and run no callbacks.destroy
will always run the callbacks.More information here:
Difference between Destroy and Delete
Why the Ruby on Rails action "destroy" is not named "delete"?
-
answered 2022-05-05 08:15
max
If you REALLY want to route
DELETE /articles/:id
to the delete method in your controller you can do it by customizing your routes:Rails.application.routes.draw do resources :articles, except: :destroy do delete '/', on: :member, action: :delete # but why? end end
You could also just alias the method instead of modifying your routes:
class ArticlesController < ApplicationController # ... def delete @article = Article.find(params[:id]) @article.delete # you should be using destroy redirect_to articles_path end alias delete destroy # ... end
However this is utterly pointless. Follow the conventions and spend your time being productive and writing maintainable code instead.
do you know?
how many words do you know
See also questions close to this topic
-
problems to run rails s on Rails 4 :S
Im trying to run s on my project in Rails 4.2.2
but i get a unexpected error :S
Someone know the reason of the error?
Thanks in advance This is my logs .................................................................................................................................................................................................➜ backend-ror git:(dev) ✗ rails s => Booting Puma => Rails 4.2.2 application starting in development on http://localhost:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server Expected string default value for '--jbuilder'; got true (boolean) Expected string default value for '--serializer'; got true (boolean) Exiting /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `load': /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/app/controllers/application_controller.rb:39: syntax error, unexpected tIDENTIFIER, expecting keyword_then or ',' or ';' or '\n' (SyntaxError) from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `block in load_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:647:in `new_constants_in' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:456:in `load_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:354:in `require_or_load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:494:in `load_missing_constant' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:184:in `const_missing' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/inherited_resources-1.9.0/app/controllers/inherited_resources/base.rb:11:in `<module:InheritedResources>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/inherited_resources-1.9.0/app/controllers/inherited_resources/base.rb:1:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:457:in `block in load_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:647:in `new_constants_in' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:456:in `load_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:354:in `require_or_load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:494:in `load_missing_constant' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:184:in `const_missing' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/base_controller/authorization.rb:2:in `<module:ActiveAdmin>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/base_controller/authorization.rb:1:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/base_controller.rb:1:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/application.rb:166:in `controllers_for_filters' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin/application.rb:159:in `block (2 levels) in <class:Application>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config/initializers/active_admin.rb:12:in `block in <top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activeadmin-1.4.3/lib/active_admin.rb:69:in `setup' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config/initializers/active_admin.rb:1:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:652:in `block in load_config_initializer' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/notifications.rb:166:in `instrument' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:651:in `load_config_initializer' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:615:in `each' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/engine.rb:615:in `block in <class:Engine>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:30:in `instance_exec' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:30:in `run' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:55:in `block in run_initializers' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:421:in `block in each_strongly_connected_component_from' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:44:in `each' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:44:in `tsort_each_child' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:415:in `call' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:415:in `each_strongly_connected_component_from' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/initializable.rb:54:in `run_initializers' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/application.rb:352:in `initialize!' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config/environment.rb:5:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `block in require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:274:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config.ru:3:in `block in <main>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:55:in `instance_eval' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:55:in `initialize' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config.ru:in `new' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/config.ru:in `<main>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:49:in `eval' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:49:in `new_from_string' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/builder.rb:40:in `parse_file' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/server.rb:300:in `build_app_and_options_from_config' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/server.rb:209:in `app' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/server.rb:61:in `app' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/rack-1.6.13/lib/rack/server.rb:337:in `wrapped_app' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/server.rb:139:in `log_to_stdout' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/server.rb:78:in `start' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:80:in `block in server' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:75:in `tap' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:75:in `server' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands/commands_tasks.rb:39:in `run_command!' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/railties-4.2.2/lib/rails/commands.rb:17:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/bin/rails:9:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/bin/rails:9:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/client/rails.rb:28:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/client/rails.rb:28:in `call' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/client/command.rb:7:in `call' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/client.rb:28:in `run' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/bin/spring:49:in `<top (required)>' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/binstub.rb:11:in `load' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/vendor/cache/ruby/2.3.0/gems/spring-1.5.0/lib/spring/binstub.rb:11:in `<top (required)>' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Users/Cristopheer96/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /Users/Cristopheer96/code/Cristopheer96/Guudjob/backend-ror/bin/spring:13:in `<top (required)>' from ./bin/rails:3:in `load' from ./bin/rails:3:in `<main>' ➜ backend-ror git:(dev) ✗
-
Devise Registration controller, every parameter seems to be removed
trying to override Devise Registration controller, following a couple examples online, but for some reason every parameter passed in seems to be removed. this is the controller.
class RegistrationsController < Devise::RegistrationsController skip_before_action :verify_authenticity_token, :only => :create def create p sign_up_params build_resource(sign_up_params) resource.save sign_up(resource_name, resource) if resource.persisted? render json: resource end end
this is the request
curl --location --request POST 'localhost:3000/buyer_users' \ --data-raw '{ "buyer_users": { "email":"peter245@email.com", "password":"123123123", "site_id":"103eb514-8185-4820-8f16-3a9d082c5f60", "account_id": "f362189f-a843-4c19-927a-845ce2cb0ad0" } }'
we seems to be picking up params, but everything is removed
21:16:05 web.1 | Started POST "/buyer_users" for ::1 at 2022-05-06 21:16:05 -0700 21:16:05 web.1 | Processing by RegistrationsController#create as JSON 21:16:05 web.1 | Parameters: {"{\n \"buyer_users\": {\n \"email\":\"peter245@email.com\",\n \"password\":\"123123123\",\n \"site_id\":\"103eb514-8185-4820-8f16-3a9d082c5f60\",\n \"account_id\": \"f362189f-a843-4c19-927a-845ce2cb0ad0\"\n }\n}"=>"[FILTERED]"} 21:16:05 web.1 | {} 21:16:05 web.1 | Completed 200 OK in 7ms (Views: 0.1ms | ActiveRecord: 0.0ms | Allocations: 1155)
routes are set up like this
devise_for :buyer_users, defaults: { format: :json }, controllers: { registrations: 'registrations' }
EDIT: after debugging some more it seems that the entire parameter is taken in as 1 string.
-
Whats missing on my Ruby 'Inverse Of' relationship
I know this topic has been addressed, but I have been at this for 2 days and I'm just stuck. I know inverse of does not create a new query, so should I use another method?
Question: How to set up an 'inverse of' with a has_one, belongs_to situation & same class..
Explanation: A user 'has_one :spouse' and 'belongs_to :spouse_from'. They are inverse of each other. When a User signs up, they can invite their significant other. For Example
- user_a invites & creates user_b
- user_b.spouse_id is set to user_a.id
- In a separate method I want to be able to update like.. user_a.spouse_id = user_a.spouse.id
The only association that works at this point is user_b.spouse.
Class User has_one :spouse, class_name: 'User', foreign_key: :spouse_id, dependent: :nullify, inverse_of: :spouse_from belongs_to :spouse_from, class_name: 'User', foreign_key: :spouse_id, inverse_of: :spouse, optional: true
-
Library functions in Inspec Test
Team --
I have a ruby library helper in which I have defined multiple functions/methods. How do I reference those in
Chef
Inspec
tests?def interface_name # some code with logic end
In my specific use case, I am checking to see if a custom networking device was specified as a JSON parameter, and if it was, I am validating it make sure its real (incase they misspelled it) and also gathering its IP and state and other data as reported by
Ohai
This is what I have so far, but I'm not sure if this is correct
describe file('/path/to/custom_attributes.json') do it { should exist } unless json('/path/to/custom_attributes.json').empty? do its(['networking']['interface_name']) { should_not be_empty } interface_name = file(json('/path/to/custom_attributes.json').params['networking']['interface_name']) end end describe file('/etc/NetworkManager/system-connections/wired_connection') do unless interface_name.empty? its('content') { should_not match(/^interface-name=wl.*/mx) } end its('content') { should match(%r{ca-cert=/etc/ssl/certs/ca-certificates\.crt}mx) } its('content') { should match(/id=\.corp.*type=ethernet.*autoconnect-priority=100.*dns-search=corp\.domain.com.*/mx) } end end
The problem / question is that if I gather the parameter directly from the JSON file, then I am bypassing all the validation logic that I'm doing in the library, which defeats the purpose. So, how do I get access to that library function/method in the Inspec test?
For reference, here is the function:
def interface_name file = '/path/to/custom_attributes.json' if File.exist?(file) && !File.stat(file).zero? attributes = JSON.parse(File.read(file)) device_name = attributes['networking']['interface_name'] if device_name && !device_name.empty? && networking_devices.include?(device_name) interface = device_name Chef::Log.info("Valid custom interface provided, using \"#{device_name}\".") else Chef::Log.debug("Invalid interface (\"#{device_name}\") provided. Valid options are: \"#{networking_devices.keys}\"") interface = nil end else Chef::Log.debug('No custom interface provided.') end interface rescue JSON::ParserError nil end
-
Writing structured facts
I've wrote some ruby code which will run on a linux server and return details about the server as a fact. It does this by connecting to amazon and retrieving some json (it runs two separate commands one to retrieve a list of disks - e.g /dev/sda1, /dev/xvdb and then it maps this to a volumeID via another query).
I've made some small amendments to the output and added some values I'm interested in. The code runs multiple times and returns multiple hashes (one for each disk - maybe I should merge them?). Anyway here's an example of a server which has two disks below (this is just some debug output):
Hash is {"/dev/sda1"=>{"disk_mapping"=>"", "is_lvm_partitioned"=>"false", "volumeid"=>"vol1234"}}. Hash is {"/dev/xvdb1"=>{"disk_mapping"=>"xvdb1", "is_lvm_partitioned"=>"true", "volumeid"=>"vol5678"}}.
The next thing I want to to is turn this into a structured fact (with the devices: /dev/sda1, /dev/xvdb1 as the "keys"). Here's a rough idea of how I've done it (I've skipped a lot of the irrelevant code).
json_string = { "#{path}" => { "disk_mapping" => "#{disk_mapping}", "is_lvm_partitioned" => "#{is_lvm_partitioned}", "volumeid" => "#{getVolumes}" } }.to_json hash = JSON.parse(json_string) if hash.is_a? Hash then debug_msg("Hash is #{hash}.") hash["#{path}"].each do |key, child| debug_msg("Setting key: #{key} child: #{child}.") end end
I've never really wrote any ruby before so this is copied from multiple places, but besides aggregated facts I can't find a way to do this; I've tried to do something like this:
Facter.add(:test["#{path}"]["#{key}"]) do setcode do #{child} end end
So I guess in order I want to know:
- Should I merge the hash somehow? I originally assumed I did but found this incredibly hard due to not knowing how many hash's I'd have.
- Should I be using an aggregated fact or a "standard" one?
- How do I retain the structure of the hash and then call it with a single query (e.g facts test).
- Any examples which are similar to my code (the puppet ones I've found quite hard to follow).
Here's what I'm looking for at the end:
[root@blah ~]# facter test { /dev/sda1 => { disk_mapping => "", is_lvm_partitioned => "false", volumeid => "vol1234" }, /dev/xvdb => { disk_mapping => "xvdb1", is_lvm_partitioned => "true", volumeid => "vol5678" } }
Thanks.