page with params between search and show page - ruby-on-rails

So i have this page that shows a result of events with a button that takes you to the eventid (which has a list of prices)
What im wanting to do is one of my api's is only showing all tickets available (how considerate). So i'm having to adjust to them.
What im wanting (and have made but not put in yet) is to have a page inbetween the search results and the show.html.erb
This page has a bunch of buttons on it, Asking how many tickets your looking for. e.g. 2 tickets for the event.
Now what im wondering is how i can go about this?
Currently the button is linked up like this (on the search results page)
<%= link_to 'Compare', event_path(event.id), class: "btn btn-info" %>
However what im going to want to do is have this button link off to a page called ticket_numbers.html.erb This will sit inbetween these two pages and when a user clicks said button on that page it shall allow me to use the number in the view and related jquery files.
Any help would be great!!
Thanks
Sam

You need to define a route for this new page:
match '/ticket_numbers' => 'controllerName#ticket_numbers'
And to create the file ticket_numbers.html.erb on the corresponding view folder.After this your new page will work.
If you need any controller logic define a method ticket_numbers on the corresponding controller:
class controllerNameController < ApplicationController
def ticket_numbers
end
end

Related

Ruby on Rails - Update multiple data in a table with select (bulk edit)

(Rail 5 beta 3)
I have a table on an index page (action) of a view with around 15 columns. Some of them are for text and some of them are for integers only. Every entry of this list (table) will be filled out by a 'form_for' form (in new or edit action).
For editing or deleting there are links with each list entry in the index view leading to the corresponding show, edit or destroy actions. This all works well. Some of these entries are entered by a select with pulldown on the new or edit view. This works well, too.
But if one of these selects should be changed for more than one entry in the list it takes too much time to click on 'edit', change the select and click on submit at each list item. To make this a better user experience I would like to be able to change the selects in the list (table) directly. It would be good to have the select/pulldown in place. The change of the state or choosen entry should than be saved in place as well or with an extra button ("save changes") above/below the table.
To say it in short:
I want to update multiple entries in a table in an index view without editig each single entry via edit view. The dates will be changed by a select and the data should be saved by a submit button on this page
Has anybody an idea how I can solve this?
Try best_in_place gem. It can solve the problem you have quoted. here are some links to it
https://github.com/bernat/best_in_place
http://railscasts.com/episodes/302-in-place-editing?view=asciicast
Your original text wasn't that confusing to me...
You want what's called a bulk edit feature. Easiest way would be to set up a new target at the controller level specifically to handle this request. I'll give you the pseudocode and it should be easy enough to fill in the blanks, but essentially:
Create a "bulk edit" form (the drop down select menu above the table)
Create a controller method to handle the bulk edit (controller#bulk)
Update routes.rb to direct a URL to that new method
Handle the bulk update in the controller and redirect back to index upon completion (cheap way of updating the page after editing is done).
Note: I'm assuming your model name is "Resource" because you did not specify what it actually was
On your index page, you want HTML like:
<form method="POST" action="/resources/bulk">
<select name="bulk_value">...</select>
</form>
On change/form submit/whatever, submit that form.
If you're using resourceful routing, you can hook this into config.rb via:
resources :resources do
post :bulk, on: :collection
end
Otherwise, hook the route however you see fit.
Then, in your controller:
def bulk
value = params[:bulk_value]
Resource.update_all value: value
redirect_to {resources_path}
end
Now, you said index view, so I am assuming you want all. But if you just want a subset, you can pass the preferred IDs along with the form as a hidden field, then use a where clause to filter, i.e.
Resource.where(id: parmas[:id_array]).update_all(value: value)
Anyway, that should get you down the right path.

page inbetween search results and results

So i have this page called ticket amount which has a bunch of buttons on it.
Wha im wanting is for the user to click the button on the row of the search results and be taken to that page. The user will then click on the amount of tickets they want and be taken to the page that matches the id from the from the row the button was on.
Basically at the moment its going
Search results >>>>> results
And i want
Search results >>>>> ticket amount >>>>> results
I've currently got the first one set up like this
<%= link_to 'Compare', event_path(event.id), class: "btn btn-info" %>
How would i go about making it different? Basically i'm wanting to use the amount of tickets the user selects in the view. So probably best to change the url from this
/events/idnumber
to
events/idnumber/tickets_required=4
Any ideas how to do this?
At the moment the ticket_amount.html only has basic bootstrap buttons inside
What you need to do is to send the params in a redirect_to.
What I would do is a small form for let the user select the tickets_required, the form will point to an action in the controller, within that action I would add a redirect to the search results url sending the param:
#Within your controller action once the user selects the tickets_required
redirect_to event_path(event, tickets_required: params[:tickets_required])
Redirect to the search results url or event_path or whatever, and send the tickets_required param in the url using the param[:ticket_required] coming from the form submission
Take a look to this thread that will guide you about how to send the params in the redirect_to

How to redirect back to Listing page?

I've got some pages going on. Each page shows Listings of items that users wish to sell, along with their prices and descriptions. At the bottom of the page is a link for creating a new listing.
What I want to do is have the back button return the user to the page they created he listing on -- NOT the index of all listings.
I can't just change the back button to
link_to 'Back', pages_universityofconnecticut_path
because it will link to the University of Connecticut even when used on Harvard's page. I hope that makes sense. How do I go about doing this?
I have used this line in my view to display only the listings where the school matches the page.
here's the solution of your problem, an excellent way by ryan
http://railscasts.com/episodes/131-going-back

Jquery redirect with Ajax Calls to another page Rails

I am trying to implement two pages. The first page being a selection of the items I want to show on the second page.
On my first page, I have done a selection of the items which I want to render.
On click of a button, 1. the ids of these items will be placed in an array and 2. the user will be redirected to a new page through:
window.location.replace("/schedule");
Right now, I want to achieve the following:
Do an ajax call through rails to get each of the array items. i.e. make an ajax calls to the urls of myclass/[id]. The id belonging to the items in the array.
I am not sure how I could get my array items from the current page to the next page or if there is a better way of achieving this. Any advice or suggestion will be greatly appreciated.
Add :remote => true to your form. Then, app will render your action_name.js.erb view instead of action_name.html.erb. In js file you can put code, which will repleace div without redirection, f.e.
$('#div_id').html('<%= escape_javascript render(your_partial_name) %>');
You don't need to do anything special in partial and controller action, its all the same.

ActionLink back button

I have an Index method that does double duty as showing a list of posts and a queried list of posts and can also have pages so you get urls like /News/Page/1 or /News?query=test
When a user clicks through to a post at say News/Details/1 they get a simple ActionLink that takes them back to the list. BUT I want this link to take them back to the actual page they were on in terms of the paging or the query. How could I do this? I don't want to use the JavaScript history method. Here is my current ActionLink: <%=Html.ActionLink("<< Back to News List", "Index")%> and this is an example of the paging links: <%= Html.RouteLink("<< First Page", "NewsPaging", new { query = ViewData["query"], page = 0 })%>
Thanks
It would probably be easier to just use javascript to send them back to the last page in their history (without creating a link to the specific page).
history.go(-1)
I will go for a ActionLink like
<%=Html.ActionLink("<< Back to News List", "Index")%>
because user can access News/Details/1 directly and if then you have a link that uses history.go(-1) or history.back() function it will not redirect it to the Index action
Another way to do this is to create the link in the controller and assign it to a property of the model or a simple ViewBag variable (up to you). For this example, I'm just going to use ViewBag.
So in your example, going from /News/Details/1 back to /News/Page/1?some-querystring, you can do the following:
In your controller (assuming '1' is the 'id'):
ViewBag.BackButton = String.Format("/News/Page/{0}{1}", id.ToString(), Request.Url.Query);
In your view:
< Back
One draw back to this is that you must know what the previous page was/could be. In your case, you want your back button to go back to your list, so this technique should be fine.

Resources