Say I have two buttons in my model's index view that are used to create a new instance of the model. I want to pass the variable :number to my controller and use it in the new function so I can alter my form slightly depending on which button was pressed. How can I access :number in the controller?
<%= link_to 'New Run 1', new_test_suite_run_path, :class => "btn btn-custom1" , :number => 1 %>
<%= link_to 'New Run 2', new_test_suite_run_path, :class => "btn btn-custom1", :number => 2 %>
Do this instead
<%= link_to 'New Run 1', new_test_suite_run_path(:number => 1), :class => "btn btn-custom1" %>
And then in your controller my_number = params[:number]
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'm following this SE question to put a form in a bootstrap modal with rails.
The person who answered the question states: "Make sure that you have a unique id or class for every modal body.". So I am trying to put a unique id number in my link_to:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "<%= post.id %>-my-modal" %>
But this is causing an error. If I take out <%= post.id %> I do not have an error, but the modal behavior does not work.
How can I add the post.id with embedded ruby to the link to?
You have to write it like this:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Once you open a <% ... %> tag, you are writing ruby code. What this means is that you can't nest <% ... %> tags inside another <% ... %> tag because these tags aren't ruby syntax.
Inside the tag, to do string interpolation, use normal ruby methods:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Only mistake is putting another (ruby) tag <% %> inside the same tag.
In erb (embedded ruby) one <%= %> tag cannot have another tag inside.
This code result a syntax error.
<%= link_to "Edit", .... "data-target" => "<%= post.id %>-my-modal" %>
If you plan to output/render data inside the tag and quoted as string,
used #{put_data_hare}
Output like:
<%= link_to "Edit", .... "data-target" => "#{post.id}-my-modal" %>
In other case, you can do like:
post.id.to_s + "-my-modal"
Now I have 2 forms that submit a comment.
Form Type A
<%=form_for(([#community, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Form Type B
<%=form_for(([#user, #comment]), :remote => true, :class => 'form' ) do |f| %>
<%= f.text_field :body, :id => "body", :class => "chat" %>
<button type="submit" class="btn">submit</button>
<% end %>
Now, I want to have link_to button that functions as same as those forms do if a user clicks it.
When the user click on the link, #comment will be automatically filled just like below.
From Form Type A
#comment = "this is for community"
From Form Type B
#comment = "this is for user"
How can I do that? As far as I understand my situation.
Form is put type, then link_to is get type so it's impossible to re-use transaction of form.
Not sure what you mean by "transaction of form" but if you're asking if you can create/modify data via a single button or link than the answer is Yes, it is possible.
You can actually put with a link_to in rails ({:method => :put} (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
If you want a button to do this you should checkout button_to (http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to)
It's better to use button_to.
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 %>
I would like to be able to add an additional query string parameter on submit that is the same as the value of the classrooms_search_textbox that the user will type. How do I do this?
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
Do I need to add a hidden_tag (and if so, how would I go about doing this?) or can I just add to the classrooms_path somehow?
Thanks!
Since you're sending it your controller first, then you can just manipulate the params in your controller method before sending it off:
params[:classrooms_query] = params[:classrooms_search_textbox]
And then go ahead and use those params to send off to the other service. There's no need to add hidden field tags or use some fancy JS code.
<%= form_tag classrooms_path, :method => :get, :id => "classrooms_search_form" do %>
<%= text_field_tag "classrooms_search_textbox", "Find a classroom" %>
<%= hidden_field_tag "classrooms_query" %>
<%= submit_tag "Find", :id => "classrooms_search_button", :class => "button" %>
$('#classrooms_search_form').submit(function() {
$('#classrooms_query').value($(classrooms_search_textbox.value());
});
That would achieve what you want. Nonetheless, maybe it is in your interest to refactor the controller or view so that it doesn't have this kind of conflicts.