Rails - manipulating data in popup - ruby-on-rails

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.

Related

How can I support user edited content in a complex web application?

I'm building a web site (using ASP.NET, MVC 3, Razor) and I'm not using an off the shelf CMS. This is because I evaluated a lot of existing CMS's, and found them all to have a massive learning curve, tons of features I didn't need, and they force you into a page oriented model. By "page oriented model", I mean that you can specify a general page layout and stylesheets, but the object that the user can edit is a whole page, which displays, for example, in a central panel, and maybe you can customize the sidebars as well.
But this site is very design centric, and needs to be much more fluid and granular than this. By "design-centric", I mean that the site was built in Photoshop by a graphic designer, and there is heavy use of images and complex styling to map the design to HTML/css/js. Also, every page on the site is totally different. There are also UI elements such as accordion panels, in which we need the user to be able to edit the content of each panel, but certainly not the jQuery+HTML that powers the accordion. The users are subject matter experts but very non-technical.
So I'll have a page with lots of complex layout and styling, which I don't want the user to access, but within this there will be, say, a div containing text that I would like the user to be able to edit.
How can I best accomplish this?
So far, I'm implementing this by having snippets, which are little units of html, stored in external files, that the user can edit. In run mode, these are loaded and displayed inline (with a little "Edit This Content" button if you're logged in and have permissions). If you click the Edit button, you get a little WYSIWYG editing screen, where you can edit and save changes. So I can control all the messy stuff, and put in little placeholders for user editable content. But this isn't entirely simple for me, and I'm wondering if there's a better way.
Don't mean to necro this, but it seems to be the most relevant question to what I'm currently researching. I recently built something similar as you described above, but I'm pulling data from a database instead of static files. For each page (like /about or /contact) in the Controller I pull data for that page from the DB in the form of a Json string key/value pair. Key is the placeholder tag, Value is the.. value. After deserializing, I simply populate a list and assign it to a ViewBag, then in the CSHTML I ViewBag.List.Keyname to grab the text.
I have a small admin control panel which allows me to modify the text in the database. Having little hover-overs like you do is a great idea though!
Well, I stuck with my original plan:
So far, I'm implementing this by having snippets, which are little
units of html, stored in external files, that the user can edit. In
run mode, these are loaded and displayed inline (with a little "Edit
This Content" button if you're logged in and have permissions). If you
click the Edit button, you get a little WYSIWYG editing screen, where
you can edit and save changes. So I can control all the messy stuff,
and put in little placeholders for user editable content. But this
isn't entirely simple for me, and I'm wondering if there's a better
way.
It works reasonably well for now.

Building a site with Orchard CMS

I'm in the early stages of trying to learn Orchard, and I'm still seem to be struggling with the basics on how to build a page containing a multitude of various content that can be easily managed by non-technical users.
Ideally, what I'm trying to do is the following, I just can't figure out how to fit it into the Orchard architecture using Content Types, Parts, Fields, Widgets, Zones, etc. Also, since I'm still learning, I'm also trying to avoid any custom modules, or hard-coding content into the site (though I'm open to the idea, if that's the best way to get it done!).
Goal: Create a "home page" layout containing a Menu, Image slideshow, and several feature descriptions. For each image in the slideshow, I need a title, sub-title, description, and an image. To make this easy for non-technical users to manage, I would like to define the HTML template (custom Content Type, Part, or whatever), and allow authors to specify just those well-defined properties. I tried using Content Parts for this, but unfortunately, I can only have one Content Part of a particular type on a piece of Content. I also saw recommendations to create multiple Content Parts with the same set of properties, but I don't know how many images will be displayed (and I don't want to assign an arbitrary number).
I need to do something similar for feature descriptions, allowing authors to specify an image, title, description, and a page to link to. I'm running into the same problem as above, I'm not sure how to allow authors to specify a finite list of content, but have each content item be well-defined.
So far, the best option appears to be creating some sort of custom widget to "host" the content, but for some reason, my gut tells me that creating a custom layer for a single page just to specify which content to display is abusing the purpose of layers, which is begin able to customize a particular layout based on some criteria (whether or not the user is authenticated, for example).
I hope that made sense, and I apologize that it took so many words to explain my issue, I've just really reached my peak of frustration, and although I think that the Orchard guys definitely have it figured out in terms of architecture, I just can't get past these seemingly simple problems to build a simple website.
I greatly appreciate any tips, suggestions, advice this community has to offer!
TIA, -Jeremy
What you defined in Orchard terms is a Content Type named Feature.
Go to Contents -> Content Type, and click Create.
Select those parts by default:
Title, because you want your authors to provide a nice title/name for the features
Autoroute, which will create a SEO friendly url based on the Title (can be customized)
Click Save
Add specific Fields
SubTitle, of type TextField. Configure it to Default, Required.
Description, of type TextField. Configure it to TextArea, Required.
Image, of type Media Picker. Configure it to Required.
You can add some Hints to each fields, which will be displayed in the Feature editor to describe what to enter in each field. Very useful for authors.
Now you can create Features by clicking on the link in the top left part of the Dashboard.
Next step is to put those features on the homepage. What I suggest is to create a Projection which will be set as the homepage. A Projection is just a Page with an Url, which will display the result of a query as its content. The Query in your case will be "Give me all Features ordered by Creation Date".
In the dashboard, click on Queries
Click on "Create a new Query"
Enter "All Features"
Click on "Add a new Filter"
Select Content Type, then select Feature, Save
Click on "Add a sort Criterium"
Select "Creation Date", then Descending, Save
At this point, you can already preview the result of the query by clicking on Preview. But what we want is a front-end page.
Create a new Projection by clicking on Projection in the "New" section of the dashboard (top left again)
- Give it a title, and don't forget to check "Set as Home page" to make it the home page
- Select the only available query, named "All Featrues"
- Save
On the home page you should see all the features, ordered by date. But what you want is a slider. At that point you need two more steps:
- Integrate a slider jQuery plugin
- Render the HTML compatible with your jQuery plugin
By default, when you render a Projection it will use the standard "Summary" layout. But using projections you can decide exactly what layout you want to apply, and exactly what html tags and classes.
Edit the query named "All Features"
Add a new Layout
Select Html List
Select "Properties" and Save
Click Add Properties
Select Display Text, Save
Do the same for
Feature:SubTitle
Feature:Description
Feature:Image
Save your query
Edit the home page projection and select this specific Layout instead of the default one.
You will see that each property is rendered in an html container.
By editing each property you can decide which class to apply, and which html tag to use. By changing them you can render exactly what you want, and customize your CSS/HTML to render the slider nicely. This is purely your HTML know have to apply here, or find some articles about that.
For your editors, they just have to go to the dashboard and add/update some feature content items, it will be reflected on the website.
Optionally there is a Slider module on the Orchard Gallery. You can try this one too. But if you want to handle exactly what happens the technique I described is better.
I've just gone through this exact scenario myself. I think that what you want can be accomplished using Orchard Lists. The UX is not exactly intuitive for the end user, but it seems like it's the most straightforward way to accomplish this goal without installing/developing a custom module.
First, make sure you're using Orchard 1.4 because you'll need the built-in support for generating alternates for Container Widgets for each zone. Enable the Shape tracing, Url alternates and Widget alternates modules.
Now, create a list of items as described in the documentation (see http://docs.orchardproject.net/Documentation/Creating-lists). Translate "book review" into "slide" and "book reviews" into "slides" and you get the idea. You can add custom fields to represent your subtitle, description, etc. I'd just keep the image as part of the content itself instead of creating a custom field for it.
If you've followed the steps in the documentation, you should now be seeing a list of your slides rendered as ul/li's. Now you'll want to customize how things are rendered so you can show your custom fields and generally customize the tags. Use the shape tracer and create alternates for the list as you see fit. Now you're free to control all the rendering. See http://weblogs.asp.net/bleroy/archive/2011/03/27/taking-over-list-rendering-in-orchard.aspx and http://weblogs.asp.net/bleroy/archive/2011/05/23/orchard-list-customization-first-item-template.aspx for ideas.
Managing this list from your end-user's perspective is actually quite easy, but I don't really feel that it's very intuitive.
It took me three days of digging through blogs and docs to figure this out for myself.
I'd also recommend this wonderful module called Featured Item Slider. It has all the basic functionalities of a slideshow, including all mentioned in the question, plus some fantastic additional capabilities, such as different animations. You can also fine tune the layout and styling by providing additional css. Get the source here. And here is the slideshow in action. If a module already exists providing the functionality you're looking for, then it's best to use that rather than reinvent the wheel, unless you do it for learning purposes.

Loading data from database by Ajax - Ruby on Rails app

Sometimes at websites all comments or other data from DB is hidden by default. When user click at link like "Display comments" all comments from database are dynamically selected and placed under the content. It must be great for mysql performance, because content is generated only when user excatly need it. I would like to implement this stuff at my app.
I've got one idea to do this so far. Remote action with #comments = Content.comments and next page.insert_html at RJS template. Is it good idea or maybe I should choose different way?
The decision is purely based on the application that you are developing. For example if in case of stack overflow it does not make sense to show only the question and show answer link. But in case of a blog post it may be fine.
In the above situation, I don't think there will be a good improvement in performance by removing the comments of the content on show page. We can achieve the same functionality by making use of javascript methods. Hide the content on page load and show in on client request.

Rails 3.1 interface design - Cascading dropdown, form input

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.

ASP MVC Creating Form Rows Dynamically

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.

Resources