How am I supposed to write the forms for my models where I'm using globalize3 for translations. I cannot find any examples and I don't find any helpers in the code.
The idea would be to have everything in one form like
text_field :title
text_field :title_fr
text_field :title_en
etc....
Thanks for pointing me to some code examples.
It looks like everything I need is in batch_translations fork adapted to Rails3/Globalize3:
https://github.com/fidel/batch_translations
I don't know if I supposed to delete this question or leave it for the future. Moderators please decide :)
Related
In the Railscast dealing with Editing Multiple, Ryan shows how to edit multiple objects in batch with form_tag. Is there a good way to do this using simple_form?
The base-case for using form_for or simple_form_for helpers are to hand them an ActiveRecord model...
<%= form_for #zebra do |f| %>
...which doesn't work in this case because you're not creating a form for one single instance of an ActiveRecord model. In the RailsCast, this is why Ryan uses form_tag.
There isn't a simple_form_tag, but you can hand simple_form a :symbol instead. Check out this SO post to see how some folks approached a similar problem. Know that at that point, you're writing a lot of extra code for simple_form to do what the RailsCast code does instead, and you lose a lot of the built-in niceties of simple_form.
There might be really good reasons why you'd battle with simple_form in this non-ideal scenario. If it were me, I'd start with the RailsCast code, get it working, and worry about niceties second. :)
My form is nested so that a Cart has Products, which can have Features. I wish to be able to add features to products via checkboxes.
Upon submit, rails is creating the Cart record, but failing to use "accepts_nested_attributes_for" to complete the addition of nested records.
My form is very straight forward, and I'm not receiving any errors, it's just ignoring the fields. My fields look like this (within the form helper)
= f.fields_for :feature_line_items do |builder|
= builder.check_box :id
= builder.label :id, "Feature Label"
Thanks in advance, I'm kind of doubting this is even possible, and probably need to rethink my data architecture.
Try this episode in railscasts. Also I suggest using simple_form gem, it makes forms programming very simple and straight forward.
I hope that helped.
Take a look :
http://asciicasts.com/episodes/17-habtm-checkboxes
http://ramblings.gibberishcode.net/archives/rails-has-and-belongs-to-many-habtm-demystified/17
We're using the (brilliant) acts_as_taggable_on Rails gem, allowing users to add tags to content they write (e.g. blog comment). We auto-suggest on as they type, but would also like to identify tags that we can suggest based on the user's content.
So if the user typed "We really loved the aquarium in Boston" and we had existing tags for "boston" and "aquarium" we might suggest those.
I think this is simple conceptually (iterate words, check the tags list, order by frequency of use), but there are little nuances, performance implications, and well, you know -- always harder than it looks.
Any suggestions for existing code or examples that might help me avoid recreating a wheel?
Thanks!
Well, I don't really know acts_as_taggable... But I think you can use something like :
Tag.find(:all, :conditions => { :name => title.split(' ').map(&:downcase) })
I am creating a blog in Rails using Scaffolding. I want to add a 'tags' field on each post like on StackOverflow and WordPress. I can do this with the string type ( rails generate scaffold post title:string body:text tags:string ) and then comma separated, but it's not good practice since I want the reader to browse by tags ( e.g. /tags/web20 or /tags/lol ). How can I do this?
Tagging is so common that implementations are a commodity. I believe "acts as taggable on" is usually the preferred way of implementing tags.
See other popular solutions here.
If you wish to implement it yourself, you could dive into the source code to find some ideas.
I would suggest creating a Tag model and using has_and_belongs_to_many to assign tags to posts. I don't know if the scaffold feature will help you create a form for that, but it shouldn't be difficult to add it yourself. I also suggest using the formtastic plugin as it's much easier and nicer to create forms with it.
Err, the usual way? Add Tag entity, add has_many :tags in your Post entity. Then migrate. That would be all.
What do you think is the best way to create SEO friendly URLs (dynamically) in Rails?
Override the to_param method in your model classes so that the default numeric ID is replaced with a meaningful string. For example, this very question uses best-permalinking-for-rails in the URL.
Ryan Bates has a Railscast on this topic.
ActiveSupport has a new method in Rails to aid this - String#parameterize. The relevant commit is here; the example given in the commit message is "Donald E. Knuth".parameterize => "donald-e-knuth"
In combination with the to_param override mentioned by John Topley, this makes friendlier URLs much easier.
rsl's stringex is pretty awesome, think of it as permalink_fu done right.
I largely use to_param as suggested by John Topley.
Remember to put indexes such that whatever you're using in to_param is quickly searchable, or you'll end up with a full table scan on each access. (Not a performance-enhancer!)
A quick work-around is to put the ID somewhere in there, in which case ActiveRecord will ignore the rest of it as cruft and just search on the id. This is why you see a lot of Rails sites with URLs like http://example.com/someController/123-a-half-readable-title .
For more detail on this and other SEO observations from my experience with Rails, you may find this page on my site useful.
For me friendly_id works fine, it can generate slugs too, so You don't need to matter about duplicated urls, scopes are also supported.
Check out the permalink_fu plugin (extracted from Mephisto)... the Git repository is located here.
I have made a small and simple gem which makes it easier to override the to_param method. It can be found here.