How to access to the new page with form and target="_blank" through capybara - ruby-on-rails

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
...

Related

Input parameter of action is null when using asp-route-{value}

I want to download a file that its name has been saved in my database. I use the following code to download the file:
public async Task<IActionResult> DownloadPm(string fileName)
{
.
.
.
}
And use the following code in view:
<form method="get">
<button type="submit" class="btn btn-success btn-table" asp-controller="Page" asp-
action="DownloadPm" asp-route-fileName="#item.mainFileName">ِDownload</button>
</form>
The problem is that the input parameter in DownloadPm action is null.
You are using tag helper with a button which actually does nothing but triggers the html form. To keep your current code you can have a hidden field in the form which will post the value to the action. Sample code:
<form method="get">
<input name="fileName" type="hidden" value="#item.mainFileName">
<button type="submit" class="btn btn-success btn-table" asp-controller="Page"
asp-action="DownloadPm">ِDownload</button>
</form>
Better approach: Since you are using Http GET and there is no other control in the form then why bother with Form? Instead go with an anchor element and style it like a button. Like the followings:
<a class="btn btn-success btn-table" href="/Page/DownloadPm?fileName="#item.mainFileName">Download</a>

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)

Wrong button executes the method="post"

How do I prevent "button1" from executing the emailform.php action?
I have three buttons on my page; two of which are supposed to just make sure the text boxes are not blank and the third one is a "submit" button that is supposed to email me the form results via a emailform.php action. The problem is that when I click on button 1 to validate the text boxes, it does that correctly but then tries to email the form.
<form name="surveyform" action="/emailform.php" method="post">
<button id="button1" onclick="return validate_question1()">Go to question 2</button>
<button id="button2" onclick="return validate_question2()">Go to question 3</button>
<input name="submit" value="submit" class="submit" type="submit" />
Try this -- set the type:
<button type ="button" id="button1" onclick="return validate_question1()">
Might need to see the onclick function.

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