How to update multiple record in rails 6 - ruby-on-rails

my controller
class IndustryController < ApplicationController
def index
#industries=Industry.all
end
def new
#industry=Industry.new
end
def create
#industry = Industry.new(industry_params)
# #industry = Industry.new(params[:name_bef,:name,:status])
# #industry = Industry.new(:name_bef => (params[:name]),:name => (params[:name]), :status => (params[:status]))
if #industry.save
redirect_to industry_index_path
else
render 'new'
end
end
def update
#industry = Industry.all
#industry.each do |i|
#industry.find(i.id)
#industry.update(industry_params)
end
end
private
def industry_params
params.require(:industry).permit(:name, :status)
end
private
def industry_update
params.require(:industry).permit(:name, :status)
end
end
my html
<%= form_for #industries, as: :industry, url: industry_path(#industries), method: :patch do |f| %>
<table class="table table-bordered">
<thead class="bg-light">
<tr>
<th scope="col">業種名(変更前)</th>
<th scope="col">業種名</th>
<th scope="col">状態</th>
</tr>
</thead>
<tbody>
<% #industries.each do |industry| %>
<tr>
<td><%= industry.name %></td>
<td>
<div class="form-group">
<%= f.text_field :name, :value => industry.name, :id => industry.id, :class => "form-control" %>
</div>
</td>
<td>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="form-group text-center">
<%= f.submit"登録", :class => " btn btn-success" %>
</div>
<%end%>

My assumption is that you are talking about the update action in your controller. It looks like update_all is the method you are looking for.
def update
Industry.update_all(industry_params)
end
This can be dangerous - be aware that this will bypass active record validations and callbacks, so make sure you are certain.

Related

unable to convert unpermitted parameters to hash rails

I was trying to add csv for my the search results but it throws error telling unpermitted params. What params should i pass in the csv link? So that csv would download the only the search results.
ReportsController
def revenue_search
#bookings = Booking.complete.order("created_at DESC")
date = "1-#{params['date']['month']}-#{params['date']['year']}"
date = Date.parse(date)
#bookings = #bookings.where(created_at: date.beginning_of_month..date.end_of_month)
#per_page = params[:per_page] || Booking.per_page || 20
#bookings = #bookings.paginate( :per_page => #per_page, :page => params[:page])
if params[:currency].present?
#bookings = #bookings.where(currency: params[:currency])
end
respond_to do |format|
format.html
format.csv { send_data #bookings.revenue_csv }
end
end
Model
def self.revenue_csv
attributes = %w{ total value deposit balance }
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |booking|
csv << attributes.map{ |attr| booking.send(attr) }
end
end
end
revenue.html.erb
<%= form_tag revenue_search_path, :method => 'get' do %>
<%= select_tag(:currency, options_for_select(["USD", "GBP"]), class: "form-control custom-select" , :prompt => 'By Office' ) %>
<%= select_month((#selected_date || Date.today), {}, class:"custom-select", :prompt => 'By Month')%>
<%= select_year((#selected_year || Date.today.year), {}, class:"custom-select" )%>
<%= submit_tag "Search", name: nil , class: "btn btn-info" %>
<% end %>
<table id="demo-foo-addrow" class="table m-t-30 table-hover no-wrap contact-list no-paging footable-loaded footable" data-page-size="2">
<thead>
<tr>
<th class="footable-sortable">Total No of students<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Value of courses sold<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Total Deposits<span class="footable-sort-indicator"></span></th>
<th class="footable-sortable">Balance payments<span class="footable-sort-indicator"></span></th>
</tr>
</thead>
<tbody>
<tr>
<td><%= #bookings.count %></td>
<td><%= #bookings.count %></td>
<td><%= #bookings.sum{|x| (x.payment_gross.to_f)} %></td>
<td><%= #bookings.sum{|x| (x.course&.course_fee || x.event&.course_fee).to_f - x.payment_gross.to_f }.to_f %></td>
</tr>
</tbody>
</table>
<p> Download: <%= link_to "CSV", revenue_search_path(format: "csv" , params: params ) %> </p>
logs:
ActionView::Template::Error (unable to convert unpermitted parameters to hash):
42: </div>
43: </div>
44: </div>
45: <p> Download: <%= link_to "CSV", revenue_search_path(format: "csv" , params: params ) %> </p>
app/views/reports/revenue_search.html.erb:45:in `_app_views_reports_revenue_search_html_erb___71501213_135144540'
You can use to_unsafe_h
revenue_search_path(format: "csv" , params: params.to_unsafe_h)

undefined 'items' for nil class

on my e-store website when I try to checkout my cart, I'm getting
undefined method `items' for nil:NilClass.
Although on the error page
I know that my cart is there... but when I call it, it gives me nil
Cart Model
class Cart
attr_reader :items
def self.build_from_hash hash
items = if hash["cart"] then
hash["cart"]["items"].map do |item_data|
CartItem.new item_data["product_id"], item_data["quantity"]
end
else
[]
end
new items
end
def initialize items = []
#items = items
end
def add_item product_id
item = #items.find { |item| item.product_id == product_id }
if item
item.increment
else
#items << CartItem.new(product_id)
end
end
def empty?
#items.empty?
end
def count
#items.length
end
def serialize
items = #items.map do |item|
{
"product_id" => item.product_id,
"quantity" => item.quantity
}
end
{
"items" => items
}
end
def total_price
#items.inject(0) { |sum, item| sum + item.total_price }
end
end
Application Controller
def initialize_cart
#cart = Cart.build_from_hash session
end
Cart Controller
class CartsController < ApplicationController
before_filter :initialize_cart
def add
#cart.add_item params[:id]
session["cart"] = #cart.serialize
product = Product.find params[:id]
redirect_to :back, notice: "Added #{product.name} to cart."
end
def show
end
def checkout
#order_form = OrderForm.new user: User.new
end
end
Order Controller
class OrdersController
def create
#order_form = OrderForm.new(
user: User.new(order_params[:user]),
cart: #cart
)
if #order_form.save
redirect_to '/', notice: "Thank you for placing your order."
#cart.empty?
else
render 'carts/checkout'
end
end
Checkout View
<div class="container-checkout">
<p class="text-title"> You are checking out the following: </p>
<table class="table table-striped">
<thead class="name-table">
<tr>
<td> Image </td>
<td> Name </td>
<td> Category</td>
<td> Size </td>
<td> Item Price </td>
</tr>
</thead>
<tbody>
<% #cart.items.each do |item| %>
<tr>
<td><img src="<%= item.product.image %>" width="50px"></td>
<td><%= item.product.name.capitalize %></td>
<td><%= item.product.category.name %></td>
<td><%= item.product.size %></td>
<td class="price-item"><%= number_to_currency item.total_price %>
</td>
<% end %>
</tr>
<tr class="total-price total-price-checkout">
<td class="name-table">Total Price</td>
<td class="price-item"><%= number_to_currency #cart.total_price %></td>
</tr>
</tbody>
</table>
</div>
<div class="details-user-form">
<%= form_for #order_form, url: orders_path do |f|%>
<% f.fields_for :user, #order_form.user do |u| %>
<p class="text-title">Fill the form with your details</p>
<p><%= render "orders/errors" %></p>
<p><%= u.text_field :name, placeholder: "Name" %></p>
<p><%= u.text_field :email, placeholder: "Email" %></p>
<p><%= u.text_field :address, placeholder: "Address" %></p>
<p><%= u.text_field :postal_code, placeholder: "Postal code" %></p>
<p><%= u.text_field :city, placeholder: "City" %></p>
<p><%= u.text_field :country, placeholder: "Country" %></p>
<%= f.submit "Place order", class: "order-btn"%><br>
<% end %>
<% end %>
</div>
Any idea of why is it doing so? Also because, it was working before.. I don't know why it stopped.
I think the problem may be that the #cart variable isn't being set in the OrdersController. Setting the variable in CartsController doesn't make it available globally, as it would only be scoped to the controller that created it, in your case the CartsController.
Also, I see that your Cart model is more of a virtual model than an ActiveRecord model, is that the behaviour you were looking for as I believe ActiveRecord already has a lot of the methods you're recreating there.
I'm not totally sure but I think these may be the issues.
UPDATE
I think I found your error.
In your OrdersController you should have a
before_action :initialize_cart
That seems to be coming from your ApplicationController
If you check your checkout method in your CartController, you will see that you did not set #cart. So when you hit the checkout view, it comes to look for the value or #cart in this method. Setting it there, like the code below should clear your error.
def checkout
#order_form = OrderForm.new user: User.new
#cart = # cart object
end

Rails syntax error </div>

I am trying to set "edit" function for a simple CMS. I can make it to create/delete, but it just won't let me "edit".
here is the error message:
SyntaxError in SectionsController#edit
app/views/sections/edit.html.erb:42: syntax error, unexpected keyword_ensure, expecting keyword_end
Extracted source (around line #42):
40
41
when I checked my edit.html.erb. it seems fine?
'index'}, :class => 'back-link') %>
<div class="sections edit">
<h2>Update Sections</h2>
<%= form_for(:two, :url => {:action => 'update', :id => #one.id}) do |f| %>
<table summary="Section form fields">
<% #one.each do |f| %>
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible</th>
<td><%= f.text_field(:visible) %></td>
</tr>
<tr>
<th>content_type</th>
<td><%= f.text_field(:content_type) %></td>
</tr>
<tr>
<th>content</th>
<td><%= f.text_field(:content) %></td>
</tr>
<tr>
<th>page_id</th>
<td><%= f.text_field(:page_id) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Update Section") %>
</div>
<% end %>
</div>
Here is the controller:
class SectionsController < ApplicationController
def index
#one = Section.all
end
def show
#one = Section.find(params[:id])
end
def new
#one = Section.new
end
def create
#one = Section.new(section_params)
if #one.save
flash[:notice] = "Section created successfully!"
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#one = Section.find(params[:id])
end
def update
#one = Section.find(params[:id])
if #one.update_attributes(section_params)
flash[:notice] = "Subject updated successfully!"
redirect_to(:action => 'show', :id =>#one.id)
else
render('edit')
end
end
def delete
#one = Section.find(params[:id])
end
def destroy
subject = Section.find(params[:id]).destroy
flash[:notice] = "Subject deleted successfully!"
redirect_to(:action => 'index')
end
private
def section_params
params.require(:two).permit(:id,:name,:position,:visible,:page_id,:content,:content_type)
end
end
Thanks so much!!
Thanks #Pavan, #MarsAtomic and #Mandeep ! I found the issue: <% #one.each do |f| %>. I don't really need this line. If I need it, I need a end code for this.
In my case, I don't, so I go ahead deleted this line, and it worked now!
thanks again everyone!

Rails has_and_belongs_to_many AND collection_check_boxes

So there isn't an error, my join table just isn't being populated with any information... #lead.districts returns District::ActiveRecord_Associations_CollectionProxy rather than the expected text...
Where have I made my mistake?
new.html.erb
<%= link_to("<< Back to List", {:action => 'index'}, :class => 'back-link') %>
<div class="subjects new">
<h2>Create Lead</h2>
<%= form_for(:lead, :url => {:action => 'create'}) do |f| %>
<table cellspacing="0">
<tr>
<th>First Name</th>
<td><%= f.text_field(:first_name) %></td>
</tr>
<tr>
<th>Last Name</th>
<td><%= f.text_field(:last_name)%></td>
</tr>
<tr>
<th>Phone Number</th>
<td><%= f.text_field(:phone_number)%></td>
</tr>
<tr>
<th>Email</th>
<td><%= f.text_field(:email)%></td>
</tr>
<tr>
<th>Move-in Date</th>
<td><%= f.date_field(:move_in_date, min: Date.today)%></td>
</tr>
<tr>
<th>Bedrooms</th>
<td><%= f.text_field(:beds)%></td>
</tr>
<tr>
<th>Bathrooms</th>
<td><%= f.text_field(:baths)%></td>
</tr>
<tr>
<th>Maximum Price Considered</th>
<td><%= f.text_field(:maxprice)%></td>
</tr>
<tr>
<th>Preferred Neighborhoods</th>
<td>
<%= collection_check_boxes(:lead, :district_ids, District.all.order("districts.name ASC"), :id, :name )%>
</td>
</tr>
<tr>
<th>Have you ever broken a lease?</th>
<td>
<%= f.label :broken_lease, "Yes", :value => true %>
<%= f.radio_button :broken_lease, true %>
<%= f.label :broken_lease, "No", :value => false %>
<%= f.radio_button :broken_lease, false %>
</td>
</tr>
<tr>
<th>Have you ever been convicted of a felony?</th>
<td>
<%= f.label :felon, "Yes", :value => true %>
<%= f.radio_button :felon, true %>
<%= f.label :felon, "No", :value => false %>
<%= f.radio_button :felon, false %>
</td>
</tr>
<tr>
<th>Do you have any pets?</th>
<td>
<%= f.label :pets, "Yes", :value => true %>
<%= f.radio_button :pets, true %>
<%= f.label :pets, "No", :value => false %>
<%= f.radio_button :pets, false %>
</td>
</tr>
<tr>
<th>If so, please provide a brief description</th>
<td><%= f.text_area(:pets_description)%></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Create Lead") %>
</div>
<% end %>
</div>
leads_controller.rb
class LeadsController < ApplicationController
layout false
def index
#leads = Lead.order("leads.created_at DESC")
end
def show
#lead = Lead.find(params[:id])
end
def new
#lead = Lead.new
#districts = District.all.order("districts.name ASC")
end
def create
#lead = Lead.new(lead_params)
if #lead.save
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#lead = Lead.find(params[:id])
end
def update
#lead = Lead.find(params[:id])
if #lead.update_attributes(lead_params)
redirect_to(:action => 'show', :id => #lead.id)
else
render('edit')
end
end
def delete
#lead = Lead.find(params[:id])
end
def destroy
lead = Lead.find(params[:id]).destroy
flash[:notice] = "Lead '#{lead.id}' deleted successfully."
redirect_to(:action => 'index')
end
private
def lead_params
params.require(:lead).permit(:first_name, :last_name, :phone_number, :email, :move_in_date, :beds, :baths, :maxprice, :broken_lease, :felon, :pets, :pets_description, :district => [])
end
end
districts_controller.rb
class DistrictsController < ApplicationController
layout false
def index
#districts = District.order("districts.name ASC")
end
def show
#district = District.find(params[:id])
end
def new
#district = District.new
end
def create
#district = District.new(district_params)
if #district.save
flash[:notice] = "District created successfully."
redirect_to(:action => 'index')
else
render('new')
end
end
def edit
#district = District.find(params[:id])
end
def update
#district = District.find(params[:id])
if #district.update_attributes(district_params)
flash[:notice] = "District updated successfully."
redirect_to(:action => 'show', :id => #district.id)
else
render('edit')
end
end
def delete
#district = District.find(params[:id])
end
def destroy
district = District.find(params[:id]).destroy
flash[:notice] = "District '#{district.name}' deleted successfully."
redirect_to(:action => 'index')
end
private
def district_params
params.require(:district).permit(:name)
end
end
district.rb
class District < ActiveRecord::Base
has_and_belongs_to_many :leads
end
lead.rb
class Lead < ActiveRecord::Base
has_and_belongs_to_many :districts
end
Join table migration:
class CreateDistrictsLeadsJoin < ActiveRecord::Migration
def change
create_table :districts_leads, :id => false do |t|
t.integer "district_id"
t.integer "lead_id"
end
add_index :districts_leads, ["district_id", "lead_id"]
end
end
My error was in mass assigning my params (used :district should have been :district_ids)
Correct params function:
# leads_controller.rb
private
def lead_params
params.require(:lead).permit(:first_name, :last_name, :phone_number, :email, :move_in_date, :beds, :baths, :maxprice, :broken_lease, :felon, :pets, :pets_description, :district_ids => [])
end
You will need to add leads to district and districts to leads in both create methods.
def create # in LeadsController
#lead = Lead.new(lead_params)
if #lead.save
district = District.find(...) #find district to associate
district.leads << #lead #add lead to district
redirect_to(:action => 'index')
else
render('new')
end
end
#leads_controller.rb
def create
#lead = Lead.new(lead_params)
if #lead.save
selected_dis = params['lead_district_ids'] #[3,4,5]
districts = District.where(id: selected_dis).first
#check how you getting district params and use that to fetch orders.
districts.each do |d|
#lead.districts << d
#assumming your one lead can have many districts.
end
redirect_to leads_path
else
render :new
end
end

Rails 'stringify_keys'

(sorry for my english)
I'm trying my first rails web app.
Here is my code:
Model:
class Movie < ActiveRecord::Base
attr_accessible :about, :title, :url
end
Controller:
class MoviesController < ApplicationController
def show
#movie = Movie.find(params[:id])
end
def update
#mov = Movie.find(params[:id])
if #mov.update_attributes(params[:id])
redirect_to movies_path, :notice => "Your movie has been updated."
else
render "show"
end
end
def destroy
end
end
and the View
<%= form_for #movie do |m| %>
<center>
<table>
<td valign='middle' align='right'>
Title:
</td>
<td>
<%= m.text_field :title %>
</td>
<tr>
<td valign='middle' align='right'>
Description:
</td>
<td>
<%= m.text_area :about %>
</td>
<tr>
<td valign='middle' align='right'>
URL:
</td>
<td>
<%= m.text_field :url, :id => "url" %><input type='file' onchange="document.getElementById('url').value = this.value" />
</td>
<tr>
<td>
</td>
<td>
<center>
<%= m.submit %>
</center>
</td>
</table>
</center>
<% end %>
so this is an update action and this give an error message after press the update form button
error is:
undefined method `stringify_keys' for "15":String
so I try again with Movie.find(params[:id].to_i) and the output is:
undefined method `stringify_keys' for 15:Fixnum
thanks for answers, have a good day!=)
if #mov.update_attributes(params[:id])
Its because of the above line. Here you are trying to update the #mov attribute with the params[:id] which is wrong.
the update action in the controller should be as follow.
def update
#mov = Movie.find(params[:id])
if #mov.update_attributes(params[:movie])
redirect_to movies_path, :notice => "Your movie has been updated."
else
render "show"
end
end
Basically the update_attributes will check for hash as a parameter like below.
#mov.update_attributes(:about => "Sample About", :title => "Sample Title", :url => "Sample URL")

Resources