Routing Error - No route matches [POST] for new - ruby-on-rails

I'm getting an error with routes and I can't find where is the problem, I'm creating a simple CRUD and get this trouble with the create method.
Error
No route matches [POST] "/usuarios/new"
Controller
def new
#usuario = Usuarios.new
end
def create
#usuario = Usuarios.new(params[:usuario])
if #usuario.save
redirect_to usuario_path, :notice => "Cadastrado realizado com sucesso!"
else
render "new"
end
end
new.html.erb
<h1>Add new user</h1>
<%= form_for (:usuario) do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :idade %><br />
<%= f.text_field :idade %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.submit "send" %>
</p>
<% end %>

As Flexoid has pointed out, you probably haven't add the new method in the controller.
So, put this
def new
#usuario = Usuario.new
end
EDIT
You have to pay more attention.
Take a look:
def new
#usuario = Usuario.new # not Usuarios.new, that's wrong.
end
def create
#usuario = Usuario.new(params[:usuario]) # not usuarios, first letter should be capital
if #usuario.save
redirect_to usuarios_path, :notice => "Cadastrado realizado com sucesso!" # usuario_path requires an id parameter like `usuario_path(#usuario)` or you could redirect to the `index` with `usuarios_path`
else
render "new"
end
end

Change
<%= form_for (:usuario) do |f| %>
to
<%= form_for (#usuario) do |f| %>

Seems like you forgot about configuring of the Rails Router.
Try to add this to your config/routes.rb file:
resources :usuarios
For the reference you can read rails guide Rails Routing from the Outside In.

Related

NameError in Controller#new in view in ruby on rails on windows

I am currently doing a CRUD, i am now in part of create method but I'm encountering when I load the new method(this is where my form is) NameError in Products#new
Question: Is my products_create_path correct? This is the action after I send the form into create method
New file
Add New Item
<%= form_for :product, url: products_create_path do |f| %>
<p>
<%= f.label :Name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :Size %><br>
<%= f.text_area :description %>
</p>
<p>
<%= f.label :Price %><br>
<%= f.text_field :price %>
</p>x
<p>
<%= f.submit :Submit %>
</p>
<% end %>
<%= link_to 'BACK', products_path %>
Routes
Rails.application.routes.draw do
resources :products
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Controller
class ProductsController < ApplicationController
def index
#product = Product.all.order('created_at DESC')
end
def show
#post = Product.find(params[:id])
end
def new
#product = Product.new
end
def create
#post = Product.new(post_params)
if #post.save
redirect_to (products_path)
else
redirect_to('new')
end
end
private
def post_params
params.require(:product).permit(:name, :size, :price)
end
end
You don't need to give url in form_for tag. Rails internally redirect to path depends on in presence of id value.
Change form_for tag to this:
<%= form_for #product do |f| %>
So in your form_for tag, object (#product) has value for id field, then rails will submit the form to update routes else it will submit the form to create routes.
In your form change :description to :size
and change
<%= form_for :product, url: products_create_path do |f| %>
to
<%= form_for #product do |f| %>
products_create_path is not correct and you don't need it either. Your form should be <%= form_for(#product) do |f| %>
It should be:
<%= form_for :product, url: products_path do |f| %>
You can check your routes by using this command:
rake routes
Or find more information from here.
Hope I can help.
change your form_tag to
<%= form_for #post, :url => new_product_path do |f| %>
or
<%= form_for #product do |f| %>

<%= form_for#in1 do |f| %> ArgumentError in Info1#new

I tried using :Info1 It display the form but again it gives method error
I know it's a small silly mistake but please point it out.
new.html.erb code
add a new record
<%= form_for#Info1 do |f| %>
<p>
<%=f.label :address %><br/>
<%=f.text_field :address %><br/>
</p>
<p>
<%=f.label :state %><br/>
<%=f.text_field :state %><br/>
</p>
<p>
<%=f.submit "Submit"%>
</p>
<%end%>
Controller Code is below
class Info1Controller < ApplicationController
def create
#info1 = Info1.new(in1)
if #info1.save
redirect_to users_path, :notice => "student details are save "
else
render "new"
end
end
def new
end
def in1
params.require(:info1).permit(:address, :state)
end
end
Add this in new
def new
#info1 = Info1.new
end
Also change
<%= form_for #info1 do |f| %>
NOTE
Also, I will suggest you to use the proper naming conventions to avoid such errors
such that,
Rename the model from Info1 to Information and filename to information.rb
Rename the controller from Info1Controller to InformationsController and filename to informations_controller.rb
Routes to
resources :informations

When I try to load my create.html.erb page, I get this error "param is missing or the value is empty"

I got this error on loading the page and I would guess that it has somethinng to do with my create method in the controller
My controller looks like this
class StoryController < ApplicationController
def index
#story = Story.all
end
def new
#story = Story.new
end
def create
#story = Story.new(story_params)
if #story.save
flash[:notice] = "Story created successfully"
flash[:color]= "valid"
else
flash[:notice] = "Story is invalid, man"
flash[:color]= "invalid"
end
end
def show
#story = Story.find(params[:id])
end
private
def story_params
params.require(:story).permit(:story_title, :story_body)
end
end
My create.html.erb looks like
<%= form_for #story ,url: story_path do |f| %>
<%= label :story, :title %><br />
<%= text_field :story, :story_title %>
<%= label :story, :body %><br />
<%= text_field :story, :story_body %>
<%= submit_tag 'Create story' %>
<% end %>
My create.html.erb didnt look like this before, I changed it to that after I read some questions about how form_for would work instead of form_tag for the story_params.
But either way, I still get the error anyways and I would like to know why and if there is a fix for it.
very first, you don't need to specify a path if you are using form_for and if you don't want to submit a form on the custom route.
If you are using new object then it will submit form on create method and for existing object it will submit form on update method.
So your form will be,
<%= form_for #story do |f| %>
<%= f.label :title %><br />
<%= f.text_field :story_title %>
<%= f.label :body %><br />
<%= f.text_field :story_body %>
<%= submit_tag 'Create story' %>
<% end %>
And this form needs to be in new.html.erb file.
This form will submit your form to create action with post method and from there you need to do render or redirection depending upon condition. So your controller will be,
class StoryController < ApplicationController
def index
#story = Story.all
end
def new
#story = Story.new
end
def create
#story = Story.new(story_params)
if #story.save
flash[:notice] = "Story created successfully"
flash[:color]= "valid"
redirect_to story_path(#story)
else
flash[:notice] = "Story is invalid, man"
flash[:color]= "invalid"
render :new
end
end
def show
#story = Story.find(params[:id])
end
private
def story_params
params.require(:story).permit(:story_title, :story_body)
end
end
If you do rake routes in the termial, you can see all methods with its expected methods
Also according to rails conventions, if you have story model then you can directly create :title & :body attributes instead of :story_title and :story_body
The create method is a POST... so when he enters story/create he's already expecting those values... that's why he says he can't find the params... i didn't look at the code deeply but it seems fine. Just change the name of the view to new.html.erb. New is the setup for create.
In new you setup the values and then invoke create where the controller actually creates the story.
Just change the name of the view to new.html.erb and change it to this
<%= form_for #story do |f| %>
<%= f.label :title %><br />
<%= f.text_field :story_title %>
<%= f.label :body %><br />
<%= f.text_field :story_body %>
<%= submit_tag 'Create story' %>
<% end %>
as the other user said. you need to say that those inputs belong to form |f| the text_field belongs to f, f.text_field
And of course you access that view through stories/new
Try this make changes in the create.html.erb
<%= form_for #story ,url: story_path do |f| %>
<%= f.label :title %><br />
<%= f.text_field :story_title %>
<%= f.label :body %><br />
<%= f.text_field :story_body %>
<%= submit_tag 'Create story' %>
<% end %>
Are you sure your attributes for the Story object are named story_title and story_body ?
If not, your story_params should be like this :
def story_params
params.require(:story).permit(:title, :body)
end
And also, apply #koshlendra suggestion, he's right.
I suggest you to generate a scaffolding for a fake resource to see how it's supposed to work :
rails g scaffold Fake title:string body:text
Then look at the generated controller and views to understand fully how it works.

params.require().permit does not work as expected

I have this controller
class PeopleController < ApplicationController
def new
#person = Person.new
#person.phones.new
end
# this is the action that gets called by the form
def create
render text: person_params.inspect
# #person = Person.new(person_params)
# #person.save
# redirect_to people_path
end
def index
#person = Person.all
end
private
def person_params
params.require(:person).permit(:name, phones_attributes: [ :id, :phone_number ])
end
end
and this view
<%= form_for :person, url: people_path do |f| %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<%= f.fields_for :phones do |f_phone| %>
<div class="field">
<p>
<%= f_phone.label :phone_number %><br />
<%= f_phone.text_field :phone_number %>
</p>
</div>
<% end %>
<p>
<%= f.submit %>
</p>
<% end %>
When I fill out both form fields and hit "Save Person" I only get {"name"=>"foo"} - the phone number seems to vanish.
However, when I change phones_attributes to phones I get {"name"=>"foo", "phones"=>{"phone_number"=>"123"}} (this would however cause problems with the create function.
What's wrong here?
Please note that this question is strongly related to that one: accepts_nested_attributes_for: What am I doing wrong as well as to this posting: https://groups.google.com/forum/#!topic/rubyonrails-talk/4RF_CFChua0
You don't have #phones defined in the controller:
def new
#person = Person.new
#phones = #person.phones.new
end
Finally found the problem. In the view there should be
<%= form_for #person, url: people_path do |f| %>
Instead of
<%= form_for :person, url: people_path do |f| %>
#phron said that already here:
accepts_nested_attributes_for: What am I doing wrong

rails multiple input record

I'm new to rails. I have some question that's been quite a headache to me, so here it is:
For example i have this controller & view:
Controller:
class OrdersController < ApplicationController
def new
#Order = Order.new
end
def create
#Order = Order.new(params[:order])
if #Order.save
flash[:notice] = "Successfully created order."
redirect_to #Order
else
render :action => 'new'
end
end
View:
<% title "Menu Order" %>
<%= form_for #Order do |f| %>
<%= f.error_messages %>
<div id="form-order">
<p>
<%= f.label :name%><br />
<%= f.text_field :name, %>
</p>
<p>
<%= f.label :menu_order %><br />
<%= f.text_field :menu_order %>
</p>
</div>
<%= f.submit %>
So my question is :
before displaying the form above, I want to have a text_field_tag that specify how many forms (roughly said, duplicate the form div) I want to generate based on count, and then insert the data to the database simultaneously,
the idea is to speed things up, so that the user don't have to input the data only one at a time, but multiple record at single submit
How do I do that?

Resources