Searching in Rails - ruby-on-rails

I have two models: Inbox and Equipments. They have has_many relation :through => :inbox_equipments
In Inbox view I have the next code
<%= form_for #inbox, :html => { :multipart => true } do |f| %>
<%= f.text_area :number, placeholder: "Enter inbox number" %>
<% Equipment.all.each do |equipment| %>
<%= check_box_tag :equipment_ids, equipment.id, #inbox.equipments.include?(equipment), :name => 'inbox[equipment_ids][]' %>
<%= equipment.name %>
<% end %>
<%= f.submit "Submit", class: "btn btn-large btn-primary" %>
<% end %>
I need text area for find (or filter) desired value of equipment in check_box_tag
Like this:
<%= form_tag equipment_index_path, :method => 'get', :id => "equipments_search" do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Find", :name => nil, class: "btn btn-small btn-primary" %>
Help, please.

Although your description was not the best, I think you're looking for ajax
I would do this:
Call From Ajax
Ajax will allow you to pull the other form from your Rails app as a partial, appending it as required. This will give you the ability to treat the form as an asynchronous request, removing any issue from the form itself
Here's what I'd do:
#app/assets/javascripts/application.js
$("input[type=checkbox]").on("change", function() {
$.ajax({
url: "controller/search",
data: $("input[name=inbox[equipment_ids]").serialize(),
error: function(data) {
alert(data)
}
});
});
#config/routes.rb
match 'search(/:search)', :to => 'controller#search', :as => :search, via: [:get, :post]
#app/controllers/your_controller.rb
def search
#results = Model.where(:id => params[:equipment_ids])
respond_to do |format|
format.js
end
end
#app/views/your_controller/search.js.erb
$("form").append(data)
This won't work out of the box, but it will give you some ideas as to what you can do

Related

Routing error - No route matches [POST]

Have read many questions / answers on this issue, yet do not seem to find my fix.
Here is the issue: I am following the getting started for Rails to create a simple register of annotations. My forms work - can add new & update annotations. Yet when I add links to the index, I get a routing error:
This: <%= button_to "Details", annotation_path(annotation), :class => "btn btn-primary btn-xs"%> results in: No route matches [POST] "/annotations/5"
This: <%= button_to "Add Annotation", new_annotation_path, :class => "btn btn-primary btn-xs"%> to No route matches [POST] "/annotations/new"
Thanks for help
Routes.db:
Rails.application.routes.draw do
root 'dashboard#index'
devise_for :users
resources :users, :annotations
Controller:
class AnnotationsController < ApplicationController
def index
#annotations = Annotation.all
end
def show
#annotation = Annotation.find(params[:id])
end
def new
#annotation = Annotation.new
end
def edit
#annotation = Annotation.find(params[:id])
end
def create
#annotation = Annotation.new(annotation_params)
#annotation.save
redirect_to #annotation
end
def update
#annotation = Annotation.find(params[:id])
if #annotation.update(annotation_params)
redirect_to #annotation
else
render 'edit'
end
end
def destroy
#annotation = Annotation.find(params[:id])
#annotation.destroy
redirect_to annotations_path
end
private
def annotation_params
params.require(:annotation).permit(:name, :description)
end
end
And the form (= partial)
<%= simple_form_for #annotation, url: annotations_path, html: { class: 'form-horizontal' },
wrapper: :horizontal_form,
wrapper_mappings: {
check_boxes: :horizontal_radio_and_checkboxes,
radio_buttons: :horizontal_radio_and_checkboxes,
file: :horizontal_file_input,
boolean: :horizontal_boolean
} do |f| %>
<%= f.error_notification %>
<%= f.input :name, placeholder: 'Enter name' %>
<%= f.input :description, placeholder: 'Description' %>
<%= f.input :file, as: :file %>
<%= f.input :active, as: :boolean %>
<%= f.input :choice, as: :check_boxes,
collection: [
'Option one ...',
'Option two ...'] %>
<%= f.input :documenttype, as: :radio_buttons,
collection: ['Type1', 'Type2'] %>
<%= f.button :submit %>
<% end %>
Note on the form: to no avail, I tried using <%= simple_form_for :annotation, url: annotations_path,
The issue is because the button_to helper actually generates a form at code level and thus is POSTing said form, however the route for a new resource must be a GET.
The button_to tag really should not be used for GET requests so I would use a link_to with CSS classes instead (you already have the necessary classes), but you can do it using the below if you wanted:
<%= button_to "Details", annotation_path(annotation), class: "btn btn-primary btn-xs", method: :get%>
The better approach however is:
<%= link_to "Details", annotation_path(annotation), class: "btn btn-primary btn-xs" %>
Read doc here button_to
The method is by default :post change it using :method param
<%= button_to "Details", annotation_path(annotation), :class => "btn btn-primary btn-xs", method: :get%>
<%= button_to "Add Annotation", new_annotation_path, :class => "btn btn-primary btn-xs", method: :get%>

form_for issue GET request instead of patch

I have edit form for company as bellow in edit.html.erb
<%= form_for([:dashboard , #company] ) do |f| %>
<%= f.text_field :name , :class => "form-control" %>
<%= f.submit "Save" , :class => "btn btn-primary"%>
<% end %>
and my companies_controller.rb
def edit
#company = Company.find(params[:id])
end
def update
# Update code
end
and my routes.rb
namespace :dashboard do
resources :companies , only: [ :edit , :update ]
end
The problem is when submit the form get the bellow error
No route matches [GET] "/dashboard/companies/3"
form_for accepts a post method by default.Here the edit action is get method,so your form_for should look like this
<%= form_for([:dashboard , #company],:html => {:method => :get }) do |f| %>
<%= f.text_field :name , :class => "form-control" %>
<%= f.submit "Save" , :class => "btn btn-primary"%>
<% end %>
OR
You can do like this too
<%= form_for([:dashboard , #company] :url =>edit_dashboard_company_path(#company)) do |f| %>
<%= f.text_field :name , :class => "form-control" %>
<%= f.submit "Save" , :class => "btn btn-primary"%>
<% end %>

Passing model id through rails route in form

I'm calling a custom action with simple_form. I'm having trouble passing the :id parameter to the action.
routes
post '/posts/:id/admin_vote' => 'posts#admin_vote', as: 'admin_vote'
form
<%= simple_form_for :post, url: admin_vote_path(:post_id), :html => {:class => 'form-inline admin-vote-form'} do |f| %>
<%= f.select :vote, 1..20 %>
<%= f.submit 'Vote', :class => 'btn btn-primary btn-xs' %>
<% end %>
partial render
<%= render 'layouts/admin_vote', :locals => { :post => post, :post_id => post.id } %>
For some reason the action receives params[:id] = 'post_id' instead of the actual id.
You're providing :post_id symbol to the admin_vote_path, so it uses that. Change it to:
admin_vote_path(params[:post_id])
or a different parameter depending on the context of your form.
it seems to me your form should be
<%= simple_form_for post, url: admin_vote_path, :html => {:class => 'form-inline admin-vote-form'} do |f| %>
<%= f.select :vote, 1..20 %>
<%= f.submit 'Vote', :class => 'btn btn-primary btn-xs' %>
<% end %>
you should build form based on variable post

Rails: Getting a Value from a Select Tag

<%= form_tag 'select_domain', :url => administer_admin_domain_path(:id), :method => :get do %>
<%= select_tag "id", options_from_collection_for_select(#domains, :id, :caption), :onchange => "this.form.submit();" %>
<% end %>
I want the option selected's id to be the :id inside my form's url when it submits, is this possible?
How about:
<%= form_tag 'select_domain', :url => administer_admin_domains_path, :method => :get do %>
<%= select_tag "id", options_from_collection_for_select(#domains, :id, :caption), :onchange=>"this.form.action=this.form.action + '/' + this.value; this.form.submit(); %>
<% end %>
I didn't test the code. Just giving u some idea which might help. : )

form_for calls model method without submit

I have an index page for admin part of my project
<% #reviews.each do |review| %>
<p><%= review.header %></p>
<p><%= review.body %></p>
<%= form_for [:admin, review] do |f| %>
<%= f.hidden_field :approve %>
<%= f.submit "Approve" %>
<% end %>
<%= form_for [:admin, review] do |f| %>
<%= f.hidden_field :reject %>
<%= f.submit "Reject" %>
<% end %>
<% end %>
where :approve and :reject are public instance methods in Review model.
For some reason, when I load this index page, it automatically calls review.reject method which sets corresponding is_rejected field to true. Same behavior applies to form_for with :approve if I remove form_for with :reject bit.
index action from corresponding controller is very simple
def index
#reviews = Review.all
end
I realize this must be normal behavior, but what I would expect is to call reject method only when I submit corresponding form. Is there a way to fix it? Thank you.
UPDATE
Just for the future reference (including my own): it is easier to use button_to helper for things like that
<%= button_to "Approve", { :action => "update", :id => review.id, :review => { :approve => true } }, :method => :put %>
<%= button_to "Reject", { :action => "update", :id => review.id, :review => { :reject => true } }, :method => :put %>
The form builder is calling approve and reject on your model because it's trying to determine what to set the value for the hidden fields to. One way around this would be to not use f.hidden_field and just create a hidden field that's not tied to your model. You can use hidden_field_tag instead.

Resources