I am working on a rails 3 app and have a shopping cart functionality. On the products page I have Items listed and "Add to Cart". the cart is also rendered on same page as a partial. Now I need functionality to put in a maximum amount allowed in a cart. I have a input box on that page for this. When I click checkout I need to compare the the total cart price against the maximum amount and display appropriate message. Where do (which controller action) I capture form value for the maximum amount?
It would be nice if you add some code. No one is gonna tell you that at least that you said what controllers uses for your shopping cart. Maybe you should add a action in your "shop" controller, and add your restful route in your routes file.
Related
We render users in alphabetical fashion with 10 entries per page using will_paginate gem
Assuming we perform an action to activate or deactivate a user in the 2nd page (say user15)
, We would prefer to show user information on the 2nd page itself or should we redirect the user to the first page with default sort criteria here
There is another case where this is becoming problematic:
Assuming there are 31 entries, so 4th page will contain a single entry
, If we delete this user, we can't show the fourth page since no such user exists
In the above scenario, page=4 and per_page=10, but it would fail to render this page
How should we handle these scenarios, should we just show page=1 in any of the above scenarios.
What would be the right thing to do in the above scenario
We would prefer to show user information on the 2nd page itself or should we redirect the user to the first page with default sort criteria here
Can you elaborate on this one please.
Assuming there are 31 entries, so 4th page will contain a single entry , If we delete this user, we can't show the fourth page since no such user exists In the above scenario, page=4 and per_page=10, but it would fail to render this page
You might want to look into cursor pagination. Instead of pages, you have a cursor for the record and fetch everything before / after this cursor. This cursor could for instance be the encoded created_at timestamp.
Cursor pagination has a lot of other advantages over offset pagination too. However, will_paginate does not support cursor pagination so not sure if this is a feasible approach for you.
To get around these performance concerns, some database engines offer cursor-based pagination, or encourage pagination based on min/max values, e.g. WHERE id > :max_id in which the :max_id value is based on the previous page of results. This approach is usually superior for speed and memory concerns, but comes with some tradeoffs; the most notable being that it's only ever possible to go to the next page of results and not immediately jump to page 20, for example. The will_paginate library does not handle cursor-based pagination.
https://github.com/mislav/will_paginate/wiki
https://medium.com/#meganchang_96378/why-facebook-says-cursor-pagination-is-the-greatest-d6b98d86b6c0
(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.
In my project, an Article has many Items within it. Since each Item has different length, so I would like to implement pagination manually, for example, by creating a PageBreakItem model, in order to allow users insert page breaks wherever they want. But I don't know how to use "page" parameter in controller to render views correctly.
Some gems like kaminari or will_paginate only allow me to configure the number of items per page. They don't have options for inserting page breaks manually.
Any suggestions are greatly appreciated.
You don't need a special model for this. You could do this with small adaptation of your Item model:
Add sort_order numeric field to denote order of items within the article and is_on_new_page boolean field to denote a page break occurring before that article.
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
I have started a Ruby on Rails 3 project to make an online version of newspaper. The front page has the news headlines, sports headlines and life headlines. It has all the headlines for the different sections of the site.
All the stories are stored in one table and photos in another table. It is a very simple set up. I am trying be DRY but I can't seem to avoid it. On the index action page I have to look up sports stories and on the sports action page I have to look up sports.
My question is should I make a different controller for every category? Or have a main controller with categories as actions? ( which is what I am doing now? )
I would have a single Story controller that takes a querystring to determine the category. In other words, if you go to
/stories/
you get the front page, which lists all (or a portion of all) stories. If you want sports you go to
/stories/?category=sports
and the Story controller filters the list of stories (and presumably alters the view headings, etc.) based on the querystring.