Ransack Search model attributes from different controller - ruby-on-rails

I have a Rails App going here and I am building ransack into it.
I Have Devise installed, but really have no use for a user index page, instead I have a controller named MemberListController with directory method in it and a directory page that links to it. This is where I want my admin users to view the membership directory. (see below for controller)
MemberListController.rb
class MemberListController < ApplicationController
before_action :authenticate_user!
def directory
#users = User.all
#q = User.ransack(params[:q])
#user = #q.result(distinct: true)
respond_to do |format|
format.html
format.xlsx
end
end
end
I built in the following search form to the directory page:
<%= search_form_for #q do |f| %>
<%= f.label :f_name_or_l_name_or_email_or_business_name %>
<%= f.search_field :f_name_or_l_name_or_email_or_business_name %>
<%= f.submit %>
<% end %>
When I submit the form from the directory page (where i want the results to be displayed.. I get the following error:
UsersController#index is missing a template for this request format and variant. request.formats: ["text/html"] request.variant: [] NOTE! For XHR/Ajax or API requests, this action would normally respond with 204 No Content: an empty white screen. Since you're loading it in a web browser, we assume that you expected to actually render a template, not nothing, so we're showing an error to be extra-clear. If you expect 204 No Content, carry on. That's what you'll get from an XHR or API request. Give it a shot.
Im not sure why i twats a user controller or index action as everything is managed through the directory controller.
Please help not sure where I'm going wrong with this.. is there a way to display this out side of the users controller like ransack wants to?

Ransack is smart enough to infer form_url from ransack search object.
So, if you don't pass any url option to ransack search_form_for it will submit the form to index action of the entity for which ransack search form is built.
So, you have to change your view logic to the following code-
<%= search_form_for #q, url: member_lists_path do |f| %>
<%= f.label :f_name_or_l_name_or_email_or_business_name %>
<%= f.search_field :f_name_or_l_name_or_email_or_business_name %>
<%= f.submit %>
<% end %>
Here, you can read more about ransack.

Related

Using ransack to search from different controller action

I have a PagesController that contains two actions
def search
#q = Listing.ransack(params[:q])
#listings = #q.result(distinct: true)
#listings = #listings.where(active: true).order("created_at DESC").page(params[:page]).per(12)
end
and
def home
end
now what I would like to achieve is that I could fire a search from the home path to the search path like
home.html.erb
<%= search_form_for #q, url: search_path do |f| %>
<%= f.search_field :listing_name_cont %>
<%= f.submit 'Search' %>
but this returns No Ransack Search object was provided to search_form_for which It should since there is no q param passed to the home action. The only way I can get this to work is that if I modify the home action as follows
def home
#q = Listing.ransack(params[:q])
end
but this doesn't seem reasonable since I'm not displaying any of the listings on the home page so I can imagine this will just slow down the page by quite a lot If I'm fetching a lot of listings. Is there any other way I could achieve this?
You can do <%= search_form_for #q, url: 'home' do |f| %>
'home' can be whatever you want the URL extension to be.

error - First argument in form cannot contain nil or be empty

to start out preemptively, I've already looked at various similar articles dealing with this, but I still get the error.
I'm starting out on rails and attempting to create a GPA calculator and tracker application for fun (and spent a lot of time searching through documentation); I have a singular controller and view since redirecting to an entire different page for calculating or saving a new GPA every time would look ugly.
Rails will display everything without error up until I add the form, no other erb is written currently, and the form is meant to submit letter grade values from the "f.selection" tag.
The culprit is #cgpa in <%= form_for #cgpa do |f| %>.
My form from main\index view:
<%= form_for #cgpa do |f| %>
<div class="field">
(...)
</div>
<div class="actions">
<%= f.submit 'Calculate' %>
</div>
<% end %>
My controller:
class MainController < ApplicationController
def index
##cgpa = CurrentGpa.all #currently calls a key_to error while form exists, otherwise no error is raised
#pgpa = PastGpa.all
#csem = CurrentSemester.all
#psem = PastSemester.all
end
def new
#cgpa = CurrentGpa.new
end
def create
(...)
end
end
The routes are simply Rails.application.routes.draw { root 'main#index'; resources :main }
If any other information is needed, just let me know to add >.>
When you use: <%= form_for #cgpa do |f| %> this automatically tries to submit the form to the CurrentGpasController create action and for doing so it sends a request to current_gpas_path. So you don't have this path in routes that is why it throwing error. Either you can add routes for CurrentGpa like:
resources :current_gpas
or you can specify a path in the form_for:
<%= form_for #cgpa, url: any_path do |f| %>
So this will submit your form to that url specified.
If you add the current_gpas routes then do create the controller and action to process your input.
And as mentioned in comments do add the #cgpa = CurrentGpa.new this in index action. The above will solve your error you are getting after that.
Hope this helps.

How to render the index view, from inside a view in ruby-on-rails

I'm trying to render a the index view inside my ruby-on-rails application. How do a I render the index view, from inside a view passing an array of things to display? Using the link_to.
I do not want to re-route to the controller, I just want to link_to a view passing it the variables it needs, how can I do this?
EDIT:
I am trying to create a page type functionality in the index of my article model. So I have around 400 articles for example, and when the index action in the article controller is called, it of course renders the index view which is calling a partial for each article in the '#articles array' passed on by article controller's index action.
So in the view, I'm trying to do something like:
<% count = 0 %>
<% #articles.each do |article| %>
<% if count <10 %>
<%= render partial: 'index_articles', locals: {article: article} %>
<% count = count + 1 %>
<% end %>
<% end %>
<% #articles = #articles.drop(10) %>
<% if #articles.any? %>
<%= link_to "Next", 'articles', locals: {#articles => #articles} %>
<% end %>
Thank you in advanced for all of your help.
You'll need to use the render command, probably with a partial:
<%= render "controller/index", collection: ["your", "array"], as: :object_name %>
You will have to call a controller action to generate this. You cannot simply load it on your screen, unless it was preloaded inside your javascript for something:
#View
<%= link_to "Index", controllers_path(ids: ["1","2"]), remote: true %>
#app/controllers/your_controller.rb
class YourController < ApplicationController
def index
#posts = request.xhr? Post.find(params[:ids]) : Post.all
respond_to do |format|
format.js #-> app/views/controller/index.js.erb
format.html
end
end
end
#app/views/controller/index.js.erb
$(".element").html("<%=j render 'index' %>");
There are several issues with this approach...
Flow
First of all, your flow of your app should be as structured as possible.
In short, if you're calling the index view inside another action, it's not the index view any more.
What you should look at is how to use a partial in your app:
#app/controller/views/_partial.html.erb
<%= post.title %>
This way, you can adapt your index view and your other page to use the partial in their respective action layouts:
#app/controller/views/index.html.erb
<%= render "partial", collection: #posts, as: :post %>
This will allow you to "reuse" code much in the way you want. This will be much more appropriate than trying to invoke other action/views.
-
Resources
Secondly, you'll want to look at how your app functions.
Your index view is meant to show all the items for a particular object. Whilst you're free to change this as you want, the fact remains that you have to keep some structure.
You should read up on the routes for your actions, and how they're meant to work in your application. This will give you some perspective on the resourceful nature of Rails routes, and how you'll have to call specific routes with specific actions.
Your problem is probably that the file needs to be named _index.html.erb. You can have another file named index.html.erb which just renders _index.html.erb.
If you need a full guide on using AJAX, look up the railscast. If you're not using AJAX and you just want to render it, then you don't use link_to. You just do <%= render :index %>.

Working with Third party yummly api in rails 4

I've searched all over the web for decent explanations of how to do what I want to do, but cannot find any.
What I want to do is have the user be able to search through the yummly api and return some results back...
Here is some code.
index.html.erb
<% #results.each do |r| %>
<%= r.name %>
<% end %>
home_controller.rb
class HomeController < ApplicationController
def index
#results = Yummly.search('Onion')
#recipe = #results.map(&:to_s)
end
end
I've installed the Yummly gem which allows me to call Yummly.search
How can I allow the user to search for the term instead of hard coding it? It returns just fine hard coded but I cannot figure out how to allow the user to search.
Thank you!
Ideally, you would have a form that allows the user to enter a search term, and then pass that term to the Yummly API.
Something like (substitute your own route name):
<%= form_tag({controller: 'home', action: 'index'}, {method: :get}) do %>
<%= text_field_tag 'search' %>
<% end %>
And in your controller:
Yummly.search(params[:search])

rails search result

Now I can search only from videos page, How to add search from any page ot the site?
When I want to search from another controller search doesn't redirect to index
my controllers/videos_controller.rb
def index
#videos = Video.text_search(params[:query]).page(params[:page]).per(12)
end
my views/shared/_menu.html.erb
<%= form_tag videos_path, method: :get do %>
<%= search_field_tag :query, params[:query] %>
<% end %>
You can add that partial to any view page on the site (page_name.html.erb file). It will submit to the controller via the routes file.
Anywhere on the site you can add the following to a view:
<%= render "shared/menu" %>

Resources