werkzeug.routing.BuildError: Could not build url for endpoint 'result'. Did you forget to specify values ['search']? - url

I want to create a search function, I'll add it later, now I'm just concerned about my URLs. When I search, I want to have the thing that I searched for in the URL. But every time I get this error:
werkzeug.routing.BuildError: Could not build url for endpoint 'result'. Did you forget to specify values ['search']?
this is my python file
#app.route("/search")
def search():
search = request.form.get("search")
return redirect(url_for('result', search=search))
#app.route("/result/<search>")
def result(search):
return render_template("result.html")
And here is my HTML with the form
<form class="form-inline my-2 my-lg-0" action="{{ url_for('search')}}">
<input class="form-control mr-sm-2" name="search" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>

The reason is beacause search is None
You should implement your search in this way:
HTML
<form method='GET' action='/search'>
<input type='text' name='search'>
<input type='submit'>
</form>
Python
#app.route('/search')
def search():
if 'search' not in request.args:
#return normal page
#Do stuff
return redirect(url_for('result',search=request.args['search']))
#app.route('/result/<search>')
def result(search):
#Show the result
return search

Related

Search box and asp-route

I have just added search box to an index view
<form asp-action="Index" asp-route-account="#Model.First().AccountId" method="get">
<div class="form-actions ">
<p>
<input id="searchStringId" type="text" name="searchString" value="#ViewData["currentFilter"]" />
<input type="submit" value="Search" class="btn btn-primary">
<a asp-action="Index" asp-route-account="#Model.First().AccountId">Back to Full List</a>
</p>
</div>
</form>
This gives me:
https://localhost:44377/MainUsers/Funds?searchString=aldi
But I need
https://localhost:44377/MainUsers/Funds?account=1&&searchString=aldi In other words I need the search string to also include the route id.
Any ideas?
I need the search string to also include the route id. Any ideas?
You can try to achieve the requirement by using a hidden input field as below.
<input name="account" type="hidden" value="#Model.First().AccountId" />
<input id="searchStringId" type="text" name="searchString" value="#ViewData["currentFilter"]" />
Test Result

How to access to the new page with form and target="_blank" through capybara

I am trying to scrape this page with capybara + poltergeist.
<form action="/agency/advertiser/login" method="POST" role="form" target="_blank">
<input type="hidden" name="csrfToken" value="token"/>
<input type="hidden" name="account_id" value="id">
<input type="submit" value="login" class="btn btn-warning">
</form>
I could access to the element below, and tried click.
<input type="submit" value="login" class="btn btn-warning">
However, I can't access to the new page that opens after the click. How can I do that?
You need to tell Capybara you want to work in the new window. You do that by getting a handle to newly opened window and then using within_window or switch_window.
new_window = page.window_opened_by do
# perform whatever action causes the new window to open
page.find('form').click_button
end
page.within_window(new_window) do
# perform actions in the new window
end
# context returned to the original window
...

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)

How to post html form data to controller using ruby on rails 3.1

I am using ruby on rails 3.1. And am trying to post html form data to controller for saving the record in database. But I am getting routing error like this 'No route matches [POST] first/save' .But when I tried to run this link in address bar like '127.0.0.1:3000/first/save' it is working fine. Can any one please tell me where am doing wrong.
My routes are like:
Rails.application.routes.draw do
root 'first#hello'
get 'first/save'
end
And my html form is like:
<form accept-charset="UTF-8" method='post' action='/first/save'>
<label for='S.No'>S.No</label>
<input type="text" name="s_no" placeholder='Enter s. no.'>
<label for='name'>Name</label>
<input type="text" name='name' placeholder='Enter your name'> <br>
<label for='seller_id'>Seller ID</label>
<input type="text" name='seller_id' placeholder='Enter your seller ID'> <br>
<label for='email'>Email</label>
<input type="email" name='email' placeholder='Enter your email'> <br>
<label for='img_url'>Image</label>
<input type='text' name='img_url' placeholder='Enter your image url'> <br>
<input type="submit" name='save' value='Save'>
</form>
And here is my controller:
class FirstController < ApplicationController
def save
#name = params[:name]
#email = params[:email]
#seller_id = params[:seller_id]
#img_url = params[:img_url]
#s_no = params[:s_no]
end
end
If you want to do POST requests, instead of
get 'first/save'
you should have
post 'first/save'

Create a search page in ActiveAdmin

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.

Resources