I have tried to search for an answer for this, but nothing seems to be exactly what I'm looking for.
I'm using Rails 4 and I have an Exercise model and a Routine model that have a has_many through relationship via Exercise_Routine. The users have associated with them a list of routines and a list of exercises. The goal is to let a user make up the associations of what exercises belong in routines by being able to drag them from their list of exercises to a list of exercises associated with the routine they're editing.
So it would involve two lists. One being the list of exercises a user has and the other being the list of exercises associated with the current routine. I want to make it so they can drag and drop from either list to edit this or even for the initial creation.
I can get the drag and drop between lists to work. What confuses me is how to do the updating of the database with the changes. Would rails realize that the user just removed one thing from the list and that it should delete that record? Would it try and recreate all the associations in the list every time resulting in an error?
I would also have this happen as part of the edit action inside the routine controller since that is where it logically would make sense to do it. Also would it be better to do an AJAX call that does the update each time they make a change or wait for all the changes and a submit button press to do the actual update?
I can see how I want this to work in my head, but the how is eluding me. Any help is appreciated.
Rails cannot do drag and drop by itself: you need to use some kind of Javascript library like jQuery.
There is a railscast on this topic: http://railscasts.com/episodes/147-sortable-lists-revised?view=comments
Related
Im working on the search feature for my app and I would like to give users the option for some sorting of viewing the recipes. Im trying to make a popular feature but Im not entirely sure where to start and if I need to make any modifications to my schema. Ive read this post http://sorentwo.com/2013/12/30/let-postgres-do-the-work.html and get the jist of what its doing. My main question is how do I track when a page is viewed and use that in the calculations? And also track views over time?
Also for using things like comments as a weight is it better to count the number of comments dynamically (query the comment table and add them up) or keep a column in the recipe table that gets added to whenever a comment is added?
I'm new in Rails, I have a Meal model which has many Products. Meals are assign to User (maybe this is important for a concept). In meals/new.html.erb I want to create new Meal as follow:
Click the button "Display Products"
On the same page (meals/new.html.erb) open modal (pop-up) with all products assigned to current user ( I have help method for current-user). It should be displayad like a list or grid with checboxes for example.
Then user can check few products and click "Confirm".
After that in meals/new.html should be appeared list of chosen products with additional input to fill their quantity.
So I have two problem here.
How should I display modal? Is needed any Ajax (I'm not so familiar with this technology)
How can I pass products between view and modal?
Could you help me a little to achieve these goals?
Regarding your first problem, displaying the modal is fairly straight forward. Essentially you will create a div with the proper bootstrap classes to be hidden when the page is loaded, and then create a button that makes it visible. I would recommend either reading over the W3Schools entry on modals, or from the appropriate part of the bootstrap javascript documentation.
Regarding your second question, this depends on exactly what you mean. The modal is part of the view, so if you're only trying to put information that is currently on the modal back onto the "page" behind it, you can do so fairly simply with javascript (copying content out of one element into another, or updating states of inputs). If, on the other hand, you're trying to use the modal to retrieve information from the server (for instance if you wanted to show a list of possible options, and then display detailed information about the selected items from the database) that would require Ajax.
If you have any snippets of code that aren't functioning as you expect, feel free to add your View to the question. In cases like this, usually the best way for us to provide help is for you to take an early crack at this, post the relevant code, and then seek answers for the things that behave unexpectedly.
I hope that helps.
I'm writing an app that allows users to feed their inbox, then process their input, making it either a note or a task.
Since I need notes and tasks to behave very differently, I plan on using different models for them.
The question is: how do I go about changing inbox items into notes or tasks?
My first idea was to write an action that would destroy an inbox item and call the create action on the notes (or tasks) controller, but that does not seem right.
Is there a better way to do this?
Update
I'm looking into polymorphic associations as a solution for this, as suggested by #Dipak.
This is my schema: (sorry I can't paste the code. I'm using a web based ssh tool)
I have decided to use the Idea model to define inbox items
And these are my models:
I want to be able to click this link (on my partial)
and have it do two things:
create a task
assign this task to idea.thought
How should I do this? Is this the proper way to use this kind of association?
As per your question it seems like your few initial fields are same for all notes and tasks, that you calling inbox. So better you can save those fields in different table and other fields in related table.
For such situation you should use polymorphic association. You can read about it here. So your new inbox table should be intermediate table used for polymorphic association.
For a case where I have models like:
Coutries
States
Cities
Opinions
I'd like to begin with just presenting a user with a dropdown for Coutries.
Once a country is selected, then a States dropdown would appear below Countries.
Once a State is selected, then a Cities dropdown would appear below States.
Once a City is selected, a textboxt appears and the user writes their opinion of the city.
Once text is placed in the textbox, a 'submit' button appears.
Once the user clicks 'submit' I process the form data.
I'd like to populate each dropdown from a corresponding table using the previous selection to limit the dropdown items.
I'd like the same sort of functionality with checkboxes too.
Basically, I'd like to be able to define a cascading form input scheme. I have seen some information on the topic, but nothing that wraps it up tight for Rails 3. For instance:
Rails 3.1 Dependent / Cascading Dropdowns and
Rails 3.1 interdependent select dropdown lists
... seem to offer only partial solutions. I am getting the feeling that I might be missing some fundamental Rails concept and that this (seemingly common) task should be easier. I want to streamline the user input experience. My models have a lot of good default data that I'd like to present to the user as they make their input choices. I just can't seem to get it all on one page.
I don't get the search results I am seeking when I look for "Rails dynamic form input" Searching for "Rails Cascading dropdown" has helped some. "Rails active form" gets me a piece of internet history.
Can someone guide the way?
Thanks!
ADDENDUM
This seems to allude to the trouble I have stumbled upon: http://guides.rubyonrails.org/form_helpers.html#building-complex-forms Perhaps that's my answer?
JQuery is the way to go here I think. Now I am no expert in matters pertaining to JQuery, but I hope I can at least point you in the right direction. To accomplish your cascading list, you could show and hide the select inputs based on the change of the previous one. From there I would look into a JQuery library called jquery-chained. This library would allow you to show in the states select only those states which pertain to the selected country. I do have a couple working examples of the chained library that I could show an example of if you would like, and I am sure there are many examples out there of hiding and showing form elements in JQuery.
I haven't even attempted this yet and am creating this question for advice really.
I have a strongly typed page which receives a form model composed of several components. It is to create a mitigating circumstance (MC) for a student at a university for my final year project. A MC can be composed of the initial problem, assessment extensions, and I use a multi select box to allow the user to select staff retrieved from the database which are able to view the MC once created.
The thing is I feel that a student could be granted many assignment extensions for one problem. I am wander if it is possible to include a button/image on the form which when clicked will create a new assessment extension object, and duplicate the form components to set the values for the object? This would all need to occur without any page refreshes.
Any advice, or links to good tutorials would be appreciated. I have so far been unable to find any suitable examples.
Thanks,
Jon
There are a number of ways to do this, but the fastest is to create a javascript handler which creates the form controls without any sort of server request, all you need to do is keep track of how many items are in your list so you can name the form controls correctly.
Start by making a button that when you click on it creates form controls. Once you get that, work on the naming.