I am very new at Ruby on Rails, and I am not quite sure what this error means:
uninitialized constant StorevaluesController
I have a storevalue_controller.rb which works (I can get to content on the page) but when I try to submit a form like this:
new.html.erb
<h1>Fill out form to add to db</h1>
<%= form_for :storevalue, url: storevalue_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
rails throws the error I reference above.
storevalue_controller.rb
class StorevalueController < ApplicationController
def new
end
def create
#storevalue = Storevalue.new(storevalue_params)
#storevalue.save
redirect_to #storevalue
end
def show
#storevalue = Storevalue.find(params[:id])
end
private
def storevalue_params
params.require(:storevalue).permit(:title, :text)
end
end
my route trace:
welcome_index_path GET /welcome/index(.:format) welcome#index
root_path GET / welcome#index
storevalue_new_path GET /storevalue/new(.:format) storevalue#new
storevalue_path POST /storevalue(.:format) storevalues#create
new_storevalue_path GET /storevalue/new(.:format) storevalues#new
edit_storevalue_path GET /storevalue/edit(.:format) storevalues#edit
GET /storevalue(.:format) storevalues#show
PATCH /storevalue(.:format) storevalues#update
PUT /storevalue(.:format) storevalues#update
DELETE /storevalue(.:format) storevalues#destroy
Note the error is plural "values" and your actual controller name is not. You're using the plural name somewhere when it doesn't exist. In Ruby, class names are constants. Hence you have the error message wording, which I agree is pretty misleading at face value.
Related
I have a UsersController and it has below code
def sign_up
#user = User.new
end
And my view page has
<div class="col-md-6 col-md-offset-3">
<%= form_for #user do |f| %>
<%= f.label :first_name%>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.submit "Register", class: 'btn btn-primary'%>
<% end %>
</div>
My routes.rb file contains the following entry
get 'signup' => 'users#sign_up'
But When I submit the form, it says
ActionView::Template::Error (undefined method `users_path' for #<#<Class:0x00000004d91490>:0x00000004d90220>)
Why does this throw an error and do I need to explicity point to url in the form_for?? Why is it so??
Change your routes to:
resources :users, only: [:new, :create], path_names: {new: 'sign_up'}
and rename your sign_up action back to new. The reason you are getting the error is rails trying to guess the correct url for given resource. Since you have passed #user, which is an instance of User class, it will try to call "#{#user.class.model_name.route_key}_path key, which results in the error you got.
To solve the issue you need either make your routes to define users_path or you need to specify the url directly using url option. users_path can be defined by either index or create action, so the above solution will work (and will not create remaining CRUD routes, yey!)
Try changing this line:
<%= form_for #user do |f| %>
to this:
<%= form_for(#user, url: 'signup') do |f| %>
I am new to Rails, and just working my way through my first solo project, but I seem to be running into a syntax error with a constant not being initialized (Ive gotten several of these, but each seems to have a different cause.....not quite sure how i keep getting the same error with different causes :)):
uninitialized constant DatastringsController::Datastrings
DatastringsController:
class DatastringsController < ApplicationController
def new
end
def create
#datastrings = Datastrings.new(datastrings_params) #ERROR returned on this line
#datastrings.save
redirect_to #datastrings
end
def show
#datastrings = Datastrings.find(params[:id])
end
private
def datastrings_params
params.require(:datastrings).permit(:title, :text)
end
end
I believe my form is correct:
<%= form_for :datastrings, url: datastrings_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
The main problem you have is here:
<%= form_for :datastrings, url: datastrings_path do |f| %>
form_for really should be populated with an ActiveRecord object, as this allows Rails to build the relative paths it requires correctly.
Although I don't know why this is the case, your current setup is basically trying to render DatastringsController::Datastrings -- primarily because you've not set up your form_for correctly
--
Fix
If you want to create a datastring object, I'd follow convention and do this:
#config/routes.rb
resources :datastrings
#app/controllers/datastrings_controller.rb
Class DatastringsController < ApplicationController
def new
#datastring = Datastring.new
end
def create
#datastring = Datastring.new(datastring_params)
#datastring.save
end
private
def datastring_params
params.require(:datastring).permit(:title, :text)
end
end
#app/views/datastrings/new.html.erb
<%= form_for #datastring do |f| %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
--
YOU ALSO NEED TO NAME YOUR MODELS IN SINGULAR
Looking at it now, it seems that your main issue is likely that you've named your model as a plural.
The reason this will be bad is that when you load Rails, it will load all your models, and consequently, allow you to call them by referencing their class name. If a model is plural, I don't think it will load it correctly, causing the error you've highlighted
If you name your model to the following, it should be better:
#app/models/datastring.rb
Class Datastring < ActiveRecord::Base
end
I'm building an app where members can create a trip and I'm using the devise gem for authorization.
When a member logs in they are redirected to the trip new page where there is a form. For some reason, I can't get the form syntax just right and I'm running into an error: undefined method `member_trips_path'
This is the trip new form
<%= form_for [current_member, #trip] do |f| %>
<%= f.label :where, "Where?" %><br/>
<%= f.text_field :where, placeholder: "Hawaii" %><br>
<%= f.label :when, "When?" %><br/>
<%= f.text_field :when, placeholder: "#" %><br>
<%= f.label :price_per_person, "Price per person? (Approximately)" %><br/>
<%= f.text_field :price_per_person, placeholder: "$550" %><br>
<%= f.submit "Create Trip Idea"%>
<% end %>
current member is equivalent to #member (this is what the devise gem does)
rake routes
member_trip_index GET /members/:member_id/trip(.:format) trip#index
POST /members/:member_id/trip(.:format) trip#create
new_member_trip GET /members/:member_id/trip/new(.:format) trip#new
edit_member_trip GET /members/:member_id/trip/:id/edit(.:format) trip#edit
member_trip GET /members/:member_id/trip/:id(.:format) trip#show
PATCH /members/:member_id/trip/:id(.:format) trip#update
PUT /members/:member_id/trip/:id(.:format) trip#update
DELETE /members/:member_id/trip/:id(.:format) trip#destroy
Trip controller
class TripController < ApplicationController
def index
end
def new
#trip = Trip.new
end
def create
end
end
Your routing is wrong - I think you have defined something like resources :trip when it should be resources :trips. Mixing singulars and plurals in routing is dangerous and leads to odd errors.
I have a form in Rails
<div class="page-header">
<h3>Create Blah</h3>
</div>
<%= simple_form_for #blah do |f| %>
<%= f.input :id %>
<%= f.input :name %>
<%= f.input :pho %>
<%= f.input :fun %>
<%= f.submit :class => 'btn btn-primary' %>
<% end %>
<br>
When I click the submit button, where does the code attempt to go? Does it call the create method for blah_controller.rb? Because currently, I get a routing error
Routing Error
uninitialized constant BlahsController
Here is the BlahController#create method:
def create
authorize! :create, :blahs
#blah = Blah.new(params[:blah])
if #blah.save
redirect_to admin_blah_path(#blah), :notice => 'New blah created!'
else
render :new
end
end
In my rake routes, I have
admin_blahs GET /admin/blahs(.:format) admin/blahs#index
POST /admin/blahs(.:format) admin/blahs#create
new_admin_blah GET /admin/blahs/new(.:format) admin/blahs#new
edit_admin_blah GET /admin/blahs/:id/edit(.:format) admin/blahs#edit
admin_blah GET /admin/blahs/:id(.:format) admin/blahs#show
PUT /admin/blahs/:id(.:format) admin/blahs#update
DELETE /admin/blahs/:id(.:format) admin/blahs#destroy
It looks like your BlahsController is a namespaced controller, living under the Admin module (i.e., its fully-qualified name is Admin::BlahsController). If so, when constructing forms you must also provide the :admin namespace, using something like the following:
<%= simple_form_for [:admin, #blah] do |f| %>
See the Rails Guide to Form Helpers, under the "Dealing with Namespaces" section.
So I've been banging my head against the wall trying to figure out why this isn't working. I keep getting
ActionView::Template::Error:
undefined method `admin_information_index_path' for #<#<Class:0x007fc67971cab8>:0x007fc67d775740>
With the trace:
# ./app/views/admin/informations/_form.html.erb:1:in `_app_views_admin_informations__form_html_erb__2815737811953353352_70245242566200'
# ./app/views/admin/informations/new.html.erb:2:in `_app_views_admin_informations_new_html_erb___3700624853666844924_70245242606040'
Any tips in the right direction?
My routes:
namespace :admin do
resources :informations
end
My controller:
class Admin::InformationsController < Admin::AdminController
def new
#information = Information.new
end
end
views/admin/informations/new.html.erb:
<h1>Add New Information Page</h1>
<%= render :partial => 'form', locals: { information: #information } %>
views/admin/informations/_form.html.erb:
<%= form_for [:admin, information] do |f| %>
<%= error_messages_for information %>
<%= f.label :title %><br>
<%= f.text_field :title %><br><br>
<%= f.label :content %><br>
<%= f.text_area :content %><br><br>
<%= f.submit "Submit" %>
<% end %>
Output of rake routes
admin_informations GET /admin/informations(.:format) admin/informations#index
POST /admin/informations(.:format) admin/informations#create
new_admin_information GET /admin/informations/new(.:format) admin/informations#new
edit_admin_information GET /admin/informations/:id/edit(.:format) admin/informations#edit
admin_information GET /admin/informations/:id(.:format) admin/informations#show
PUT /admin/informations/:id(.:format) admin/informations#update
DELETE /admin/informations/:id(.:format) admin/informations#destroy
admin_root /admin(.:format) admin/sessions#new
Try just
<%= form_for information ,:namespace=>'admin' do |f| %>
UPDATE:
Look at your routes 'informations' pluralized, but your using the singular form 'information'
You must use correct form of controller, because:
'information'.pluralize
is
"information", not informations.
So, rename controller and view folder.
I'm not sure if this will work... Just a guess.
form_for #admin.information or something along those lines.