I've been using ActiveAdmin to create new posts for my news app, the problem is that even though I'm using the type "text' for my body property I don't know how to break lines when writing a new post.
That's my code:
ActiveAdmin.register Post do
permit_params :title, :body, :description, :remote_image_thumb_url, :remote_image_banner_url, :category_id
form do |f|
f.inputs "Post Details", :multipart => true do
f.collection_select :category_id, Category.all, :id, :name
f.input :title
f.input :body
f.input :description
f.input :remote_image_thumb_url
f.input :remote_image_banner_url
end
f.actions
end
end
I've been googling for a while but didn't fidn any solutions yet. Any ideas?
Rails saves the new lines when you hit the carriage return. However to render it is a different thing because it is naturally escaped.
You have a few options:
Use the default simple_format in rails.
Use a markdown renderer like Redcarpet to do that + alot more functionality.
Related
I have this code within my rails form:
Categories: <%= f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} %>
This code is working, but I want to use simpleform gem to redesign my form. However, I cannot seem to figure how to 'translate' this code into simple form. Anyone have any idea how? Thanks.
Something like this should do the trick:
If you have a many to many relation you could first try what the default does.
<%= f.association :tags %>
If the defaults don't work out you can make an explicit collection:
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name), label_method: :name, input_html: {multiple: true} %>
# or
<%= f.input :tag_ids, as: :select, collection: Tag.order(:name).pluck(:name, :id), input_html: {multiple: true} %>
Alternatively if you define the Tag#to_label method you don't have to pass the name of the label method. The Tag#id gets used as default value method. If you would like another value specify the method like so: value_method: :something_else.
See the simple_form Usage section (intro, collections and associations).
I am building a Rails app that tracks musicians. I want each user to be able to to select multiple genres they like from a form when they create a profile. I want to store it within the "profile" table for the user in the "genre" column. Right now, I have this, but it only allows me to select ONE option. I want the dropdown to allow multiple options. Is that possible? I'm sure this is very simple.
<%= simple_form_for #profile do |f| %>
<%= f.input :genre, collection:['Rock','Blues','Jazz','Classical','Soul','R&B','Alternative', 'Other'], label: "Favorite Genres" %>
Right now, genre is a string. Do I need to pass an array? How would I do that?
There are 2 things you need to do to handle a multi-select with rails.
1) Allowing the user to select multiple options on the form with the {:multiple => true} option.
<%= simple_form_for #profile do |f| %>
<%= f.input :genre, collection:['Rock','Blues','Jazz','Classical','Soul','R&B','Alternative', 'Other'], label: "Favorite Genres", input_html: {:multiple => true} %>
2) Let your controller accept an array of params for the value.
def profile_params
params.require(:profile).permit( :years_spent_playing, :user_id, :name, :bio, :age, :city, :state, :primary_instrument, :second_instrument, :third_instrument, :status, :looking_for, :image, :genre => [])
end
Im not sure your setup but I get the feeling you may not have the proper models and associations setup to handle this in the ideal way. I would suggest looking at adding a has_many :through association and breaking your genres out into a separate model and table.
I'm working on some app and I have 2 models. Categories in which I create category names and Question. Category has_many questions and Question belongs_to Category.
I've added category_id to Question model.
Now I need to take all Category_names and display them in form where I create Question so User can choose in which category_name will save question.
I've tried something like this in firs line of code but not working.
<%= f.input :category_id, Category.all.map(&:name) %>
<%= f.input :question_name, wrapper: :vertical_text_input, as: :text %>
<%= link_to "Markdown help", "http://assemble.io/docs/Cheatsheet-Markdown.html", target: "_blank", class: "right" %>
<%= f.input :answer %>
<%= f.input :image, as: :attachinary %>
QUESTION: How to display all Category names in form where I create new Questions?
you may wanna use a select box and use the collection select rails helper :
f.collection_select(:category_id, Category.all, :id, :name)
Also you can use this way.
<%= f.select :category_id, Category.all.map(&:name), {prompt:"Choose Category"}%>
#Jhon suggestion.
<%= f.select :category_id, Category.pluck(:name), {prompt:"Choose Category"}%>
I am using ActiveAdmin with rails 4 and it is working fine. When I add an item, I am taken to the index page listing the new item/items and the filters on the right side. Also, there are the email and logout links at the top right.
My issue is when I add an item with an image the filters, email and logout links disappear.
If I delete the item with the image they appear again. I can add /new to the url and it still takes me to the form but I am trying to figure out why adding a picture removes the links.
here is my form:
form(:html => { :multipart => true }) do |f|
f.inputs "Size" do
f.input :title
f.input :description
f.input :price
f.input :image, :as => :file
f.input :developer_days
f.input :designer_days
f.input :name
end
f.actions
end
I just can't figure out why this happens.
I am using Ruby on Rails (3.2.2), globalize3 (0.2.0) and batch_translations (0.1.2) ruby-gems. I would like to validate translated data handled by globalize3 as well as it should be. That is, for example, if...
... in my ROOT_RAILS/app/models/article.rb file I have:
class Article < ActiveRecord::Base
translates :title, :content
# This is needed to make the batch_translations to work.
attr_accessible :translations_attributes
accepts_nested_attributes_for :translations
validates :title,
:presence => true,
:length => ...
validates :content,
:presence => true,
:length => ...
...
end
... in my ROOT_RAILS/app/views/articles/_form.html.erb file I have:
<%= form_for(#article) do |f| %>
English translation:
<%= f.text_field :title %>
<%= f.text_field :content %>
Italiano translation:
<%= f.globalize_fields_for :it do |g| %>
<%= g.text_field :title %>
<%= f.text_field :content %>
<% end %>
<% end %>
I would like to validate data for title and content values when submitting the form and the current I18n.locale is not the I18n.default_locale. In other words, in order to store in the database correct information also for translations, I would like to validate title and content attributes when an user submit translation data for a locale language that is not the default English language.
Is it possible? If so, how?
Note: I took example from this question.
If I'm understanding you correctly, there's a few ways you could go about this.
1) A controller level test, where you test that a form submitted with the locale included persists to the correct localized content table. One way to do that would be something like this:
it "persists localized content for the Italian locale" do
Article.should_receive(:create).with(:locale => 'it', :title => 'title goes here', :content => 'this is content')
post :create, :locale => 'it', :title => 'title goes here', :content => 'this is content'
end
This basically says that the Article model should receive the create message with these arguments. I'm not entirely clear on how the batch_translations API works so you'll want to verify that this is how the parameters come in from that style form.
At that point, I think you'd be adequately testing that the data got persisted to the database correctly. Anything further would be testing that the Globalize gem works, which you want to avoid. Try and test the boundaries.