rails multiple input record - ruby-on-rails

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?

Related

<%= 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.

Rails: form_for doesn't turn parameters

I'm basically trying to create a Rails form_for but it simply does not turn parameters. I've checked the server log and it receives the parameter but cannot capture on the create function. I'm watching andrewperk's tutorial (http://www.youtube.com/watch?v=unq1yubL6lQ) about create and save methods. What could I possible done wrong?. Here is my Users controller;
def new
#user=User.new
end
def create
#user=User.new(params[:user])
if #user.blank?
render :new
else
#user.save
render :index
end
end
And here is my form;
<h1>Add an available user</h1>
<%= form_for #user do |a| %>
<p>
<%= a.label :name %>
<%= a.text_field :name %>
</p>
<p>
<%= a.label :surname %>
<%= a.text_field :surname %>
</p>
<p>
<%= a.submit "Add new available user"%>
</p>
<% end %>
You should use params[:user] in your controller, because this is how the HTML form fields are named by default (you can check page source).
Also I'd suggest refactoring the create method a bit. It is a convention that you redirect to index page instead of just rendering it after creating a record.
def create
#user = User.new(params[:user])
if #user.save
redirect_to users_path
else
render :new
end
end

Routing Error - No route matches [POST] for new

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.

Form is submitting when the page loads

I have a really simple Rails app. Basically an article with comments. I want the article page to show the article, comments underneath and then a textbox to write a comment and a submit button to submit it.
I got it all working except for one (big) problem. When the page loads.. example.com/article/1
a blank comment is submitted to the database.
I fixed that by including "validates_presence_of :body" in the Comment model. But that results in the following image when the page loads:
This is my code by the way:
def show
#place = Article.find(params[:id])
#comment = Article.find(params[:id]).comments.create(params[:comment])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #article }
end
end
and
<% form_for([#article, #comment]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
It's because you made a create.
Create try save object and like is invalid, an error is generate.
Made :
#comment = Article.find(params[:id]).comments.build(params[:comment])
Fix your problem I think

Resources