I have a Rails app that includes Requests and Workorders tables.
I have a New Workorder button on the Request show page. I need to pass information from the Request to the Workorder - for instance the Request.id.
I'm currently using flash to do this. Here is the button on the Request show page:
<%= link_to 'New Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => (flash[:request_id] = #request.id %>
In the new Workorder form, I have:
<% if flash[:request_id] != nil %>
<%= f.hidden_field :request_id, :value => flash[:request_id] %>
This works. But, not always. And I haven't been able to figure out why it fails sometimes.
Is there a better way to pass this data?
Thanks for the help!!
UDPDATE1
Sometimes I need to bring forward quite a few data fields. For example:
<%= link_to 'Follow-up Work Order', new_workorder_path, :class => 'btn btn-primary', :onclick => ( flash[:workorder_id] = #workorder.id, flash[:client_id] = #workorder.client_id, flash[:contact_id] = #workorder.contact_id, flash[:location_id] = #workorder.location_id, flash[:type_id] = #workorder.type_id, flash[:woasset_id] = #workorder.woasset_id) %>
You can try passing the parameter to the path of the link and then pass it to the form via your controller's action:
Link:
<%= link_to 'New Work Order', new_workorder_path(request_id: #request.id), :class => 'btn btn-primary' %>
Controller
def new
#request_id = params[:request_id]
...
end
In your view:
<%= f.hidden_field :request_id, value: #request_id %>
Related
I am developing a rails application using the Ransack gem and below is the code that I have written so far to filter my database which works like a charm. Now what I am trying to do is to add additional button like filter options to my index view (where each button has pre-defined filter value). In other words, once the database is first filtered with a brand name, then I would like users to be able to further filter the database by clicking one of the buttons which has a pre-defined filter value of say 'colour = white', then rails will show all the data with the selected brand name and the colour of white).
Controller
class ProjectsController < ApplicationController
def index
#q = Project.ransack(params[:q])
#projects = #q.result(distinct: true)
#projects_count = #q.result.count
#projects = Kaminari.paginate_array(#projects).page(params[:page]).per(30)
end
Index View
<%= search_form_for #q, remote: true, :builder => SimpleForm::FormBuilder do |f| %>
<%= f.input :brand_id_eq, label: false, collection: Brand.all.map{ |f| [f.name, f.id] }, prompt: "All", input_html: { class: "form-control" } %>
<%= f.button :submit, label: "Search", input_html: { class: "btn btn-primary" } %>
<% end %>
...
<span class="data-sort all"><%= link_to "All",q: {color_cont: 'white'}, :class => 'link-sort', :remote => true, :method => :post %></span>
index.js.erb
$('#projects').html('<%= escape_javascript (render partial: 'index') %>').hide().fadeIn('slow');
The problem that I am facing with this approach using the Ransack gem is that when I click the link_to filter button, it does filter the database with the pre-defined filter value of 'white color' however it resets all the previously selected filter options.
Is my approach correct or any better way to achieve this other than using the link_to option?
SOLUTION
I finally got this working using the rail's scope method and a simple jQuery code as shown in my final code below. One thing that I did initially wrong was that I set the name of the scope same as one of my db column name which caused an error. Once I changed the scope name to 'status1', not 'stock_no', it started to work. Hope this helps.
Defined Scope
class ApplicationRecord < ActiveRecord::Base
scope :status1, -> { where( stock_no = "15251" ) }
def self.ransackable_scopes(auth_object = nil)
[:status1]
end
Index.erb
<%= f.hidden_field :status1 %>
<%= f.submit "Stock", :id => "status1", :onclick => "document.getElementById('q_status1').value = 1;", class: 'btn btn-primary status1' %>
Try this question. It is somewhat what you are trying to do, except instead of a submit button, just make yours a button for filtering. It needs to be inside your search_form_for I'm pretty sure as well. And then write a jquery function to submit when the button is clicked like:
$(document).on("turbolinks:load", function(){
$(".data-sort").on('click', function() {
$("form.your-search-form-classname").trigger('submit.rails');
});
});
UPDATE
Try removing the (boolean = true) attribute from the scope. I tested with a similar app of my own and it worked well.
UPDATE 2
I put the following in my app (where status is a column in by db just like your stock_no) and a got the correct query from my database:
<%= f.hidden_field :stock_no %>
<%= f.submit "Stock", :id => "stock_no", :onclick => "document.getElementById('q_stock_no').value = 1;", class: 'btn btn-primary stock_no' %>
scope :stock_no, -> { where( status: 2 ) }
def self.ransackable_scopes(auth_object = nil)
[:stock_no]
end
Are you sure you are putting the scope in the right model?
Replace
<%= link_to "All",q: {color_cont: 'white'}, :class => 'link-sort', :remote => true, :method => :post %>
with
<%= link_to "All",q: {color_cont: 'white', brand_id_eq: params[:q][:brand_id_eq]}, :class => 'link-sort', :remote => true, :method => :post %>
Here assumption is
Once the database is first filtered with a brand name, then I would like users to be able to further filter the database by clicking one of the buttons which has a pre-defined filter value
I have the requirement to have some scopes as clickable links in my application. This will allow the user to change the data they are seeing as required. Using Ransack and it's ransackable_scopes functionality I am very close. I do need to retain any filtering Ransack has done when the user clicks the scope.
I've got the scopes working but now I just need to construct the clickable link.
Here's my model:
class Product < ActiveRecord::Base
scope :upward_trending, -> { where( "status > ?", 100).where(above_revenue_average: true).order('end_date DESC') }
scope :downward_trending, -> { where( "status < ?", 100).order('end_date DESC') }
def self.ransackable_scopes(auth_object = nil)
[:upward_trending, :downward_trending]
end
end
Now in my view I've added these two scopes as hidden fields, like so:
<%= search_form_for #q, :html => {:class => 'filter-form'} do |f| %>
<div>
<%= f.hidden_field :upward_trending %>
<%= f.hidden_field :downward_trending %>
<%= f.label :name_cont, "Search", class: 'label' %>
<%= f.search_field :name_cont, class: 'form-control input-box', :placeholder => 'Search' %>
</div>
<div>
<%= f.submit "Filter", class: 'btn btn-primary' %>
<%= link_to "Clear Search", request.path, class:"btn btn-default" %>
</div>
<% end %>
From here I just need to create the links and it should work.. What is the best way to do this?
Thanks for your help!
I was planning to do dirty way. (But haven't yet)
create search_form_for every scope (you will have 2 form for your case)
set hidden field with own criteria (as you do in your code but each in own form)
make submit button looks like link (with css I think it is not very difficult. You can see Bootstrap button appeared as link)
Not very clean or elegant.
I achieved this by creating hidden fields for each scope and then creating a button with onclick javascript:
<%= f.hidden_field :upward_trending %>
<%= button_tag(:type => 'submit', :class => 'btn btn-primary scope-button upward_trending', :id => "upward_trending", :onclick => "document.getElementById('q_downward_trending').value = 0; document.getElementById('q_upward_trending').value = 1;") do %>
<i class="fa fa-chevron-up fa-2x"></i><br>Upward<br>Trending
<% end %>
<%= text_area_tag :body_html, "", :class =>"required mceEditor", :id=>"txaEditableContent" %>
This is my code. I want this text_area_tag to be assigned to an object(object is note in my case) just like we use "form_for object" so that the :body_html goes straight into params[:note] on submit. How can I do this?
You have got to set first an instance of Note in the controller corresponding action:
#note = Note.new
Then, in your view put this form:
<%= form_for #note do |f| %>
<%= f.text_area :body_html, :class => "required mceEditor", :id => "txaEditableContent" %>
<%= f.submit "Submit" %>
<% end %>
Now when you click the submit button you should get the right post request as you needed.
I have the follwoing form in my view :
I have an instance variable #selected_folder somewhere above in this view
<%= form_for :workflow_selection, :remote => true, :method => "get", :url => {:action => "final_submission"} do |f| %>
<p> Check the workflows needed and then click on the button with folder name</p>
<% #workflow_map[#selected_folder].each do |i| %>
<p><%= f.check_box(i)%> <%= f.label(i, i)%><br /><p>
<% end %>
<br />
<p><%= f.submit #selected_folder%></p>
<% end %>
I want to label the submit button as just 'submit' and should still be able to pass the #selected_folder instance variable to the final_submission action mentioned in the form_for tag
I tried various option like
<%= form_for :workflow_selection, :remote => true, :method => "get", :selected_folder => #selected_folder
:url => {:action => "final_submission"} do |f| %>
i tried to create a select drop down and hide it from view but still trying it to pass once the submit button is clicked.
and some more options..
None of them worked
Please help.
If you want to pass #selected_folder along in the form submission, you can add a hidden_field_tag.
As per Rails documentation:
hidden_field_tag(name, value = nil, options = {})
So in your case
<%= hidden_field_tag 'selected_folder', #selected_folder %>
in the workflow_selection, selected_folder will be present in the form hash.
I know some will think that i should use form_tag with :remote=>true, but i don't know how to render an entire html output....
My problem is the following:
I have this form that sends a collection through 3 comboxes
<%= form_remote_tag :url => report_client_reports_path, :update => :graphic do%>
<%#= form_tag reporte_client_reports_path%>
<p><%= label_tag :supermercados %>
<%=select_tag "supermercados[]", options_from_collection_for_select(#supermercados, "id", "name"),{:multiple=>true, :id => "supermarkets"}%>
</p>
<p><%= label_tag :cortes %>
<%=select_tag "cortes[]", options_from_collection_for_select(#cortes, "corte_real","cuts"),{:multiple=>true, :id => "cortes"}%>
</p>
<p><%= label_tag :productos %>
<%= select_tag "productos[]", options_from_collection_for_select(#productos, "id", "name"),{:multiple=>true, :id => "products"}%>
</p>
<p><%= submit_tag 'Send' %></p>
<%end%>
If i uncommented this line:
<%#= form_tag reporte_client_reports_path%>
It work good and present me the graph, but not the way i expect to work.
I have detected that using form_remote_tag, it sends all vars with their values, bu i dont know why my controller only see one value of each variable.
here is the controller:
#super = params[:supermarkets]
#superm = []
#super.each do |s|
#superm << Company.find(s).abbr
end
#cuts = params[:cuts]
#prods = params[:products]
#cortesGraph = []
#cortess.to_a.each do |c|
#cortesGraph << "#{RawData.find_by_real_cut(c).cuts}"
end
The objects #superm,#cuts and #products arent receving more than 1 value in the array, is a routing problem or a option i have missed in the form_remote_tag?
And update to simplyfy, what actually is still happening is this:
This Works:
<%= form_tag reporte_client_reports_path do%>
This doesn't:
<%= form_tag reporte_client_reports_path,:remote=>true do%>
The problem is that :remote is not sending my params as array it send all data as 1 var :s
I resolved in functional way, but there might be something very weird with this in my controller:
if request.xhr?
#super = params[:supermercados].to_s.split(",")
#cortess = params[:cortes].to_s.split(",")
#prods = params[:productos].to_s.split(",")
end