undefined method `simple_form_for' in RoR application - ruby-on-rails

I've been browsing stack overflow and I noticed that many people use simple_form to make their lives easier.
I wanted to give it a try, so I added the gem to my gem file (gem 'simple_form') and sent:
rails generate simple_form:install --bootstrap
After reading the installation message, I created a view with the sample code from the readme:
<%= simple_form_for #user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
Unfortunately, I get this error as soon as I try to load the page:
undefined method `simple_form_for' for #<#<Class:0x69dd688>:0x6903ac8>
What gives? How come it can't even recognize the method? I feel like I'm missing something really simply here. Could it be because i'm using bootstrap-sass? Do I need to include a helper in my user controller?

Did you restart your server?
Always keep in mind that after you install a gem or plugin.
Always restart your server

Its all because you didn't restart your server after installing new libraries such as simple_form, devise etc.
Try stop your server and restart it. Your code is perfect!. I encountered this several times. The solution is just restarting the server!.

You must restart your server after installing simple_form gem.

For any absolute beginners. This error will occur if you forgot to install simple_form. To rectify, add this to your Gemfile
gem 'simple_form'
and bundle install

To summarize all possibilities from above, hopefully this list is useful for other gems too.
Check if the simple_form gem is installed correctly
Find the gem in Gemfile and Gemfile.lock. Another way is to run this command in your terminal: bundle list | grep simple_form
If the output is empty
Possibly simple_form isn't installed in your project yet. Since the gem is an external gem, it isn't included in the Rails framework. Rails has its own form library (aka form_with and form_for).
To add simple_form to your project, open the Gemfile and add gem 'simple_form', then run the command in your terminal bundle install.
If the output isn't empty
Check if you have restart the server yet.
After restart the server, and you still not be able to access SimpleForm constant
Check if the gem is set to require: false in Gemfile. If so, remove this part and run bundle install.
You can check if the gem is loaded from rails console by typing rails console or rails c in your terminal.
After the console is loaded, type SimpleForm and hit Enter. In the case there is some errors complaining about the constant is undefined, refer to previous steps to fix.

Related

Rails 5: record_tag_helper continues to give an error

In my project with content_tag_for I had an error:
The content_tag_for method has been removed from Rails. To continue
using it, add the record_tag_helper gem to your Gemfile: gem
'record_tag_helper', '~> 1.0'
So, I added this gem to my Gemfile. $ bundle install returns:
Using record_tag_helper 1.0.0
I have restarted server, cleared Rails cache and so on, but I still have the same error, that content_tag_for method has been removed from Rails. I just have no more ideas why it doesn't work.
Share your ideas, please.
Looking at the Github page, the gem has not been updated in 2 years. Also the last update contains the message 'Prep for Rails 5'. I'm assuming that this gem isn't compatible with Rails 5.
I have added "require 'record_tag_helper'" to my application.rb file and now it works.

how do you use react-rails react_component with webpacker gem rails 5.1?

Newbie here, I've recently implemented the webpacker gem in my react-rails project. Being new to webpack and react, this gem makes a lot of sense to me coming from a rails background.
i'm wondering if it were possible to use the react-rails gem in my rails webpacker project so we could still use the view helper method like this:
<%= react_component('MyComponent', #controller_value.to_json)%>
if not, is there a reason why?
Thanks for any input! :)
First you need to install node- I used nvm,
and install yarn- link.
Then, add these lines to your Gemfile:
gem 'webpacker', '~> 2.0'
gem 'react-rails'
then run
bundle install
rails webpacker:install
rails webpacker:install:react
rails generate react:install
This will create the necessary files in app/javascript (not app/assests/javascripts) and config files.
Now add <%= javascript_pack_tag 'application' %> to the appropriate layout file, like app/views/layouts/application.html.erb
Finally you will need to run ./bin/webpack-dev-server in a separate terminal, along with rails server.
That's it. Now you can use
<%= react_component('MyComponent', #controller_value) %>
Here MyComponent will be defined in app/javascript/components/MyComponent.js
You can use another gem for this
--> #react_on_rails (5.2.0)
$ bundle show react_on_rails
--> #- /Users/mark/.rvm/gems/ruby-2.1.2/gems/react_on_rails-5.2.0
Isn't the helper supposed to be included something like this?:
See http://pothibo.com/2013/06/customize-rails-view-controller-with-railtie/
and https://github.com/reactjs/react-rails/blob/master/lib/react/rails/railtie.rb#L42-L44
Hear is the example of gem react_on_rails and gem react-rails
https://learnetto.com/blog/rails-data-react-component-webpacker

Rails 4.2.0 beta4 - Haml with no output

I have installed haml gem in my rails app (v4.2.0 beta4)
but it doesn't seem to compile any output.
I have included gem in my gemfile and ran bundle install and made sure
that I changed my html.erb files to html.haml
This is one of the template and all I see is the heading "New recipe".
%h1 New recipe
= render 'form'
= link_to 'Back', recipes_path
And nothing gets displayed for form partial.
I've come across someone with similar issue and the solution to the issue was making controller inherit
applicationController rather than actionController.
I wonder if this is what I need to do to make it working.
I would have thought installing haml gem would have handled everything needed to get it working.
In your Gemfile, put
gem "haml-rails"
And then of course bundle install
This is the ONLY gem you need to integrate HAML into your Rails app, as it provides the wrappers needed to be able to use ruby logic in your haml views. Simply using haml alone is not sufficient as haml is framework independent.
Since you've added the HAML gem (gem 'haml', '~> 4.0.5'), ran bundle install and restarted your server (Have you restarted your server??) I believe you have installed the Gem correctly.
When you say in your template file you see "New recipe", it makes me think the issue is not with the haml gem but with partial file.
Just to confirm, your partial is saved within the same folder (app/views/recipes/) as:
_form.html.haml
You need to make sure you have the underscore in front of the file name for partials.
I ended up downgrading my project to 4.1.8 and Haml works just fine.
gem 'haml-rails' was not necessary.
gem 'haml' did the job just fine.

NameError in PostsController#pingback

This is my first time trying to get a Gem to work in rails where I haven't just been able to follow the documentation.
I first installed the gem using sudo gem install ping back and then added it to my Gemfile via gem 'pingback'. I then ran bundle install and it shows it installed in the list it outputs.
So then I wrong a little function that looks like this and is in my posts controller:
def send_trackback(posts)
posts.each do |post|
source_uri = "http://example.com/posts/#{post.slug_url}"
target_uri = post.target_url
Pingback::Client.new.ping(source_uri, target_uri)
end
end
whenever I try to load the admin page that sends the trackbacks I get the following:
NameError in PostsController#pingback
uninitialized constant PostsController::Pingback
Do I have to do more than just install the gem via bundler and then plug and play?
Update
adding require 'pingback' to the top of my posts controller results in this:
cannot load such file -- pingback
The error message to me indicates that the VM is trying to find PingBack in PostsController, I am thinking you are missing a require or include statement for PingBack.
It may be a typo, but pingback needs to be one word, not 'ping back' for the line in the gemfile, and for the gem install.
I would try running 'bundle list' to make sure the gem is installed.
I restarted the rails server and I believe this has resolved this issue.

Rails 2.3.2 trying to render ERB instead of HAML

Rails is suddenly trying to render ERB instead of Haml and I can't figure out why. I've created new rails projects, reinstalled Haml, and reinstalled Rails.
Here's exactly the steps I take when making my application (Rails 2.3.2):
rails> rails test
rails> cd test
rails\test> haml --rails .
rails\test> ruby script\generate model user email:string password:string
rails\test> ruby script\generate controller users index
rails\test> rake db:migrate
Here's what the UsersController looks like:
class UsersController < ApplicationController
def index
#users = User.all
end
end
My routes:
ActionController::Routing::Routes.draw do |map|
map.resources :users
end
I now create views\users\index.html.haml:
%table
%th(style="text-align: left;")
%h1 Users
- for user in #users
%tr
%td= user.email
%td= user.password
Annnd run the server...
I navigate to localhost:3000\users and I get this error message:
Template is missing
Missing template users/index.erb in view path app/views
For some reason Rails is trying to find and render .erb files instead of .haml files.
vendor\plugins\haml\init.rb exists, untouched.
I've reinstalled Haml (Pretty Penny) multiple times and still get the same results.
I've also tried adding config.gem 'haml' to my environment.rb but this also doesn't work.
I can't figure out why suddenly rails will not render haml for me.
Hi it seem like haml is not enabled as Rails plugin ,in order to enable it use the following command .
Go to your application folder from the command prompt type the following
$ cd ..
$ haml --rails <yourproject>
if this doesnot work try installing haml gem with the following code
$ gem install haml
I tried with above example , it did work for me ,i have haml gem installed in my ubuntu system .
Good luck !
NOTE: "haml --rails" was deprecated in HAML 3.1
It's worth noting that the fact that the error message says that it couldn't find index.erb doesn't mean that it didn't look for index.haml too. The erb extension is hard-coded into the error message.
I thought that I had the same problem you describe, but it turned out that my application simply couldn't find my partial at all - it had nothing to do with the file extension.
I had this same problem (see this post) with Rails 2.3.4. Multiple gem uninstall/gem install rails didn't fix the problem. But downgrading to Rails 2.3.2 worked! (I know HAML previously worked in this project with this version of Rails).
sudo gem install -v 2.3.2 rails
Using Rails 3.1, I ran in to the same error and had to restart the web server.
I have this old project in rails 2.3.18, where the gems are managed using bundler, and all I had to do was explicitly use version 3.1.3 It did not work with the latest version (4.0). So in my version I added
gem 'haml', '3.1.3'
did bundle install and restarted my development server :)
Hmmm strange, this might be related.
According to: http://www.ruby-forum.com/topic/101346 you should use resource_url helpers in controllers and resource_path helpers in views. Right?
BUT, if I do use a resource_url helper in a redirect_to call inside my controller, then I get:
Missing template htp://localhost:4000/categories/new.erb in view path app/views
If I use the resource_path helper instead, there aren't any problems at all.
Anyone knows what could be wrong?
Why is the resource_url helper trying to redirect to an .erb file?
This is the error from the server log:
ActionView::MissingTemplate (Missing template http://localhost:4000/categories/new.erb in view path app/views):
haml (2.2.2) lib/haml/helpers/action_view_mods.rb:13:in `render'
app/controllers/categories_controller.rb:15:in `create'
haml (2.2.2) rails/./lib/sass/plugin/rails.rb:19:in `process'
P.S. This is in Rails 2.3.3
maybe your file name is wrong, if you have a whitespace in the end of the index.html.haml_, rails will wrong...
I ran into the same problem and I had to restart my server after installing Haml before my rails app recognized the changes.
I was having this issue with Ruby 1.9x, Rails 2.3.5, and HAML 3.1. I believe part of the issue is that some of the deprecated calls in 1.8 were removed in 1.9.
IMHO, if you want to use HAML in Rails 2, you'd be better off downgrading to Ruby 1.8. (which is what I did to fix my problem). In Rails 2, you MUST have the gem.config "haml" in your config.
Even better, move forward to Rails 3 on Ruby 1.9!
I've had this same problem.
The solution is documented in https://github.com/haml/haml/issues/672
You need to add
config.after_initialize do
require 'haml'
Haml.init_rails(binding)
end
inside your config/environment.rb within the Rails::Initializer.run do |config| configuration block !!
Updated - Not actual anymore:
I name all of my haml file only .haml
To illustrate:
test.haml
# not
test.html.haml
Update 5 years later:
I recommend to name them "file.format.haml", because its much more clear which format is the outcome...

Resources