I'm getting this odd error, as if SimpleForm isn't even installed:
undefined method `simple_form_for'
SimpleForm IS in my gemfile and I've installed:
Using simple_form (2.0.2)
I'm running on the Pow server, so it's restarting every time.
#events_controller.rb
class admin::EventsController < ApplicationController
def new
#event = Event.new
end
#Event.rb
class Event
include Mongoid::Document
field :summary
field :start_date
field :end_date
end
#new.html.haml
= simple_form_for [:admin, #event] do |f|
= f.input :summary
= f.input :start_date
= f.input :end_date
Any ideas why it seems like SimpleForm isn't even installed?
Did you run the generator after you installed the gem? In your console you need to run this:
rails generate simple_form:install
I tried the command:
rails generate simple_form:install
And - it didn't run. Instead the output looked like I gave rails an ill-formed command line and it gave back a copy of the usage file.
Then, within RubyMine, I tried it again - this time got an error traceback - first line is shown below:
/home/user1/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/generators/base.rb:265:in `const_defined?': wrong constant name SimpleForm:installGenerator
After commenting out the lines 265-269 in base.rb:
# if last && last.const_defined?(last_name.camelize, false)
# raise Error, "The name '#{class_name}' is either already used in your application " <<
# "or reserved by Ruby on Rails. Please choose an alternative and run " <<
# "this generator again."
# end
and running the command again, I got:
/home/user1/.rvm/rubies/ruby-2.0.0-p247/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/user1/RubymineProjects/TravelAid/bin/rails generate generator simple_form:install -s
create lib/generators/simple_form:install
create lib/generators/simple_form:install/simple_form:install_generator.rb
create lib/generators/simple_form:install/USAGE
create lib/generators/simple_form:install/templates
Process finished with exit code 0
This looks pretty good, but I haven't gone back to check if simple_form works yet.
Under lib/generators, I have:
simple_form
templates
simple_form_generator.rb
USAGE
simple_form.install
templates
simple_form.install_generator.rb (created about 18 minutes ago - consistent with new)
USAGE
I am running Rails 4.0.0 and ruby-2.0.0-p247 within Rubymine
Related
I have plugin that takes attribute from post's front matter and uses it in permalink. Problem is I need to clean up any accents and diacritics from the string before putting it in to the permalink. Ruby on rails has method called parametrize which does exactly what I need but I have no idea how to use it in plugin.
This is plugins code I have:
module JekyllCustomPermalink
class CustomPermalink < Jekyll::Generator
safe true
priority :low
def generate(site)
# nothing to do, wait for hook
end
Jekyll::Hooks.register :documents, :pre_render do |doc|
begin
# check if jekyll can resolve the url template
doc.url
rescue NoMethodError => error
begin
if !doc.collection.metadata.fetch("custom_permalink_placeholders").is_a?(Array)
raise CustomPermalinkSetupError, "The custom placeholders need to be an array! Check the settings of your '#{doc.collection.label}' collection."
end
def doc.url_template
#custom_url_template ||= collection.metadata.fetch("custom_permalink_placeholders").inject(collection.url_template){|o,m| o.sub ":" + m, data[m].to_s.parameterize}
end
rescue KeyError
# "custom_permalink_placeholders"
raise CustomPermalinkSetupError, "No custom placeholders defined for the '#{doc.collection.label}' collection. Define an array of placeholders under the key 'custom_permalink_placeholders'. \nCaused by: " + error.to_s
end
end
end
end
end
but I get this error:
john#arch-thinkpad ~/P/blog (master)> bundle exec jekyll serve --trace
Configuration file: /home/john/Projects/lyricall/_config.yml
Source: /home/john/Projects/lyricall
Destination: /home/john/Projects/lyricall/_site
Incremental build: disabled. Enable with --incremental
Generating...
Jekyll Feed: Generating feed for posts
Liquid Exception: undefined method `parameterize' for "Žďořšťáčik":String in feed.xml
bundler: failed to load command: jekyll (/home/john/.gem/ruby/3.0.0/bin/jekyll)
/usr/lib/ruby/gems/3.0.0/gems/jekyll_custom_permalink-0.0.1/lib/jekyll_custom_permalink/custom_permalink.rb:20:in `block in url_template': undefined method `parameterize' for "Žďořšťáčik":String (NoMethodError)
What am I doing wrong ? How can I use this method which should be part of a string class but apparently it is not ? How can I achieve same result without ruby on rails framework ?
INFO:
jekyll 4.1.1
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]
Thank you for help
Rails additions to base Ruby classes, like String#parameterize, are part of the Active Support Core Extensions. The activesupport gem can be installed and used independent of Rails.
To keep the default footprint low, ActiveSupport allows you to require only the individual extensions you want to use. In your case, you will need to require the string inflection extensions:
require 'active_support/core_ext/string/inflections'
"Kurt Gödel".parameterize
=> "kurt-godel"
I am trying to get notifications to work in my app. I found "noticed gem" from this GitHub repo and followed all the steps that he does. I have the gem in my gem file, I did bundle install and update and rails db:migrate and everything. However when I try running this in rails console
CommentNotification.with(post: #post).deliver(current_user)
I get
Traceback (most recent call last):
1: from (irb):1
NameError (uninitialized constant CommentNotification)
This is my comment_notification.rb class that gets generated under app/notifications/comment_notificaiton.rb when I run rails generate noticed:notification CommentNotification just as he does in the video and just as the documentation suggests.
# To deliver this notification:
#
CommentNotification.with(post: #post).deliver_later(current_user)
CommentNotification.with(post: #post).deliver(current_user)
class CommentNotification < Noticed::Base
# Add your delivery methods
#
deliver_by :database
# deliver_by :email, mailer: "UserMailer"
# deliver_by :slack
# deliver_by :custom, class: "MyDeliveryMethod"
# Add required params
#
param :post
# Define helper methods to make rendering easier.
#
def message
t(".message")
end
#
def url
post_path(params[:post])
end
end
You have to restart your spring server.
Using bin/spring stop command, the spring server will be stopped. Then the server will be started using rails server or rails s.
A bit late to this, but manually loading the notification class in rails console solved this issue in my case, i.e.: load "app/notifications/comment_notification.rb". (PS: I would also check the spelling of the file name, i.e. comment_notification vs comment_notificaiton)
Uncomment the first two lines:
# CommentNotification.with(post: #post).deliver_later(current_user)
# CommentNotification.with(post: #post).deliver(current_user)
remember, #post must be the resource that you are going to store in user notifications
user.notifications
#post must exist
I am using ActionText to edit a paragraph and it works perfectly locally but when I deploy it to Heroku the page which has the form with rich_text_area it throws an error saying undefined method rich_text_area_tag even though I followed the rails guide. I thought I needed to configure Active Storage on production but that's not the case.
Here is what I am getting in Heroku's logs:
ActionView::Template::Error (undefined method 'rich_text_area_tag' for #<#<Class> Did you mean? rich_text_area)
<%= f.label :something, class:'label' %>
<%= f.rich_text_area :something %>
I found this online, and it was helpful for me:
https://github.com/JoeWoodward/spree_helper_issue
I am not sure if this is the correct way to do it, but it's a temporary workaround.
SOLUTION:
In application_controller.rb enter the following:
require 'action_text'
class ApplicationController < ActionController::Base
helper ActionText::Engine.helpers
...
end
Docs for helper: https://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper
First of all make sure that you have this line in config/application.rb:
require 'rails/all'
If it's a rails 6 app action_text/engine should be loaded.
If it doesn't it's possible taht zeitwerk is not loaded. This happens when you have a rails 5.2 app that was updated to rails 6.
In the same file (config/application.rb) you have to change config.load_defaults to 6.0:
config.load_defaults 6.0
If you want to know what happens in the background take a look at this link on line 125.
To avoid cluttering the ApplicationController code, you can use an initializer:
i.e. add a new file config/initializers/action_text.rb:
ActiveSupport.on_load(:action_view) do
include ActionText::ContentHelper
include ActionText::TagHelper
end
Inspired by p8's comment in a closed Rails issue.
In my Rails project, I saw the link_to is overridden in config/initializers/extend_action_view.rb
module ActionView
module Helpers
def link_to(name, options = {}, html_options = nil)
...
end
end
end
I found that with the famous command line in linux grep -r 'def link_to' *.
My question is : Is there a way to find out it in rails console ? Is there a native function rails or ruby can give us the path of file ? Something like .ancestors for a object.
ps: My IDE is vi
If you use the pry gem, you can find it by using Method#source_location.
Pass binding.pry in your view and render it. Then write:
method(:link_to).source_location
=> ["path_to_helper.rb", 124]
Use $ command in pry gem
Insert binding.pry in your code, which sets a breakpoint and then in the Pry commandline use the $ command
[1] pry(#<AdminController>)> $ Person.find
From: /Users/joe_example/.rvm/gems/ruby-1.9.3-p551/gems/composite_primary_keys-8.1.0/lib/composite_primary_keys/core.rb # line 21:
Owner: ActiveRecord::Core::ClassMethods
Visibility: public
Number of lines: 38
def find(*ids) # :nodoc:
# We don't have cache keys for this stuff yet
return super unless ids.length == 1
...
The $ command is literally worth dollars. It shows you where a method is defined as well as the source code.
I attempted to install the gmaps4rails gem.
I added gem 'gmaps4rails' to my Gemfile, and ran 'bundle install. It said that my bundle installed successfully. I can find "Using gmaps4rails (0.8.8)" with 'gem list'. I added the specified columns to my users table with rake db:migrate and added acts_as_gmappable and the gmaps4rails_address method to my User model.
Visiting pages that involve the user model gives me "undefined local variable or method 'acts_as_gmappable'"error.
Is there something that I am missing?
For greater context, the code I am using is from the Rails 3 Tutorial.
OS X 10.6.6
ruby 1.8.7
rails 3.0.7
passenger 3.0.7
Restarting the server should resolve the problem :)
I had the same issue : undefined local variable or method 'acts_as_gmappable'
I just restarted the server and it error went away.
I was using Mongoid and had the same trouble.
In which case, make sure your model includes:
include Gmaps4rails::ActsAsGmappable
acts_as_gmappable :position => :location
field :gmaps, :type => Boolean
field :location, :type => Array
def gmaps4rails_address
#describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
"#{self.address}, #{self.city}, #{self.country}"
end
I think this may be helpful (similar issue):
rvm + rails3 + gmaps4rails - acts_as_gmappable