Unable to store cookies in Rails - ruby-on-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)

Related

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

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

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

i want to get the form data form one gsp and want to access that data in another gsp and want to insert that data in table, how can i do this?

This is one gsp where i created one form
<div class="form-group">
<label for="msg_to">To</label>
<input type="email" id="msg_to" name="msg_to" class="form-control" style="width:500px;" required="">
</div>
<div class="form-group">
<label for="msg_subject">Subject</label>
<input type="text" id="msg_subject" class="form-control" style="width:500px;"name="msg_subject" required="" maxlength="50">
</div>
<div class="form-group">
<label for="msg_body">Body</label>
<textarea class="form-control" id="msg_body" style="width: 500px; height: 250px" name="msg_body" required="" ></textarea>
</div>
%{--<button type="submit" class="btn btn-default">Send</button>--}%
<g:actionSubmit value="Send" action="g_Inbox" style="color: #000000"></g:actionSubmit>
</form>
The data of above form must be access to another controller after submit and data should be display in the table form on that another view.
I fetch above data in following action:
def g_Inbox(){
def msg_to = params.msg_to;
def msg_subject = params.msg_subject;
def msg_body = params.msg_body;
println msg_to
println msg_subject
println msg_body
render(view: 'g_Inbox', model: [m_to : msg_to , m_sub : msg_subject , m_body: msg_body] )
}
from here i want to send it to view and want to add in new row of table..
You're in luck. What you want to do is very easy with Grails. But... you have to do your homework.
Things to read and digest
(best served with pizza and iced tea)
Groovy Server Pages -
https://grails.github.io/grails-doc/latest/guide/theWebLayer.html#gsp
Controllers - https://grails.github.io/grails-doc/latest/guide/theWebLayer.html#controllers
Once you understand GSP and controllers, you'll know exactly what to do.
Simple if You know how it works
render(view: 'g_Inbox', model:[params:params])
Access the params in gsp by
${params.msg_to}
and for Another Controller
redirect controller:'Controller' action: 'create',params:params

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.

how to generate AuthenticityToken on rails

I build the form tag by myself and when I post the form to server it give me a InvalidAuthenticityToken error, so I want to know how to add it in my own in current situation:
<form accept-charset="UTF-8" action="/crops/update" method="post">
<input id="crop_x" name="crop_x" size="30" type="text" /><br />
<input id="crop_y" name="crop_y" size="30" type="text" /><br />
<input id="crop_w" name="crop_w" size="30" type="text" /><br />
<input id="crop_h" name="crop_h" size="30" type="text" /><br />
<input id="crop" name="crop" type="submit" value="Crop!" />
</form>
Response error is:
ActionController::InvalidAuthenticityToken in CropsController#update
ActionController::InvalidAuthenticityToken
Rails.root: /home/mlzboy/my/crop2
Application Trace | Framework Trace | Full Trace
There is a view helper called form_authenticity_token that returns the current session's authenticity token.
In your view.html.erb:
<form action="/blah" method="POST">
<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden">
<input name="first_name" type="text">
</form>
This answer is first for rails form token tag in Google so to keep it simpler for future googling generations: just use token_tag, it's a helper defined in ActionView::Helpers::UrlHelper that returns hidden input with form_authenticity_token as default value.
To generate the token you have to use the method: form_authenticity_token as it was correctly noted by #flitzwald. Since it is rediced in a active controller's concern, you must include the module into a controller expclicitly before using as follows:
include ActionController::RequestForgeryProtection
# use
def set_csrf_header
response.headers['X-CSRF-Token'] = form_authenticity_token
end

Resources