I have a problem with this code .
This is the controller :
def create
#mean = TrasmissionMean.new(trasmission_mean_params)
if #mean.save
flash[:success] = "Mezzo di Trasmissione #{#mean.description}"
else
flash[:error] = "Error #{#mean.errors.full_messages}"
render :action => 'new'
end
end
def trasmission_mean_params
params.require(:mean).permit(:description)
end
I have a problem with this code .
This is the view , with render the form:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<%= form_for #mean, :html => {:class => "form-horizontal" },:url => url_for(:controller => "trasmission_means", :action => "create" ) do |f| %>
<hr>
<%= render partial: "form", :locals => { :#mean => #mean, :f => f } %>
<hr>
<%= f.submit "Aggiungi", class: 'btn btn-success btn-lg pull-right' %>
<% end %>
</div>
</div>
the render form :
<div class="form-group">
<div class="col-lg-12">
<%= f.text_area :description, :rows => 4, class: 'form-control ', placeholder: 'Descrizione' %>
</div>
</div>
and this is the error . I do not understand where the problem is
ActionController::ParameterMissing in Protocol::TrasmissionMeansController#create
param is missing or the value is empty: mean
The parameters are based on the class
def trasmission_mean_params
params.require(:mean).permit(:description)
end
Is looking for mean in the params hash but your class is called TransmissionMean which means it should be
def trasmission_mean_params
params.require(:transmission_mean).permit(:description)
end
Related
i am having 2 models where invoice_details has_many multiple_goods and multiple_goods belongs_to invoice_details.
I am having a condition where when I click on new I have to show the form of multiple_goods as a pop-up in invoice_details_show.
invoice_details/show.html.erb:
<div><%= link_to 'New Person', '#new_person_modal', 'data-toggle' => 'modal' %></div>
<%# Bootstrap modal markup. #see: http://getbootstrap.com/javascript/#modals %>
<div class="modal fade" id="new_person_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Create new person</h4>
</div>
<div class="modal-body">
<%# Render the new person form (passing modal => true to enable remote => true) %>
<%= render 'multiple_goods/form', modal: true %>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
multiple_goods/_form.html.erb:
<%=form_for([:invoice_detail,#multiple_good], html: {class: 'form-horizontal', role: 'form' }) do |f| %>
<% if #multiple_good.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#multiple_good.errors.count, "error") %> prohibited this multiple_good from being saved:</h2>
<ul>
<% #multiple_good.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<br/>
<div class="field">
<%= f.label :description_of_goods1, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :description_of_goods1, :class => 'text_field', :required => true,:maxlength => 20, :placeholder => '20 Alpha numeric characters' %>
</div>
</div>
<div class="field">
<%= f.label :quatity1, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :quatity1, :class => 'text_field', :required => true,:maxlength => 20, :placeholder => 'Enter quatity' %>
</div>
</div>
<div class="field">
<%= f.label :price_per_unit1, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :price_per_unit1, :class => 'text_field', :placeholder => 'Enter price Per unit' %>
</div>
</div>
<div class="field">
<%= f.label :total_amount1, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :total_amount1, :class => 'text_field', readonly: true, :placeholder => 'This field is auto saved' %>
</div>
</div>
<div class="form-actions2"style="text-align:center">
<%= f.submit :class => 'btn btn-primary' %>
</div>
</div>
<% end %>
Finally my controller:
class MultipleGoodsController < ApplicationController
before_action :set_multiple_good, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#multiple_goods = MultipleGood.all
respond_with(#multiple_goods)
end
def show
respond_with(#multiple_good)
end
def new
#multiple_good = MultipleGood.new
respond_with(#multiple_good)
end
def edit
end
def create
#invoice_detail = InvoiceDetail.find(params[:invoice_detail_id])
#multiple_good = #invoice_detail.multiple_goods.create(multiple_good_params)
redirect_to invoice_detail_path(#invoice_detail)
end
def update
#multiple_good.update(multiple_good_params)
respond_with(#multiple_good)
end
def destroy
#multiple_good.destroy
respond_with(#multiple_good)
end
private
def set_multiple_good
#multiple_good = MultipleGood.find(params[:id])
end
def multiple_good_params
params.require(:multiple_good).permit(:description_of_goods1, :quatity1, :price_per_unit1, :total_amount1)
end
end
My invoice Details controller
class InvoiceDetailsController < ApplicationController
before_action :set_invoice_detail, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
#invoice_details = InvoiceDetail.all
respond_with(#invoice_details)
end
def show
#invoice_detail = InvoiceDetail.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #invoice_detail }
format.pdf { render :layout => false }
end
end
def new
#invoice_detail = InvoiceDetail.new
#invoice_detail_attachment = #invoice_detail.invoice_detail_attachments.build
end
def edit
end
def create
#invoice_detail = InvoiceDetail.new(invoice_detail_params)
respond_to do |format|
if #invoice_detail.save
params[:invoice_detail_attachments]['avatar'].each do |a|
#invoice_detail_attachment = #invoice_detail.invoice_detail_attachments.create!(:avatar => a, :invoice_detail_id => #invoice_detail.id)
end
format.html { redirect_to #invoice_detail, notice: '' }
else
format.html { render action: 'new' }
end
end
end
def update
#invoice_detail.update(invoice_detail_params)
if #invoice_detail.save
params[:invoice_detail_attachments]['avatar'].each do |a|
#invoice_detail_attachment = #invoice_detail.invoice_detail_attachments.create!(:avatar => a, :invoice_detail_id => #invoice_detail.id)
end
redirect_to invoice_details_path
end
end
def destroy
#invoice_detail.destroy
redirect_to invoice_details_path
end
def download
require 'zip/zip'
require 'zip/zipfilesystem'
#invoice_detail = InvoiceDetail.find(params[:invoice_id])
t = Tempfile.new('tmp-zip-' + request.remote_ip)
Zip::ZipOutputStream.open(t.path) do |zos|
#invoice_detail.invoice_detail_attachments.each do |file|
zos.put_next_entry(file.invoice_detail)
zos.print IO.read(file.avatar.path)
end
end
send_file t.path, :type => "application/zip", :filename => "#{#invoice_detail.invoice_number}.zip"
t.close
end
private
def set_invoice_detail
#invoice_detail = InvoiceDetail.find(params[:id])
end
def invoice_detail_params
params.require(:invoice_detail).permit(:avatar,:attachment,:invoice_number, :supplier_name, :invoice_date, :invoice_due_date, :description_of_goods, :quatity, :price_per_unit, :total_amount, :mode_of_payment, :status, :shipping_country, :sl_no, :containers, :net_weight, :bl_number, :bl_date, :insurance_provider, :insurance_amount, :insurance_status, :coo_payment, :farworder, :farworder_inv_amt_doller, :farworder_inv_amt_idr, :payment_status_to_farworder,:final_amount )
end
end
When I am running on server, i am getting error as :
ArgumentError in InvoiceDetails#show
First argument in form cannot contain nil or be empty
Your action show in InvoiceDetailsController doesn't include a new multiple_goods for the _form, you should take into account that render in views only render a view doesn't pass throught another action.
def show
#invoice_detail = InvoiceDetail.find(params[:id])
#multiple_good = MultipleGood.new
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #invoice_detail }
format.pdf { render :layout => false }
end
end
I am not getting exact solution for finding the strong params for given parameters.
Please help me on this
"service"=>{"1"=>{"client_id"=>"testid", "client_secret"=>"testsecret"}, "2"=>{"client_id"=>"testkey", "client_secret"=>""}, "3"=>{"client_id"=>"", "client_secret"=>""}}
I tried
def service_params
params.require(:service).permit(:id, :client_id, :client_secret)
end
I am getting error
Unpermitted parameters: 1, 2, 3
EDIT:
my form is
<%= form_for :service, :url => update_config_path, :html => { :class => "form-horizontal", :method => "put", :remote => true } do %>
<% #services.each do |s| %>
<%= fields_for "service[]", s do |service_field| %>
<fieldset>
<legend><%= s.name %></legend>
<div class="form-group">
<%= service_field.label :client_id, "Consumer Key", :class => "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= service_field.text_field :client_id, :class => "form-control" %>
</div>
</div>
<div class="form-group">
<%= service_field.label :client_secret, "Consumer Secret", :class => "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= service_field.text_field :client_secret, :class => "form-control" %>
</div>
</div>
</fieldset>
<% end %>
<% end %>
<%= submit_tag %>
<% end %>
I haven't tested it, but something like this might work:
def service_params
params.require(:service).map do |_, p|
p.permit(:id, :client_id, :client_secret)
end
end
Something like this could work:
def service_params
params.require(:service).permit.tap do |whitelisted|
whitelisted["1"] = params["1"]
whitelisted["2"] = params["2"]
whitelisted["3"] = params["3"]
end
end
Validation for the create action are not working, I have made validation for the field to be present, but if I keep the fields empty and press submit, I get routing error, if I fill in the complete fields, It works perfectly fine. Also, the validation works perfectly fine for the update action.
Here is the view:
<%= stylesheet_link_tag 'gmaps4rails' %>
<%= form_for #estate, :html => { :class => 'form-horizontal',:multipart => true } do |f| %>
<% if #estate.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#estate.errors.count, "error") %> prohibited this estate from being saved:</h2>
<ul>
<% #estate.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<script>
function enableDisable(bEnable, textBoxID)
{
document.getElementById(textBoxID).disabled = !bEnable
}
</script>
<div class="control-group">
<%= f.label :Name, "Property/Tenant Name", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :Name, :class => 'text_field', :placeholder => "e.g., Saxbys Coffee" %>
</div>
</div>
<div class="control-group">
<%= f.label :Address, :class => 'control-label' %>
<div class="controls">
<%= f.text_area :Address, :class => 'text_area', :cols => '10', :rows => '10', :placeholder => "e.g., 1236 36th Street NW Washington, DC 20007" %>
</div>
</div>
<div class="control-group">
<%= f.label :asset, "Upload Picture", :class => 'control-label' %>
<div class="controls">
<%= f.file_field :asset %>
</div>
</div>
<% if #estate.asset.present? %>
<div class="control-group">
<%= f.label "Delete Existing Picture", :class => 'control-label' %>
<div class="controls">
<%= f.check_box(:delete_asset) %>
</div>
</div>
<% end %>
<!-- <div class="control-group">
<%= f.label :Mgmt, "Would you like to share this property with your Real Estate Management Company?", :class => 'control-label' %>
<div class="controls">
<input type="checkbox" id="chkbox" onchange="document.getElementById('txtBox').disabled=!this.checked;" checked="checked" />
</div>
</div> -->
<% if current_user.Company.nil? %>
<div id="flip"><a>Would you like to share this property with your Management Company?</a></div>
<br />
<div id="panel">
<div class="control-group">
<%= f.label :Mgmt, "Company Name", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :Mgmt, :class => 'text_field', :id => 'txtBox'%>
</div>
</div>
<div class="control-group">
<%= f.label :companyemail, "Company Email", :class => 'control-label' %>
<div class="controls">
<%= f.text_field :companyemail, :class => 'text_field', :id => 'txtBox'%>
</div>
</div>
</div>
<% end %>
<div class="form-actions">
<% if current_page?(controller:"estates", action:"edit", :id => params[:id] || 0)%>
<%= f.submit "Update Property Details", :class => 'btn btn-info' %>
<% else %>
<%= f.submit "Upload Property Details", :class => 'btn btn-info' %>
<% end %>
<% end %>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
</script>
Here is the controller:
def create
# #estate = Estate.new(params[:estate])
if current_user.Company.nil?
#estate = current_user.estates.build(params[:estate])
else
serve = User.find(##key)
#estate = Estate.new(params[:estate])
#estate.user_id = serve.id
#estate.Mgmt = current_user.Company
end
respond_to do |format|
if #estate.save
if current_user.Company.nil?
if #estate.companyemail = ''
#
else
EstateMailer.company_confirmation(#estate).deliver
end
end
format.html { redirect_to #estate, notice: 'Property details were successfully updated.' }
format.json { render json: #estate, status: :created, location: #estate }
else
format.html { render action: "new" }
format.json { render json: #estate.errors, status: :unprocessable_entity }
end
end
end
Error message:
No route matches {:action=>"show", :controller=>"estates", :id=>#<Estate id: nil, Name: "", Address: "", created_at: nil, updated_at: nil, user_id: 5, asset_file_name: nil, asset_content_type: nil, asset_file_size: nil, asset_updated_at: nil, Mgmt: "", companyemail: "", latitude: nil, longitude: nil, gmaps: nil>}
routes.rb
resources :feedbacks
root to: 'home#index'
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"},
controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :profiles
resources :estates do
resources :records do
resources :documents
end
end
get 'faq/faqs'
match '/records',to: 'estates#record'
get 'management/index'
match 'management/show', to: 'management#show'
match 'management/showrecord', to: 'management#showrecord'
after a little chat with Hrishikesh Sardar the bug was fixed, the problem was in if block of the create action:
if current_user.Company.nil?
#estate = current_user.estates.build(params[:estate])
else
serve = User.find(##key)
#estate = Estate.new(params[:estate])
#estate.user_id = serve.id
#estate.Mgmt = current_user.Company
end
this line:
#estate = current_user.estates.build(params[:estate])
had to be replaced by:
#estate = Estate.new(params[:estate])
#estate.user_id = current_user.id
somehow current_user.estates.new(params[:estate]) didn't worked, even if the relation between estate and user was built as required.
I'm using Rails3. Now trying to implement follow button in index.html.erb just like twitter.
It shows member list and their follow buttons.
It looks okay but if I press any of those follow button, appearance doesn't change.
It should changes follow to un-follow right away.
I have no idea why it does. But if I reload the page, it shows correct status.
follows_controller.rb
class FollowsController < ApplicationController
def create
#user = User.find(params[:user_id])
current_user.follow(#user)
respond_to do |format|
format.js {render :action=>"create.js"}
end
end
def destroy
#user = User.find(params[:user_id])
current_user.stop_following(#user)
respond_to do |format|
format.js {render :action=>"destroy.js"}
end
end
end
views/users/_follow_user.html.erb
<% unless user == current_user %>
<% if current_user.following?(user) %>
<%= button_to("Un-Follow", user_follow_path(user.to_param, current_user.get_follow(user).id),
:method => :delete,
:remote => true,
:class => 'btn') %>
<% else %>
<%= button_to("Follow", user_follows_path(user.to_param),
:remote => true,
:class => 'btn btn-primary') %>
<% end %>
<% end %>
views/users/create.js.erb
$('.follow_user[data-user-id="<%=#user.id%>"]').html('<%= escape_javascript(render :partial => "follow_user", :locals => {:user => #user}) %>');
#jQuery
views/users/destroy.js.erb
$('.follow_user[data-user-id="<%=#user.id%>"]').html('<%= escape_javascript(render :partial => "follow_user", :locals => {:user => #user}) %>');
#jQuery
views/users/index.html.erb
<%- model_class = User.new.class -%>
<div class="page-header">
<h1><%=t '.title', :default => model_class.model_name.human.pluralize %></h1>
</div>
<% #from %>
<h3>tag cloud</h3>
<% tag_cloud(#tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
<%= link_to tag.name, {:action=>'index', :tag=>tag.name}, :class => css_class%>
<% end %>
<%= paginate #users %>
<table class="table table-condensed">
<thead></thead>
<tbody>
<% #users.each do |user| %>
<div class="memberListBox">
<div class="memberList">
<p class="name"><span><%= user.user_profile.nickname %></span>(<%= user.user_profile.age %>)</p>
<p class="size"><%= user.username %></p>
<p class="img">
<% if user.user_profile.user_avatar? %>
<%= image_tag(user.user_profile.user_avatar.url(:thumb),:height => 100, :width => 100, :class => 'img-polaroid' ) %>
<% else %>
<%= image_tag('nophoto.gif',:height => 100, :width => 100, :class => 'img-polaroid' ) %>
<% end %>
</p>
<div class="introduction">
<%= user.user_profile.introduction %>
</div>
<% if user_signed_in? && current_user!=user %>
<div id="follow_user">
<%= render :partial => "follow_user", :locals => {:user => user} %>
</div>
<% end %>
<%= link_to sanitize('<i class="icon-pencil icon-white"></i> ') + 'Message', new_messages_path(user.username), :class => 'btn btn-primary' %>
<%= link_to sanitize('<i class="icon-user icon-white"></i> ') + 'Profile', show_user_path(:username => user.username, :breadcrumb => #from), :class => 'btn btn-info' %>
</div>
</div>
<% end %>
</tbody>
</table>
response content
$('#follow_user').html(' <form action=\"/users/1/follows\" class=\"button_to\" data-remote=\"true\" method=\"post\"><div><input class=\"btn btn-primary\" type=\"submit\" value=\"Follow\" /><input name=\"authenticity_token\" type=\"hidden\" value=\"G/taOUeWy2gumhWUi10cPvECAmYLQdhQ2/eGGMJwvPE=\" /><\/div><\/form>\n');
Add the user id to the attribute data-user-id and add the class follow_user
<div class="follow_user" data-user-id="<%= user.id %>">
<%= render :partial => "follow_user", :locals => {:user => user} %>
</div>
My Rails application just wont add any class to fields with errors. Cant find wihat is the problem.
Got this in model:
validates_presence_of :name
validates_uniqueness_of :name
validates_presence_of :phone
Any ideas where to start looking for solutions ?
This is the view erb file which does not generate the required styled class:
<%= form_for :company, :url => {:action => 'create_lead'}, :html => {:class => "form-horizontal"} do |f| %>
<div class="">
<div class="span2">
<%= f.label :csdd_nr, "CSDD numurs" %>
<%= f.text_field :csdd_nr, {:class => "input-small"} %>
</div>
<div class="span4">
<%= f.label :name, "Nosaukums" %>
<%= f.text_field :name %>
</div>
<div class="span6">
<%= f.label :ap_veh_count, "Auto skaits" %>
<%= f.text_field :ap_veh_count, {:class => "input-small"} %><br /><br />
</div>
<div class="span6">
<%= f.label :office_adress_street, "Faktiskā adrese" %>
<%= f.text_field(:office_adress_street, {:placeholder => 'Iela', :class => "input-medium"}) %> <%= f.text_field(:office_adress_city, {:placeholder => 'Pilsēta', :class => "input-small"}) %> <%= f.text_field(:office_adress_postcode, {:placeholder => 'Pasta indekss', :class => "input-small"}) %>
</div>
<div class="span4">
<%= f.label :web, "Mājaslapa" %>
<%= f.text_field :web %><br /><br />
</div>
<div class="span4">
<%= f.label :phone, "Telefona numurs" %>
<%= f.text_field :phone %>
</div>
<div class="span4">
<%= f.label :email, "E-pasts" %>
<%= f.text_field :email %>
</div>
<div class="span4">
<%= f.label :company_field, "Uzņēmuma nodarbošanās" %>
<%= f.text_field :company_field %><br /><br />
</div>
<%= f.hidden_field(:company_status, :value => "3") %>
<div class="span12">
<br /><br />
<%= submit_tag("Saglabāt", :class => 'btn btn-primary') %>
<%= link_to "Atcelt", {:action => 'list_leads'}, :class => 'btn' %>
</div> def new_lead
#company = Company.new
end
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
redirect_to(:action => 'new_lead')
end
end
</div>
<% end %>
OK, and here is the controller which saves the data to database:
def new_lead
#company = Company.new
end
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
redirect_to(:action => 'new_lead')
end
end
This happens because you're redirecting instead of rendering, when there is a validation error. Your controller should look like:
def create_lead
#company = Company.new(params[:company])
if #company.save
flash[:success] = "Uzņēmums saglabāts"
redirect_to(:action => 'new_lead')
else
flash[:alert] = "!!! Uzņēmums nav saglabāts"
render(:action => 'new_lead')
end
end