Rails Radio Button With New Records Help - ruby-on-rails

I have a Boards model and it has many Attachments. I want to be able to have ONE featured attachment. I have a featured_attachment_id column on the Boards table.
When I'm editing a board, I can select which attachment is the featured one. I'm doing this with a Radio button. Because each attachment is already persisted, I can easily get it's id and make it the featured attachment.
The problem is when I'm creating boards or I want the featured attachment to be a new one. Since it has no id, I have no idea how to know that this new attachment will be the featured one.
Any help on getting this done for new records? I'm using the Board.accepts_nested_attributes_for :attachment, so I can have the board_form.fields_for :attachments ...
Thanks a lot,
Nicolás Hock Isaza

It sounds like you have two different use cases:
make one of the current attachments
featured
make new attachment and mark it
featured
It sounds like you've already implemented a working version of #1 but need to build #2.
I suggest adding a new control on your upload form, perhaps a check box, that lets you flag that new upload as featured. You should be able to correlate that to your new upload in the controller and mark that attachment as featured after you've saved the attachment. You can't do this by attachment ID, but you should be able to do it by ordinal or other method (ie: 3 uploads, mark third as featured).

Related

Updating autorout URL from C# code in ORCHAD CMS

I have built a simple website and have the requirement to display all post from a blog for a year.
For example www.mysite.com/blogs/2015 should display all posts of 2015.
However, this year is not the real creation date of the post but it is given as input while creating the post (I have added new field to the editor).
I should also have a way to access a post with a unique URL like www.mysite.com/blogs/2015/2, which should display the second post of 2015.
The post number is unique for the year. So I cannot use the content id.
I was able to make this work with my own controller, but an issue remains:
When posts are listed it will attach the URL created by the AutoroutePart, which will be like www.mysite.com/blogs/first-post.
I attempted to change this URL while creating the post by updating the Path property of the AutoroutePart but had no luck.
Any suggestions or advise are appreciated.
You can define you own route for any content item which has the AutoroutePart.
Example in you situation you need to edit the content definition of the blog posts. So under the Content Definition menu you select Blog Post, then edit, then you click on the expando arrow next to the Autoroute label and you'll see the Patterns field where you can define your own pattern.

Ruby on rails AngularJS - create multiple models from direct upload

I recently am given a task by upper management to implement a mass upload coupon task. The goal is to allow users to upload an list of coupon codes (excel or csv), and subsequently giving these coupons codes attributes such as start date, expiry, quantity and so on.
I have already implemented a form to input these attributes. The current implementation method I am doing is as follows:
1.I upload a list of coupon codes (eg: 333245454dfee) and I do not write them directly to the database, instead I converted them to string and display on a page for the user to view. (done)
2.from that view page, there is a form with all the attributes. The user can then input these attributes. (done)
3.The user can create all of these coupons codes with the attributes set.
However, I am stuck now because I am unsure of how to mass create multiple codes and attach all the attributes to them. Right now, I can only create one coupon at a time.
So, to summarize, I would like to ask is it possible to
have a field that contains all the codes I have uploaded
I have others fields for different attributes
how to create all the codes I have uploaded as separate models.
I do not need the codes, I would like to hear what approach there are. I am thinking of creating a variable to store these coupons codes first, then loop them. But I have no idea how to do all of that by pressing one single button.
Thanks in advance.
Hmm, how I would approach this problem would be to have the user upload an csv file with 1 column and multiple rows of coupon. Using carrierwave to upload the file and parse the csv file. After parsing it, you should have an array of coupon and you can just input them into the database.
A good reference for your problem would be http://railscasts.com/episodes/396-importing-csv-and-excel

Rails variable photos on document

This is a generic question. My wife has company that has one-of-a-kind products such as the photo showed.
The challenge is, she wants to be able to create something like what is shown without doing it manually. The challenge is the products/photos are constantly changing so the photos that show would have to be dynamic and show the inventory.
Sometimes there are 20 products/photos other times there are 40... it seems to me the challenge would be in the view and changing the amount of photos that show up based on the change in inventory.
Thanks for your thoughts, this is a great community.
What I would do in this case is have a couple of models
1) Client - the peroson whom the product is for this would look like:
id
name
2) Album - this is how we can group all the photos in a collection
id
client_id
name
3) Image - this are the actual images you want to use in the collection
id
album_id
file
display_order
if you are still having question just ask
I will try to build a demo of of this.

How to tag data from one model to a separate controller in ROR?

Problem:
I am trying to create an app whereby a person can 'click' on tag buttons that are of different sports preferences (ie: "Running", "Involves Bats"), each of which are tagged to certain sports. So, if the user selects the tag "Involves Bats", then the app will generate all associated sports such as "Baseball", which would have "Involves Bats" as a pre built tag.
What I've Done So Far:
I have added the acts_as_taggable gem
I have created a model for "Sports", that has no controller.
I have in seeds.rb created defined the "Sports" model with names of each sport.
I have a Users controller, where someone can submit email which takes to them to the their user page.
What is Left
I need to tag all preferences (ie "Running") to all potential "Sports". I am having a really difficult time doing this.
I also need to on the Users Page, show the tags ("Running") from which to choose.
I need to let User select the tags, and then generate the recommendations of Sports based on the associated tags
I know this is a fairly complex question, but I believe is a fairly common/basic app to build.
Thanks!
You should check out Railscast episode #382 on tagging: it shows exactly what you need!
http://railscasts.com/episodes/382-tagging

Multiple File Uploads for a new record

I have implemented multiple file uploads for existing records as seen here https://github.com/websymphony/Rails3-Paperclip-Uploadify
I would like to know if it is possible to implement multiple file uploads when creating a new record.
Since we need flash to do the multiple file uploads, how would it be possible to associate the uploaded files with the record if the record has not yet been created.
I have thought of a hack-ish way to essentially make a "draft" and update it. However, I hope there is a better way to do this.
There is no better than the kind of hackish way you present:
creating orphans objects and give them parents later or delete them (sad huh? :) )
creating parent object by default, add some confirmation field in the form so that you know what objects really have an owner, delete the rest.
BTW, you don't "need" flash for multiple uploads, see here: http://blueimp.github.com/jQuery-File-Upload/
Yes, you can use http://blueimp.github.com/jQuery-File-Upload/. But there are still some points you need to be careful.
Don't forget to remove appended array after you define the file field with "multiple". For example: replace "photo[image][]" with "photo[image]".Otherwise file uploaders like "carrierware" will not be working.
If you are using rails 3.2.13, the appended array will always appear no matter whether you set the name to be without appended array. You can use "file_field_tag" to resolve this problem. Please refer this issue to: https://github.com/tors/jquery-fileupload-rails/issues/36.
For the association:
You need to create a hidden text field which contains IDs of images that will be associated to the record you are going to create.
Upload images by "jquery-fileupload"(it is ajax), then get IDs of images from response bodies.
Set these IDs to the hidden field.

Resources