I'm trying to make a form that will post to a database, I'm really struggling at the moment and i'm getting this error.
NameError in AddController#index
uninitialized constant AddController::Newevents
Could you advise what i would need to do?
Heres all the code i have
Form
<%= simple_form_for(#newevent) do |f| %>
<%= f.input :eventname, required: true %>
<%= f.input :eventdate %>
<%= f.input :eventimage %>
<%= f.button :submit %>
<% end %>
controller
class AddController < ApplicationController
def index
#newevent = Newevent.new
end
end
Model
class Newevent < ActiveRecord::Base
def event_params
params.require(:Newevent).permit(:eventname, :eventdate, :eventimage)
end
end
Routes
resources :add
Edit
i now have this error undefined methodnewevents_path'` after changing this
#newevents = Newevent.new
It seems that you miscopied your code here. The error message indicates that your index method actually looks like this
def index
#newevent = Newevents.new
end
Remove the s from the end of Newevent and it should work.
RE: your edit
Your routes declare that you have a resource named add, if you want to show and create your Newevent objects, then you should create a controller for that. Declare resources :newevents in your routes and create a controller to handle it.
You should research RESTful routes, because that's what Rails's resource routing works best with. The form to create a new object should be displayed by the new action and not index.
You should be using create method instead of index if you are using POST http method. index will be called if you are using GET method and it shouldn't be used to post the form data. Refer this link for more information on rails routing.
class AddController < ApplicationController
def create
#newevent = Newevent.new
end
end
Related
I am having problem with routing when using simple_form_for. I understand it is something to do with pluralizing but the same form is working perfectly for edit/update method.
undefined method `companies_path'
Did you mean? companys_path
company_path
companys_controller
def new
#company = Company.new
end
def create
#company = Company.new(company_params)
if #company.save
redirect_to #company
else
render 'new'
end
end
_form.html.haml
= simple_form_for #company do |f|
= f.input :name
= f.input :description
= f.input :website
= f.button :submit
rake routes
companys GET /companys(.:format) companys#index
POST /companys(.:format) companys#create
new_company GET /companys/new(.:format) companys#new
edit_company GET /companys/:id/edit(.:format) companys#edit
company GET /companys/:id(.:format) companys#show
PATCH /companys/:id(.:format) companys#update
PUT /companys/:id(.:format) companys#update
DELETE /companys/:id(.:format) companys#destroy
I guess the problem is, Edit/update is going for 'companys_path' and new/create is going for 'companies_path'. Any help is appreciated.
Controller name should be plural so in your case it should be companies_controller
Follow these steps to change it
Rename controller to companies_controller.rb
Change controller class name to
class CompaniesController < ActionController::Base
# controller code
end
Change in routes.rb
resources :companies
Edit
Controller naming conventions
link in comments by Greg
First thing, you should follow rails naming convention and rename your controller from companys_controller.rb to companies_controller.rb.
Your issue will be fixed after renaming.
Simple form taking assumption that you have followed rails naming convention and making request at companies_path which is not present in your routes.
I am getting an undefined method stripe_managed_accounts_path when trying to create a new resource via typical rails forms. Below is my code, I am dumbfounded, cannot figure it out.
Controller
class StripeManagedAccountsController < ApplicationController
before_action :authenticate_printer!
def new
#stripe_managed_account = StripeManagedAccount.new(printer_id: current_printer.id)
end
end
model
class StripeManagedAccount < ActiveRecord::Base
belongs_to :printer
end
views/new
<h1>Create New Stripe Managed Account</h1>
<%= render 'form' %>
view/form
<h5>inside the form</h5>
<%= form_for #stripe_managed_account do |f| %>
<% end %>
routes
resources :printers, only: [:show, :edit, :update] do
resources :stripe_managed_accounts
end
error
`undefined method 'stripe_managed_accounts_path' for #<#<Class:0x007fc627d342b8>:0x007fc62b36e108>`
routes
printer_stripe_managed_accounts GET /printers/:printer_id/stripe_managed_accounts(.:format) stripe_managed_accounts#index
POST /printers/:printer_id/stripe_managed_accounts(.:format) stripe_managed_accounts#create
new_printer_stripe_managed_account GET /printers/:printer_id/stripe_managed_accounts/new(.:format) stripe_managed_accounts#new
edit_printer_stripe_managed_account GET /printers/:printer_id/stripe_managed_accounts/:id/edit(.:format) stripe_managed_accounts#edit
printer_stripe_managed_account GET /printers/:printer_id/stripe_managed_accounts/:id(.:format) stripe_managed_accounts#show
PATCH /printers/:printer_id/stripe_managed_accounts/:id(.:format) stripe_managed_accounts#update
PUT /printers/:printer_id/stripe_managed_accounts/:id(.:format) stripe_managed_accounts#update
DELETE /printers/:printer_id/stripe_managed_accounts/:id(.:format) stripe_managed_accounts#destroy
and it is highliting this line <%= form_for #stripe_managed_account do |f| %>
I have grepped the entire code base for stripe_managed_accounts_path and it is no where. I am at odds end...
UPDATE:::
If I add that route it disappears??? Why is it looking for that route. Is it becasue of how I named my fodlers, etc??
You're nesting stripe_managed_accounts inside printers on your routes file. If you take a look at the output of rake routes, you can see that there isn't a path for stripe_managed_accounts_path.
You can either use the shallow option on the stripe_managed_accounts resource or adjust your form to include the printer which the managed account will belong to.
#controller
class StripeManagedAccountsController < ApplicationController
before_action :authenticate_printer!
def new
#stripe_managed_account = current_printer.build_stripe_managed_account
end
def create
current_printer.stripe_managed_accounts.create stripe_managed_account_params
# handle response
end
def stripe_managed_account_params
params.require(:stripe_managed_account).permit([list of attributes])
end
end
#form
<h5>inside the form</h5>
<%= form_for [current_printer, #stripe_managed_account] do |f| %>
<% end %>
That will generate the proper url, nesting the stripe_managed_account inside the current printer.
For has_one association reference http://guides.rubyonrails.org/association_basics.html#has-one-association-reference
I'm trying to make simple app. I input my first name and last name to simple <%= form_for #data do |f| %> rails form and after submitting it, app should render simple text like this. My first name is <%= data.first_name %> and my last name is <%= data.last_name %>. I don't know why but my app is saying this error:
undefined local variable or method `data' for
It's probably saying it because no params are passed to view.
Here is my code.
routes.rb
resources :data, only: [:new, :create, :index]
data_controller.rb
class DataController < ApplicationController
def new
#data = Data.new
end
def index
end
def create
#data = Data.new(data_params)
if #data.valid?
redirect_to #data
else
render :new
end
end
private
def data_params
params.require(:data).permit(:first_name, :second_name)
end
end
/views/data/new.html.erb
<%= form_for #data do |f| %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :second_name %>
<%= f.text_field :second_name %>
<%= f.submit 'Continue', class: 'button' %>
<% end %>
/views/data/index.html.erb
<h2>Coolest app ever :D</h2>
<p>My first name is: <%= data.first_name %>.</p>
<p>And my second name is: <%= data.second_name %>.</p>
/models/data.rb
class Data
include ActiveModel::Model
attr_accessor :first_name, :second_name
validates :first_name, :second_name, presence: true
end
Please help to find out why params are not passing to next page. Thanks anyways :D
Your view should look like this:
<h2>Coolest app ever :D</h2>
<p>My first name is: <%= #data.first_name %>.</p>
<p>And my second name is: <%= #data.second_name %>.</p>
Also, I would suggest that calling a model something generic like Data is not a very Rails-y approach. Generally, domain models correspond to real-world things like User and Article, which are easy to understand and relate to. It'll get confusing quite fast if you use need to make another model and want to call it Data2 or something :)
Edit:
Since you specified that you do not wish to use the database, I would recommend passing in the object params through the redirect:
redirect_to(data_path(data: #data))
and in your controller's index method:
def index
#data = Data.new(params[:data])
end
Now your view should render properly, since you're passing the in-memory #data object attributes as params within the redirect. You then recreate this object in the index page or wherever you wish to redirect to.
To expand on Matt's answer, the reason you're getting NilClass errors is because:
You're redirecting to a data#show action when no show action has been enabled within your routes file. Since you've set your views up for the index, I'm assuming you want to redirect there when the #data object has been verified as valid:
redirect_to data_path
However I would recommend you follow Rails conventions and specify the data#show route within your routes.rb:
resources :data, only: [:index, :new, :create, :show]
and in your data_controller.rb:
def show
#data = Data.find(params[:id])
end
Another problem is that you're not actually saving the #data object upon creating it. The new method populates the attributes, and valid? runs all the validations within the specified context of your defined model and returns true if no errors are found, false otherwise. You want to do something like:
def create
#data = Data.new(data_params)
if #data.save
redirect_to data_path
else
render :new
end
end
Using save attempts to save the record to the database, and runs a validation check anyways - if validation fails the save command will return false, the record will not be saved, and the new template will be re-rendered. If it is saved properly, the controller will redirect to the index page, where you can call upon the particular data object you want and display it within your view.
I've been stuck on this for a bit and can't figure out the exact reason why I'm getting the following error:
undefined method `entries_path' for <%= form_for(#entry) do |f| %>
entry_controller:
class EntryController < ApplicationController
def index
end
def new
#entry = Entry.new
end
def create
#entry = Entry.new(user_params)
if #entry.save
redirect_to #entry
else
render 'new'
end
end
private
def user_params
params.require(:entry).permit(:comment, :flag)
end
end
routes has:
resources :entry
and the new page where the error occurs:
<%= form_for(#entry) do |f| %>
<%= f.label :comment %>
<%= f.text_field :comment %>
<%= f.label :flag %>
<%= f.text_field :flag %>
<% end %>
I can't figure out why I'm getting this error.
form_for needs to reference the path associated with #entry (i.e. entries_path), but your routes.rb file uses the singular form of the resource (:entry) rather than the required plural form (:entries), so the proper path names don't exist.
Rails models use the singular form, but the Rails database, controllers, views use the plural form and this is reflected in the routes file. One way to remember this is that a model is describing a single class that each object belongs to. Everything else, pretty much, is responsible for managing multiple instances, so while they themselves are singular (e.g. Controller), they refer to the objects they manage in the plural form (e.g. EntriesController, controller/entries directory).
See Ruby on Rails plural (controller) and singular (model) convention - explanation for more discussion of this.
Controller and views should always be treated in plural form. For example, if you have an object Book, then the controller declaration should be
class BooksController < ApplicationController
and the views( new, edit, show, index ) should be inside a folder named
/books
Also, the declaration of routes should be in plural form. In this case, the routes should be declared as
resources :books
You could try to generate the controller and view folder by running in your terminal:
rails generate controller name_of_object_in_plural_form( for sample, books)
The script will generate a controller named books_controller.rb and /books folder under /views
I am attempting to get a form generated by simple-form in my modal, however I keep running into the following error upon loading the page.
undefined method 'model_name' for NilClass:Class
Here is the simple code I was using to try to generate the form
_header.html.erb (under the view_pages_controller)
<%= simple_form_for #update do |f| %>
<%= f.input :lang %>
<%= f.input :book %> #temp, just for testing simpform
<%= f.button :submit %>
<% end %>
I am pretty sure the problem lies with my controller code
updates_controller.rb
class UpdatesController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
def create
#update = current_user.updates.build(params[:update])
if #update.save
flash[:success] = "Update successful"
redirect_to root_path
else
flash[:error] = "Failed to update, please try again"
redirect_to root_path
end
end
end
update.rb
class Update < ActiveRecord::Base
attr_accessible :book, :user_id, :lang, :round_id
belongs_to :user
end
Any help/tips would be greatly appreciated. I know my code sucks.
The problem is that #update is null in your view. You should clarify which action renders this view, and set value to #update. The create action only sets it based on the params, and then redirects to root.
I believe you are using some kind of RESTfull controller, and you are rendering your form on action :new. So, to solve your trouble, add
#update = current_user.updates.build(params[:update])
to your new action
Have you tried putting a debugger in above the line in your controller where you declare #update.
You could then see your current_user object and what methods are available using something like current_user.methods
Have you included resource:'updates' or resources:'updates' in routes.rb.This may solve the problem.