I am trying to pass parameters in controller but it is not returning anything.Please help me how I can pass parameters .
Controller Code -
def print_salary_slip_monthwise
#month = params[:salary][:month]
#year = params[:salary][:year]
#company = params[:salary][:company_id]
#company_location = params[:salary][:company_location_id]
#department = params[:salary][:department_id]
#salaryslips = Salaryslip.where(month: #month,year: #year.to_s,employee_id: #salary1)
end
form.html.erb - This is my form in which I used on change event .
<div class="box">
<div class="box-header">
<h3 class="box-title">Salary Slip Department Wise</h3>
</div><!-- /.box-header -->
<div class="box-body">
<%= bootstrap_form_for(:pdf_salaries, url: { action: 'print_salary_slip_monthwise'},html: {id: 'pdf_salaries'},remote: true ) do |f| %>
<div class="row">
<div class="col-sm-2">
<label>Year</label>
<div class="field">
<%= select :salary,:year,['2015','2016','2017','2018','2019','2020','2021','2022','2023','2024','2025','2026','2027'],{label: 'Select Year',include_blank: " Select Year"},{class: 'form-control'} %>
</div>
</div>
<div class="col-sm-2">
<label>Month</label>
<div class="field">
<%= select :salary,:month, ['January','February','March','April','May','June','July','August','September','October','November','December'],{label: 'Select Month',include_blank: " Select Month"}, class: "form-control" %>
</div>
</div>
<div class="col-sm-2">
<div class="form-group required">
<div class="input-group">
<%= f.select :company_id, all_company,{include_blank: "Select Company"},{onchange:"var a={id:$(this).val(), form : 'employee'}; $.get('/employees/collect_company_location',a,function(response){});"}%>
</div>
</div>
</div>
<div class="col-sm-2">
<div class="form-group required">
<div id="company_location">
<%= render 'employees/company_location_dropdown' %>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group required">
<div class="input-group">
<div id="department">
<%= render 'employees/department_dropdown' %>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-2">
<div class="actions">
<%= f.submit "Display Report", class: "btn btn-sm btn-primary",id: "buttonCtc" %>
</div>
</div>
</div>
<div class="ajax-progress"></div>
<div id="employee_list_pdf"></div>
</div>
</div>
<%end%>
You can pass parameters using form or using ajax. Once you pass a parameters to your server (controller) you can check it to your server log to see the parameters. In rails 4 you need to use Strong Parameters to save these parameters to your database.
Take this as an example of the Form will below field.
<%= simple_form_for #user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
When above form will be submitted. you can see the rails server terminal for params
Parameters: {"user"=>{"username"=>"user1", "password"=>"password"}, "commit"=>"Submit"}
Access params in controller like this:
params[:user][:username] # code in controller
Hope this give you the clear understaning.
Related
I'm new to ruby on rails.
I've two type of users Admin and User. Admin can create edit and destroy user. I've no problem with create and destroy and have implemented quite easily.
I've used following code in controller,
private
def allowed_params
params.require(:user).permit(:role_id, :email, :password, :password_confirmation, :fname, :lname)
end
to define parameters required and allowed while creating user.
User Create
def save_user
#user = User.new(allowed_params)
if #user.save
redirect_to list_users_path
else
render 'add_user'
end
end
User Update
def update_user
#user = User.find(params[:id])
if #user.update_attributes(allowed_params)
redirect_to list_users_path
else
render 'edit_user'
end
end
View
<div class="container">
<div class="row">
<div class="col-md-12">
<%= link_to "Admin Dashboard", admin_dashboard_path, :class=>'pull-left' %>
<%= form_for #user, :url => update_user_path do |f| %>
<div class="row">
<div class="col-md-12 field">
<div class="col-md-3">
<h2 style="text-align: center;">Edit User Details</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 field">
<div class="col-md-3">
<%= f.label :fname, 'First Name' %><br />
<%= f.text_field :fname, :class => 'form-control' %>
<p class="error"><%= show_errors(#user, :fname) %></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 field">
<div class="col-md-3">
<%= f.label :lname, 'Last Name' %><br />
<%= f.text_field :lname, :class => 'form-control' %>
<p class="error"><%= show_errors(#user, :lname) %></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 field">
<div class="col-md-3">
<%= f.label :email, 'Email' %><br />
<%= f.text_field :email, :class => 'form-control' %>
<p class="error"><%= show_errors(#user, :email) %></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 field">
<div class="col-md-3" style="text-align: center">
<%= f.submit "Update User", :class => 'btn btn-sm btn-primary' %>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
when I pass only fname, lname and email nothing happened. I need to update user details what should I do here ?
Any suggestion is appreciated.
There is a debug way.
You should launch the server in development mode. Then check the changes of development.log file in the terminal by tail -f log/development.log.
Then do what you want, such as click the "submit" button when you changed the fname or other fields.
Then focus on the informations got on step 1. There are what you can get:
A. which Controller and action handled the request
B. The parameters of this request.
C. The log of SQLs excuted by DB
D. The response status
Check if the right Controller? or right parameters? or SQLs has been excuted? In addition, we used to #user.update_attributes!(allowed_params) when debug. It take more informations when something disobeys the rules defined in Model(User).
In my edit form I have six file_fields for uploading images with carrier wave, they were not populated with previous images, if forgot to select images again in edit form, they were updating with nil values.
<%= f.fields_for :gallery, #gallery do |gf| %>
<div class="gallery-uploads">
<div class="form-group row">
<div class="col-md-6">
<%= gf.label :img1 %>
<%= gf.file_field :img1, {class: 'form-control'} %>
</div>
<div class="col-md-6">
<%= gf.label :img2 %>
<%= gf.file_field :img2, {class: 'form-control'} %>
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<%= gf.label :img3 %>
<%= gf.file_field :img3, {class: 'form-control'} %>
</div>
<div class="col-md-6">
<%= gf.label :img4 %>
<%= gf.file_field :img4, {class: 'form-control'} %>
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<%= gf.label :img5 %>
<%= gf.file_field :img5, {class: 'form-control'} %>
</div>
<div class="col-md-6">
<%= gf.label :img6 %>
<%= gf.file_field :img6, {class: 'form-control'} %>
</div>
</div>
</div>
<% end %>
In controller update action as follows
def update
#product = Product.find(params[:id])
if #product.update(product_params)
redirect_to product_path
end
end
how to keep the previous images if they were already uploading in edit form(in update)
You can probably try setting the value on the file_field. If that does not work you can use a hidden_field.
I am trying to initiate a variable showName when the page loads. It worked fine when I did it using just angular but when I implemented it with rails(with embedded ruby tags) I am not able to hide the text field and save button as my variable gets undefined. I have tried couple of ways and ended with no result.
Here is my code. Any help is really appreciated. Thanks.
Edit.html.erb:
<div class="row" ng-controller="EditUserCtrl">
<div class="col-md-6 col-md-offset-3" id="editForm">
<%= form_for(#user) do |f| %>
<h1 id="editHeader">Account Information </h1>
<%= render 'shared/error_messages' %>
<div class="form-group">
<%= f.label :name, "Full Name:" %><br>
<div class="row">
<div class="col-sm-7">
<div ng-hide="showName">
<%= f.label :name, #user.name , id:"editFields" %>
</div>
<%= f.text_field :name, { :'ng-show' => 'showName'}%>
</div>
<div class="col-sm-5" ng-show="showName">
<%= f.button "Save", :action => "update_name" , :method => :put, class: "btn btn-primary", :'ng-click' => "clicked()", id:"editAccount" %>
</div>
<div class="col-sm-5" ng-hide="showName">
<i id="nav-cart-count" class="glyphicon glyphicon-edit"></i> Edit
</div>
</div>
</div>
<% end %>
</div>
Use ng-init to initialize the variable
<div class="row" ng-controller="EditUserCtrl" ng-init="showName = true">
I've declared an STI on Works
class Work < ActiveRecord::Base
validates :title, presence: true
validates :visibility, presence: true
belongs_to :category
end
class Story<Work
end
class Poem<Work
end
Code for form:
<div class="panel panel-blue margin-bottom-40">
<div class="panel-heading">
<h3 class="panel-title"><i class="icon-tasks"></i> Item Details</h3>
</div>
<div class="panel-body">
<%= form_for(#work, :html =>{:class =>"margin-bottom-40 new_item ",:multipart => true}) do |f| %>
<% if #work.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#work.errors.count, "error") %> found </h2>
<ul>
<% #work.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label "Title",:class=>"control-label" %>
<div class="controls">
<%= f.text_field :title,:class=>"form-control",:placeholder=>"Title of the story/poem" %>
<div class="description">
<div class="">
<strong>Enter a suitable title for your story/poem
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :type,:class=>"control-label" %>
<div class="controls">
<%= f.select :type,{"Story"=>"Story","Poem"=>"Poem"},{},:class=>"form-control",:placeholder=>"Story/Poem" %>
<div class="description">
<div class="">
<strong>Is it a story or a poem?
</strong>
</div>
</div>
</div>
</div>
<%
visibilities=["Me Only","Anyone with link","Logged In Users","Everyone"]
%>
<div class="form-group">
<%= f.label :visibility,:class=>"control-label" %>
<div class="controls">
<% [ 3,2, 1, 0 ].each do |option| %>
<br><%= radio_button_tag :visiblity, option, #visibility == option %>
<%= label_tag "visibility_#{option}", visibilities[option].humanize %>
<% end %>
<div class="description">
<div class="">
<strong>
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :category_id,:class=>"control-label" %>
<div class="controls">
<%= f.collection_select :category_id,Category.all,:id,:name,{},:class=>"form-control" %>
<div class="description">
<div class="">
<strong>
Type your story/Poem here
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :content,:class=>"control-label" %>
<div class="controls">
<%= f.text_area :content,:class=>"form-control" %>
<div class="description">
<div class="">
<strong>
</strong>
</div>
</div>
</div>
</div>
<div class="actions">
<%= f.submit :class=>"btn btn-primary" %>
<%= link_to 'Back', works_path,:class=>"btn" %>
</div>
<% end %>
I have scaffolding for work with the parameter Type which accepts Story and Poem. When I submitted the form initially, I would get a stories_path not found error so I mapped stories and poems paths as follows:
resources :stories, :controller => 'works'
resources :poems, :controller => 'works'
So when I submit the first time, the form shows validation errors. When I submit it again after correcting validation, I get an exception
ActionController::ParameterMissing in WorksController#create
in this line:
params.require(:work).permit(:title, :type, :visibility, :category_id, :content)
I assume this is because its now renaming all the fields to story[] instead of work[]
How do I fix this and in the greater picture, how do I make STI work on the same controller. I just want to be able to use Poem.all and Story.all for fetching records.
Submisson currently works through the same interface
I am allowing the user to select the type of work in the form.
I have this multiple select list which gives me an error Wrong number of Arguments ( 3 for 2 ) in a form_form a "Topic" model which has_many :curriculums, :through => another_model
<%= f.select :curriculum_ids, options_from_collection_for_select(Curriculum.all, :id, :name, #topic.curriculum_ids), {}, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>
However this code works just fine in another controller's view :
Here we have a Question model with has_and_belongs_to_many :skills
<%=f.select :skill_ids, options_from_collection_for_select(Skill.all, :id, :name, #question.skill_ids),{},{"data-placeholder"=>"Select Other Skills",:multiple=>true,:class=>"chzn-select"}%>
Here is the complete _form.html.erb :
<div class="row-fluid attributeContainer">
<div class="span12">
<% if #topic.errors.any? %>
<% #topic.errors.full_messages.each do |msg| %>
<div class="alert alert-error">
<button class="close" data-dismiss="alert">×</button>
<strong>Error!</strong> <%= msg %>
</div>
<% end %>
<% end %>
<div class="widget-box">
<div class="widget-title">
<span class="icon">
<i class="icon-align-justify"></i>
</span>
<h5>Topics</h5>
</div>
<div class="widget-content nopadding">
<%= form_for(#topic,:html=>{:class=>"form-horizontal"}) do |f| %>
<div class="control-topic">
<label class="control-label">Topic Name</label>
<div class="controls">
<%= f.text_field :name %>
</div>
</div>
<div class="control-group">
<label class="control-label">Description</label>
<div class="controls">
<textarea name="topic[description]" value="<%=#topic.description%>"><%=#topic.description%></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label">Subject</label>
<div class="controls">
<div class="span3">
<%= f.collection_select(:subject_id, Subject.all, :id, :name, {:include_blank => 'Please Select Subject'}) %>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Curriculum</label>
<div class="controls">
<div class="span3">
<%= f.select :curriculum_ids, options_from_collection_for_select(Curriculum.all, :id, :name, #topic.curriculum_ids), {}, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>
</div>
</div>
</div>
<div class="control-group attributeTemplate2" style="display:none;">
<label class="control-label">Concept Tag</label>
<div class="controls" >
<div class="span3" >
<%= f.collection_select(:name, Concept.all,:id, :name,{:include_blank => 'Please Select'},{:name=>"topic[concept][][concept_id]"}) %>
</div>
<div class="span2">
<%= f.text_field :name, :style=> "margin-left:25px;", :class=>'text_field', :name=>'topic[concept][][concept_sequence]', :placeholder=>'concept_sequence', :size=>'30'%>
</div>
<div>
<a style="margin-left:25px;" class="btn btn-danger removeRow"><i class="icon-white icon-remove-sign"></i></a>
</div>
</div>
</div>
<div class="control-group ">
<label class="control-label">Concept Tag</label>
<div class="controls" >
<div class="span3" >
<%= f.collection_select(:name, Concept.all,:id, :name,{:include_blank => 'Please Select'},{:name=>"topic[concept][][concept_id]",:class=>"chzn-select"}) %>
</div>
<div class="span2">
<%= f.text_field :name, :style=> "margin-left:25px;", :class=>'text_field', :name=>'topic[concept][][concept_sequence]', :placeholder=>'concept_sequence', :size=>'30'%>
</div>
<div>
<a style="margin-left:25px;" class="btn btn-danger removeRow"><i class="icon-white icon-remove-sign"></i></a>
</div>
</div>
</div>
<div class="control-group addTopicConcepts">
<div class="controls" >
<div><a class="btn btn-primary">Add Concept</a></div>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
</div>
<%end%>
</div>
</div>
</div>
</div>
CODE FIXED :
Apparently the join model had code :
belongs_to :curriculum , :grade, :topic
Changing to the code fixed everything.
belongs_to :curriculum
belongs_to :grade
belongs_to :topic
Why not use collection_select?
http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select
When used in form_for parameters are as follows
first parameter is the name of the field you wish to record the value to (e.g. :curriculum_id)
second parameter is the model you wish to collect items from (e.g. Curriculum.all)
third parameter is the field you want to record the value from (e.g. :id)
fourth parameter is the field you wish to display as the value (e.g. :name)
so for your example
<%= f.collection_select :curriculum_id, Curriculum.all, :id, :name, { "data-placeholder"=>"Select Curriculum", :multiple=>true , :class=>"chzn-select"} %>
should approximate what you want, though you may need to tweak it.