So I have this url in my site
localhost/home-loan
And when clicked, it performs the following function from the controller page
mortgage_loans_controller.rb
I then fill up a form and click submit. But i want my submit page to have the following url,
localhost/home-loan/thank-you
But it has localhost/mortgage_loans/thank_you
thank_you is also another function in the mortgage_loans_controller.
How can i do this?
Use this:
match '/home-loan/thank-you', to: 'your_controller#your_action', as: 'custom_name_for_helper', via: [:post]
Place this line inside of the routes.rb.
This line says that whenever you doing POST request to the localhost/home-loan/thank-you, execute your_controller's your_action.
as: options will use 'custom_name_for_helper' to create path helper, like custom_name_for_helper_path.
via: option determines which request method used, if you need to handle all types just use via: :all
I don't know the structure of your routes.rb, so place this line above all routes, to be sure that it will work.
Related
I want to create a back button to a page which can be accessed with a link from another page. To do this, I want to put the first page's path to the link, and on the next page I can put it to the back button.
I have this anchor tag:
Go to page
When I try to go to the site I get the following error:
Reverse for 'page' with keyword arguments '{'path': '/my_site/'}' not found. 1 pattern(s) tried: ['notifications\\/(?P<path>[^/]+)\\Z']
If you know the url name of the first page,
path('first_page/', views.first_page, name='first_page'),
then you can simply use:
Go the page
Otherwise, you must add the url of the previous page to the context of the second page's template in order to use that in that template. How to do that depends on your style. For instance, you can somehow keep a log of the visited pages on the client side or server side, and use that collection to determine the previous page.
I need route to index.html.erb? How can I do it? I tried root "index.html.erb#index" in routes.rb.
First of all, in rails we set routes for our controller's method not for our
view file. Get method of controller redirect to view the page itself.
Write below code in your routes.rb:
get 'index', to: 'controller_name#index'
You can replace get 'index' with any name like get 'users'.
You can refer this for more reference.
I'm in the design stages of a single page web app, and would like to make it so that a user can click on a formatted URL and the data requests will load in the page.
For example, a url of http://www.mysite.com/?category=some_cat will trigger the Category view with the relevant data.
My intention is to parse the URL, gather the data, then pass it to the index.html template for rendering on page load. Once the page has been loaded, a Javascript trigger setting will trigger the appropriate button to load the client view.
However, I'm having an issue setting up the URL parser, as the following settings are not matching the example url above.
from app.views import app_views, photo_views, user_views, admin_views
urlpatterns = patterns("",
url(r'^/(?P<category>\d+)/$', app_views.index)
)
You're confusing between sending information through your urls with GET and formatting you urls with arguments for the view functions. Say I am visiting a site called http://www.mysite.com/ and the page has a form that looks like this:
<form>
<input type='text' name='category' id='category'></input>
<button type='submit'>Send!</button>
</form>
upon clicking, the url will automatically change to http://www.mysite.com/?category=<value of input>. The ? marks that everything afterwards should be treated as GET data, with the syntax of <id>=<value>. You can then access them like so:
def response(request):
category = request.GET['category']
formatting urls is different, because it means looking for patterns that are part of the url. i.e. a pattern that looks like r'^/(?P<category>\d+)/$' will look for this: http://www.mysite.com/<category>/ and it will send it to the request in your views as an additional argument like so:
def response(request, category):
...
The regex is used to define how you recognize that part of the url. For example, the \d+ you're using means that category needs to be a number. You can search how to define different types of patterns according to your needs
Note that with GET you are sending the data to the same view function that rendered the page you are currently visiting, while using a different url means you tell it where to go through your urls.py (usually a different function). Does that make things a bit clearer?
I originally followed this tutorial (https://django-haystack.readthedocs.org/en/latest/tutorial.html), and have so far been able to highlight my query within my returned results. However, I want to highlight this same query when visiting the next page that I load with a separate template. Is there any way to save/access this query so that I can highlight the same results within this other template?
Whenever I try and include a statement like this, I get an error, which I'm thinking is because I'm not trying to access the query properly.
{% highlight section.body with query html_tag "span" css_class "highlighted" %}
You have to send to the next page, the information that you use to highlight the results in the first page. You can use the request.session to store the data and call it in the next page, or you can send the sqs by the url to the next page.
If you want to know how to manage the search query set, and how to edit that kind of stuff, I recommend you to read the views.py forms.py and the elasticsearch_backend in the haystack folder at: "/usr/local/lib/python2.7/dist-packages/haystack"
This is the url for the documentation of Django Session: Django Session
This is the url for the documentation to pass parameters trhough url: URL dispatcher
I want to change the display URL of a rendered response object. I have a single view "view1" and it is called by a URL, "localhost/foo/view1" . On some condition in view1 I want to change rendered URL to be displayed on browser to "localhost/foo/other/view1". I don't want to use HttpResponseRedirect. I only want to change the display URL in browser when the requested page is rendered.
No way you can do that. It would be phishing paradise if anyone could change the URL without redirecting.
Use redirects instead.
It's possible with the html5 history api, http://html5demos.com/history