Rails - Submitting multiple forms with one submit button - html

In my application I have a page for editing all users however I would like it so that I only have to click one submit button to update the users' details but at the moment I can only edit one and click the 'Update User' button to change that one user's details.
_form.html.erb
<%= form_tag edit_user_path do |form| %>
<% #users.each do |user| %>
<%= form_for user do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %><br>
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :sunday %><br>
<%= f.text_field :sunday %>
</div>
<div class="field">
<%= f.label :monday %><br>
<%= f.text_field :monday %>
</div>
<div class="field">
<%= f.label :tuesday %><br>
<%= f.text_field :tuesday %>
</div>
<div class="field">
<%= f.label :wednesday %><br>
<%= f.text_field :wednesday %>
</div>
<div class="field">
<%= f.label :thursday %><br>
<%= f.text_field :thursday %>
</div>
<div class="field">
<%= f.label :friday %><br>
<%= f.text_field :friday %>
</div>
<div class="field">
<%= f.label :saturday %><br>
<%= f.text_field :saturday %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
As you can see that submit_tag at the bottom of the code does absolutely nothing however I would like it to update all the users' details. Any help would be greatly appreciated!

Against:
<%= form_for user do |f| %>
use:
<%= fields_for "users[]", user do |f| %>
After that you will get in the controller parameters values for each user_id:
"users"=>{"user_id1"=>{"attr1"=>"value1"}, "user_id2"=>{"attr1"=>"value1"}
Also to make possible updating a collection of object a way to do it is to add an action to the UsersController like this:
def update_collection
# Update users here
end
and update routing in config/routes.rb:
resources :users do
collection do
match 'update_collection', via: [:put, :patch]
end
end
and use proper url in the main form:
<%= form_tag update_collection_users_path, method: :put do |form| %>

Related

Rails ArgumentError 'First argument in form cannot contain nil or be empty'

It's viewfile below
<%= form_for #new_question do |f| %>
<div>
<%= f.label :title %>
<%= f.text_field :title%>
</div>
<div>
<%= f.label :body %>
<%= f.text_field :body%>
</div>
<%= f.submit %>
<% end %>
It's controller below
def new
#new_question = Question.new
end
I don't know why it throws error

undefined method in text_field while column exists

I have a form that is going to be used, but i run into this problem when opening the form:
NoMethodError in Measures#new
Showing /home/supervisor/Rubyrails/Werkvergunning/app/views/measures/_form.html.erb where line #22 raised:
undefined method `measurement_type' for #<Measure:0x007f7bb3c5f978>
Did you mean? measurement
measurement?
measurement=
measurement_was
I am absolutely positive that the field exists and other fields from the same table used to work without problem, so it is just thos one field.
What is going on and how can i fix it?
The _form:
<%= form_for(#measure) do |f| %>
<% if #measure.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#measure.errors.count, "error") %> prohibited this measure from being saved:</h2>
<ul>
<% #measure.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :measurement %><br>
<%= f.text_field :measurement %>
</div>
<div class="field">
<%= f.label :type %><br>
<%# f.select :measurement_type, [['Product verklaring','product vrij verklaring'],['Elektrotechnische maatregel','Elektrotechnische maatregelen'],['Maatregel door vergunninghouder','maatregel door vergunninghouder'], ['brandpreventie', 'brandpreventie'], ['persoonnlijke bescherming', 'persoonlijke bescherming']] %>
<%= f.text_field :measurement_type %>
</div>
<div class="field">
<%= f.label :valid_from %><br>
<%= f.date_select :valid_from %>
</div>
<div class="field">
<%= f.label :valid_to %><br>
<%= f.date_select :valid_to %>
</div>
<div class="field">
<%= f.label :enquiry_measure_id %><br>
<%= f.text_field :enquiry_measure_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
The table:
create_table "measures", force: :cascade do |t|
t.string "measurement", limit: 255
t.string "measurement_type", limit: 255
t.date "valid_from"
t.date "valid_to"
t.integer "enquiry_measure_id", limit: 4
end
Thanks

gem 'country_select', github: 'stefanpenner/country_select' is not working with rails 4

I am using gem 'country_select', github: 'stefanpenner/country_select' in my gem file and in my form i have defined it like this:
<%= form_for(#account_detail) do |f| %>
<div class="field">
<%= f.label :city %><br>
<%= f.text_field :city %>
</div>
<div class="field">
<%= f.label :zip %><br>
<%= f.number_field :zip %>
</div>
<div class="field">
<%= f.label :first_name %><br>
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br>
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :country %><br>
<%= f.country_select("account_detail", "country") %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
on submit its giving error ActionView::Template::Error (wrong number of arguments (4 for 0)):
Which gem is best to show all countries?
I would use:
<%= f.country_select :country %>
If you like to prioritize some countries in the select pass them in in an array:
<%= f.country_select :country, {priority_countries: %w(<COUNTRY CODE i.e. US>), prompt: 'Select Country'} %>
You can add class: 'your-class' and id or whatever just as with any other field if you like. Hope it helps.
This should do!
<%= f.country_select :country, { priority_countries: ["GB", "US"], selected: "GB" } %>
I have solved this issue by adding this method in my model:
def country_name
country = ISO3166::Country[country_code]
country.translations[I18n.locale.to_s] || country.name
end
and in view change given line :
<%= f.country_select("account_detail", "country") %>
to this:
<%= f.country_select :country, format: :with_alpha2 %>
Hope this will help to someone else who will face this problem.

ActionController::UrlGenerationError in Products#edit

I'm new to rails and I'm having trouble in a sales site, I have a User and produito the protuto User belongs to, everything works right exeto edit the action of the product.
ActionController::UrlGenerationError in Products#edit
No route matches {:action=>"show", :controller=>"products", :format=>nil, :id=>nil, :user_id=>#} missing required keys: [:id]
my_edit
<h1>Editing product</h1>
<%= render 'form' %>
<%= link_to 'Show', #product %> |
<%= link_to 'Back', products_path %>
my _form
<%= form_for [:user,#product], :html => {multipart: true} do |f| %>
<% if #product.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#product.errors.count, "error") %> prohibited this product from being saved:</h2>
<ul>
<% #product.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :price %><br>
<%= f.text_field :price %>
</div>
<div class="field">
<%= f.label :local %><br>
<%= f.text_field :local %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :category_id %><br>
<%= f.select :category_id, Category.all.map {|c| [c.name, c.id]} %>
</div>
<div class="field">
<%= f.label :contacts %><br>
<%= f.text_field :contacts %>
</div>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
my rake routes
Prefix Verb URI Pattern Controller#Action
sessions_new GET /sessions/new(.:format) sessions#new
category_category GET /categories/:id/category(.:format) categories#category
categories GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
edit_category GET /categories/:id/edit(.:format) categories#edit
category GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
home_index GET /home(.:format) home#index
POST /home(.:format) home#create
new_home GET /home/new(.:format) home#new
edit_home GET /home/:id/edit(.:format) home#edit
home GET /home/:id(.:format) home#show
PATCH /home/:id(.:format) home#update
PUT /home/:id(.:format) home#update
DELETE /home/:id(.:format) home#destroy
profile_user GET /users/:id/profile(.:format) users#profile
user_products GET /users/:user_id/products(.:format) products#index
POST /users/:user_id/products(.:format) products#create
new_user_product GET /users/:user_id/products/new(.:format) products#new
edit_user_product GET /users/:user_id/products/:id/edit(.:format) products#edit
user_product GET /users/:user_id/products/:id(.:format) products#show
PATCH /users/:user_id/products/:id(.:format) products#update
PUT /users/:user_id/products/:id(.:format) products#update
DELETE /users/:user_id/products/:id(.:format) products#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
login GET /login(.:format) sessions#new
logout GET /logout(.:format) sessions#destroy
root GET / home#index
From your comments I found that you are missing to pass ID for the product.
You need to pass #user & #product objects like this:
<%= link_to 'Edit', edit_user_product_path(#user, #product) %>

HTML <form> tag causing Rails form to submit a GET instead of POST request

I have a form that works fine until I add the tags for styling (I am using twitter bootstrap). The app is Rails 3.1 and I am on a mac. Here is a form that works fine with no issues:
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="alert-message error">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit %></div>
<%end %>
Then, when I add the tag to the same exact form like this:
<form class="form-stacked">
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
<div class="alert-message error">
<h2>Form is invalid</h2>
<ul>
<% for message in #user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions"><%= f.submit %></div>
<%end %>
</form>
This form appears to issue a GET request instead of a POST request. Instead of redirecting me upon save like it does without the tag, I see this in the URL:
http://localhost:3000/signup?utf8=%E2%9C%93&authenticity_token=Pt1vOp7lykCPFdj5BJeZ6xwJM2vy0JomMGSKoB%2FyYpU%3D&user%5Bemail%5D=test%40get.com&user%5Bpassword%5D=kevin&user%5Bpassword_confirmation%5D=kevin&commit=Create+User
I have tried specifying post in both the rails and HTML. Specifying it in rails using :method => post yields the same results (the GET request and URL info above). Using in the HTML looks like a POST request but the User object isn't saving (I belive this is the case because I am not getting redirected to the root_url, which is set in the controller and works fine when not using tag). Any ideas?
you want this:
<%= form_for #user, :html => { :class => "form-stacked", :id => "something" } do |f| %>
stuff goes in here as before
<% end %>