Restful route design for adding item to container - ruby-on-rails

My cart can have many folders. Those can be nested, but are always displayed as a whole. I want to design an interface to add new item to the folder.
My plan is to have a button for folder, after clicking it, the products#index is displayed. The user select one product, and in the products#show page, the user fills some options and click submit, then the item is added into the particular folder.
What's a RESTful way to design the routes?
My rough design is the following. Is it okay?
get folders/13/add_products/
get folders/13/add_products/3
post folders/13/add_products/3

In general, you should avoid verbs (e.g. "add") in RESTful routes:
GET /folders/:id/products
GET /folders/:id/products/:product_id
POST /folders/:id/products/:product_id

Related

Submit a form from different page

I have a Driver form (which is basically the user - I can do current_driver and stuff) and one of the fields is a boolean checkbox for 'subscribe to newsletter'.
I want to put this form on multiple pages that do not share the same controller, i.e Dashboard which has a pages controller.
I am using a partial to do this so i can just add the partial to a template easily.
I believe that the form should post to edit_driver_path as it's the driver form? At the moment when I submit the form, I get "No route matches [POST] "/drivers/dashboard/edit".
Am I on the right path here?
Let me know if I need to post controllers, forms or whatever.

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.

How to display a different breadcrumb title for the same controller + action method?

I have a Home controller and a Business controller. The business controller has a few action methods on it: Search, Create, Update, Delete.
On my home page I have links to the Search and Create views on the Business controller. The Search view also has a link to the Create view.
I want the bread crumb to look like the following when Create is accessed from the home page:
Home > Create
…and i want it to look like the following when Create is accessed from the Search page:
Home > Business > Create
In both the cases the controller/action method is the same, but the breadcrumb I want displayed is different. Is it possible to do this using MvcSiteMapProvider?
As far as I know this is not supported out of the box. Meaning that you have to adapt the HtmlHelper template to you needs, see https://github.com/maartenba/MvcSiteMapProvider/wiki/HtmlHelper-functions.
The only way this can be done is if you add some information to your route to tell one request apart from the other, then you can configure 2 different nodes to create both breadcrumb trails.
I have a working example of how to do that on my blog: http://www.shiningtreasures.com/post/2013/08/10/mvcsitemapprovider-4-seo-features#canonical-tag. Make sure to check out the code download.

Orbeon: creating my own delete button

2/28: Seems the Go uri is only if you create your own persisten layer. I'm going to try and use a link on my form to do this. If I can figure out how to find the form_id of the current form.
Original Question:
I'm trying to restrict who can delete a form instance. It seems if people can get to the form-runner summary page, they can click the delete button and delete a form (even if they are not allowed to do any "/orbeon/fr/hr/expense-report/edit/*" options.
Anyone found a way around this issue. I wonder if we could use the GO button on the form /edit/ view to build our own delete feature.
If I look at the page source from the hr/expense-report/edit/f36b446c3ddbf7c63ec033d5c6fa7ce4 view, that the from does have the details to the actual form instance.
Example:
form id="xforms-form" class="xforms-form xforms-initially-hidden xforms-layout-nospan" action="/orbeon/fr/Test/Hidden_Search/edit/f36b446c3ddbf7c63ec033d5c6fa7ce4"
I wonder if that information could be passed to the "GO" button, if I have that on my page?
Right now, if users can access the Form Runner summary page, they can also access the "delete" button. Showing the "delete" button on the summary page for some users but not others, requires a change to Form Runner, which shouldn't very complicated.
For instance, if you only want the "delete" button to be shown for users with the role can-delete, on this xforms:bind of fr/summary/view.xhtml add the attribute:
relevant="xxforms:is-user-in-role('can-delete')"

Link_to instead of button to submit a form

I'm new at ruby on rails, and I wanted to accomplish the following:
I've got a list with check boxes(one per row) and I'd like to have a link button(not a common button or submit) so when I click it I call an action from a controller.
My questions are:
How can I call a controller action with the link_to?
How do I get the checkboxes values(which of them are checked) and fields' values so I can know to whom apply the action? I'm used to work with C#, and there the lists have an ItemDataBound method in where you can get easily every row lets say, but I can't figure anything similar here.
If it's not clear enough, I'm going to put an example:
Let's say I've got the following "screen":
Delete(link)
Article ID | Article Name
chkbox 1111 t-shirt
chkbox 2222 pants
chkbox 3333 boots
Now, let's say I'd like to delete the pants and boots. So I'll check their check boxes and then I'll press Delete. Now, I'd like to have at my Articles controller, at the method delete_article(for example) and then get the id and name for those checked articles, and delete them.
Thanks,
Brian
I would wrap the checkboxes in a form and then submit this form using the link (either with javascript or change the link to a form button and style as a link).
Rails assumes a RESTful approach out of the box, so a straight link will always hit a GET accessible action on your controller (generally index or show). GET actions should always be idempotent.
You can use link_to the standard way, check out the rails documentation on 'link_to'.
The values from checkboxes can be get from the params hash.
Just look out for the documentation.

Resources