Create a search page in ActiveAdmin - ruby-on-rails

I'm using ActiveAdmin to deploy my project. And I had some problem when I was developing.
I had some database table, e.g: "Worker", "Product", "Task". I wanted to create a page to search on those table with many situation.
I created a simple page:
ActiveAdmin.register_page "my page" do
content do
panel "Search details" do
panel "By Name" do
render :partial => "form"
end
end
end
end
And this is _form.html.erb
<form action="" method="post">
<input type="text" value="" name="taskid">Task ID</input>
<input type="text" value="" name="productid">Product ID</input>
<input type="text" value="" name="workerid">Worker ID</input>
<input type="submit" value="Submit"/>
</form>
But I don't know how can I call a controller from a form? (which controller was defined)
And How can I render or show the result to the content area of "my_page" in Activeadmin from a controller?
Anyone may help me? plz!

Design the form such that it uses the existing active admin filters and displays the results accordingly. You may want to look at the HTML of your filter forms (using firebug), in order to get the required params, and the submit action. Here is the partial which I use to search my User model (constructed from user filters):
<div class="panel_contents">
<form method="get" id="q_search" class="filter_form" action="/admin/users" accept-charset="UTF-8">
<div class="filter_form_field filter_string">
<label for="q_email" class=" label">Search User Email</label><br/>
<input type="text" name="q[email_contains]" id="q_email" />
<input type="submit" value="Go" name="commit" id="q_submit" />
</div>
</form>
</div>
and displays the result in the existing format. You don't need to design new views for that.

Related

Rails - raw() removing FORM from content field data

I have a text field that stores page content created with a WYSIWYG HTML Editor.
Text field (content):
<form accept-charset="UTF-8" action="/myaction" method="post">
<input name="myaction[product]" type="hidden" value="Other" />
<input name="myaction[product]" type="hidden" value="Car" />
<input class="btn btn-primary" type="submit" value="Get a Car"/>
</form>
Show page
<% highlight(raw(#page.content),params[:search]) %>
The form does not seem to display or show the bootstrap form and button. It seems to be getting removed. What can I do to get the form to display?

Unable to store cookies in Rails

LoginController:
class LoginController < ApplicationController
def new
cookies[:id]=rand(1000)
render "index"
end
end
index.html.erb:
<body>
<center>
<h1> Login </h1>
<form action='login/auth' method='POST'>
Name: <input type="text" name="name" widht=20><br>
Password: <input type="password" name="pass" widht=20><br>
<input type="submit" value="submit">
</form>
</center>
<br><br>
</body>
My requirement is, if login page is called, the response page should send with a cookie. So, I create a cookie with a random number in
new action. But the page "index.html" didn't have cookie in browser. But it is properly worked if "new" action call its default view file
"new.html.erb". Why the cookie only works in default view and not in some other view file. How to solve this problem ?
FYI, you have two typos in the form. "width" is typed as "widht."
<form action='login/auth' method='POST'>
Name: <input type="text" name="name" widht=20><br>
Password: <input type="password" name="pass" widht=20><br>
<input type="submit" value="submit">
</form>
This won't solve the problem, but it will remove one more error once the problem is solved.
Related to the original problem, I wonder if it could be that when you want to pass a variable to a non-default view, you may need an instance variable (but since cookies aren't expressed as instance variables, I'm not sure where to go with that)

unable to display the validation messages from #Html.ValidationSummary() in MVC view

I have a fairly simple form created in a partial view and loading the form on a jquery dialog. The view is tightly bound to a model. When the user clicks on the submit button with out entering any inputs, I need to show the validation messages.
<div>
<form method="post" enctype="multipart/form-data" id="ssimForm" action="Home/ProcessUploadedFile"
onsubmit="return false;">
<div>
<h3>
Select the file to be uploaded :</h3>
<span>
<input type="file" name="UploadFileName" id="UploadFileName" /></span>
</div>
<div>
<h3>
Select the date range :</h3>
<span class="uslabel">From Date(MM/dd/yyyy): </span>
<input class="usdate" id="usfromdate" name="StartDate" type="date" />
<span class="uslabel">To Date(MM/dd/yyyy): </span>
<input class="usdate" id="ustodate" name="EndDate" type="date" />
</div>
<div>
<br />
<input type="submit" id="submitButton" name="submitButton" value="Process File" />
</div>
#Html.ValidationSummary()
<div class="message-success">
<span>#ViewBag.Confirmation</span>
</div>
<div class="message-error">
<span>#ViewBag.Error</span>
</div>
</form>
</div>
Now comes the actual problem. when I submit the form I am able to see the validationSummary object populated with the messages, but they are not getting rendered on the screen.
I am able to see the messages if I replace the content of the dialog with the response from the jquery ajax call, that fetches the entire view from the server side. I do not want to take this approach as I beleive this is not the correct way to return validation summary in MVC.
Am I missing something? Any help is appreciated.
I don't know why you don't have the #using (Html.BeginForm()) { } block.
Here is my blog post for a quick and easy way to set up Validation Summary + Unobtrusive Validation for MVC3+.
http://geekswithblogs.net/stun/archive/2011/01/28/aspnet-mvc-3-client-side-validation-summary-with-jquery-validation-unobtrusive-javascript.aspx

How to add a filter to Active Admin dashboard?

I want to add search functionality for a couple of my models on the Active Admin Dashboard page. How do I do that?
For a regular model I can do that using "filter", but how do I do that on Dashboard view. What are the methods available within section and ActiveAdmin::Dashboards.build?
Adding how I did it (following advice from Sjors), so that someone else may find it useful:
in dashboards.rb:
section "Search User", :priority => 4 do
div do
render "search_user"
end
end
in views/admin/dashboard/_search_user.html.erb (Copied HTML of the user filter using firebug):
<div class="panel_contents">
<form method="get" id="q_search" class="filter_form" action="/admin/users" accept-charset="UTF-8">
<div class="filter_form_field filter_string">
<label for="q_email" class=" label">Search Email:</label>
<input type="text" name="q[email_contains]" id="q_email" />
<input type="submit" value="Go" name="commit" id="q_submit" />
</div>
</form>
</div>

Do MVC3 non-sequential hidden input indexes need to come first?

MVC3 non-sequential index hidden inputs for model binding..
<input type="hidden" name="Index" value="whatever" />
Does it matter if they go before, after, in the middle of the other related inputs to be posted?
Does it matter at all where they end up in the posted data?
For example, can they all be lumped together and it still works?
<input type="text" name="[A].Id" value="1" />
<input type="text" name="[B].Id" value="2" />
<input type="hidden" name="Index" value="A" />
<input type="hidden" name="Index" value="B" />
No, the order of your form fields does not matter, nore where they appear on the html page.
The most important factor for MVC3 is the name of the fields must match to the name of your controller/action parameter.
If you have two fields with the same name however, only one value will be returned into your action.
As long as the hidden fields are located inside of the form it should not matter the order in which they are placed. Please see code sample below. Notice how the hidden fields are put anywhere inside of the form.
#using (Html.BeginForm())
{
#Html.ValidationSummary(false, "Please correct the following errors")
#Html.HiddenFor(m => m.CoolStuffId)
#Html.Partial("_EditCoolStuff", Model)
<fieldset class="ui-grid-a">
<div class="ui-block-a"><a data-role="button" href="#Url.Action("ActionPlan", "Store", new { id = Model.StoreID })">Cancel</a></div>
<div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div>
</fieldset>
#Html.HiddenFor(m => m.TypeId)
}

Resources