0

(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?

2
  • Please refine your question and focus on what you are trying to achieve Commented Mar 29, 2016 at 13:29
  • @sureshprasanna70 - I wrote a short description below the long copy. I hope, my question is now easier to understand. Commented Mar 29, 2016 at 13:42

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I read about it but was not sure if there's a better way or even if this gem is THE solution for my issue. Especially when using it with Rails 5.
1

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:

  1. Create a "bulk edit" form (the drop down select menu above the table)
  2. Create a controller method to handle the bulk edit (controller#bulk)
  3. Update routes.rb to direct a URL to that new method
  4. 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.

5 Comments

Hello @GoGoCarl, I alredy read your answer yesterday but I first wanted to try out the solution by sureshprasanna70. That worked well. So far. But I'm a beginner with RoR and so I like to try different ways. Today I'll start with your suggestion. It looks pretty easy to implement. I'm looking forward to a good solution. One thing I like with your way is that there's no gem involved. I'll write a comment, when I'm done. Thanks alot for yout help.
Hi @GoGoCarl I just got into little issues doing it like you wrote. I tried it your way and had some tiny issues. One is, and thats becaus I didn't metion the name of my model I use, that I didn't got I right, where to put the config.rb. I put it in 'initializers' with the modified recources block, like this: resources :open_positions_lists do post :bulk, on: :collection end By the way the model name is 'open_positions_list'.
When I put this route into config.rb and test the routes with 'rake routes' I'll get the error message: rake aborted! NoMethodError: undefined method resources' for main:Object` If I put it direct into the routes.rb I don't get any errors wtih routes. Do I have to put the config.rb else where? I know, my solution works, but I would like to do it right.
The next thing I'm struggeling with is the setup of the index view. I put in check boxes to select the rows in the table wich shall be edited. But how do I get the link to the form part with the <...action="/open_positions_lists/bulk"> ? With this bulk edit, if I understand it right, I'll get to a form, where the editings will be made, right? Or is it possible to do it like this: All checked row get the value "Yes" in the model column "shortlist" and all uncheckt stay with "No".
...I've tried everything I found by google related to what you suggested but I'm stucked now. For better understanding, I think it's good to share the github repository, where you can find the branch with my editions. The linkt to the branch: link Maybe you have time to answer. I realy like to try it your way. If it is confusing which branch I meant, it the "bulk_edit"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.