f.collection_select not showing up - html

I'm working in rails 4 with simple_form and I have two models, one called content and the other one called category. Category has many content and the content belongs to a category. The problem is that when i want a collection_select to show in my form is not showing.
Here is the code:
<%= simple_form_for #content, html: { multipart: true } do |f| %>
<%= f.input :title, required: true %>
<%= f.input :tagline, required: true, label: 'Short description', input_html: { maxlength: 20 }%>
<%= f.input :description, required: true %>
<%= f.input :price, required: true %>
<%= f.input :team, required: true, label: 'Team member number' %>
<%= f.input :equity, required: true, label: 'Equity percentage'%>
<%= f.input :website, label: 'Website Link', as: :string%>
<%= f.input :linkedin, label: 'Linkedin Link', as: :string %>
<%=f.collection_select :category_id, Category.all, :id, :name, {prompt: "Chose a category"} %>
<br>
<%= f.input :copertina, required: true, label: 'Image Cover' %>
<br>
<%= f.button :submit %>
<% end %>
Thanks for the help.
Edit: Could it be a js problem?

Have you tried <%= f.association :categories %>?

For selecting the associated Category for an instance of Content, #content)
collection_select(:content, :category_id, Category.all, :id, :name, prompt: true)

= f.select(:category_id, Category.all.collect {|c| [c.name, c.id] },{include_blank: true} )
Hope, that helps!

Related

How to disable date_select field of form when checkbox is checked?

I have defined these fields:
<%= f.label :acc_type, 'Trial mode' %>
<%= f.check_box :acc_type %>
<%= f.label :expire_date, 'Expire date' %>
<%= f.date_select :expire_date, { discard_day: true, start_year: Date.today.year, end_year: (Date.today.year + 10), required: true }, class: 'form-control' %>
I'd like the expire_date to disable when I check the checkbox. How can I do this?
Thank you in advance.
you can use jquery to do this.
This is my idea, you can try.
For example:
<%= f.check_box :acc_type, id: "id_check_box" %>
<%= f.date_select :expire_date, { discard_day: true, start_year: Date.today.year, end_year: (Date.today.year + 10), required: true }, class: 'form-control', id: "id_expire_date" %>
<script>
$('#id_check_box').click(function() {
if (this.checked) {
$('select#id_expire_date').attr('disabled', 'disabled');
} else {
$('select#id_expire_date').removeAttr('disabled');
}
});
</script>

Handle sign-up failure fails

I did make a attribute called address before . But I didn't use it in the sign up form . When I input all text field in sign up form , it flashes the error
The form contains 1 error.
Address can't be blank
Any other places I should check with ?
Controller
def new
#tutor = Tutor.new
end
def create
#tutor = Tutor.new(tutor_params)
if #tutor.save
log_in #tutor
flash[:success] = "Congratulations! Your registration is successful!"
redirect_to #tutor
else
render 'tutors/new'
end
end
# Handle sign-up failure, to redirect the tutor to the registeration form again
def tutor_params
params.require(:tutor).permit(:name, :email, :password, :password_confirmation,:gender
,:education_level,:institution,:exprience,:district,:subject,:student_level)
end
Sign Up page
<%= form_for(#tutor) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.label :gender %>
<%= f.select(:gender, ['Male', 'Female'] , class: 'form-control' )%>
<%= f.label :tutor_education_level %>
<%= f.select(:education_level, ['Bachelor', 'Master', 'Doctor'] , class: 'form-control' )%>
<%= f.label :tutor_institution %>
<%= f.text_field :institution, class: 'form-control' %>
<%= f.label :tutorial_experience %>
<%= f.text_field :experience, class: 'form-control' %>
<%= f.label :tutor_preferred_district %>
<%= f.text_field :district, class: 'form-control' %>
<%= f.label :tutor_preferred_subject %>
<%= f.text_field :subject, class: 'form-control' %>
<%= f.label :tutor_required_student_level %>
<%= f.select(:student_level, ['P1-P3', 'P4-P6', 'S1-S3', 'S4-S6'] , class: 'form-control' )%>
<%= f.submit "Create tutor's account", class: "btn btn-primary" %>
<% end %>
_error_messages.html.erb
<% if #tutor.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(#tutor.errors.count, "error") %>.
</div>
<ul>
<% #tutor.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
db/schema.rb(uupdate)
create_table "tutors", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "address"
t.string "remember_token"
t.string "gender"
t.string "education_level"
t.string "institution"
t.integer "experience"
t.string "district"
t.string "subject"
t.string "student_level"
end
add_index "tutors", ["email"], name: "index_tutors_on_email", unique: true
add_index "tutors", ["remember_token"], name: "index_tutors_on_remember_token"
end
tutor.rb (update2)
`class Tutor < ActiveRecord::Base
before_save {self.email = email.downcase}
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
has_secure_password
validates :address, presence: true, length: {maximum: 100}
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
`
Change
validates :address, presence: true, length: {maximum: 100}
to
validates :address, length: {maximum: 100}
in your model tutor.rb
You are validating for the presence of address field in your model while creating a tutor, but you are not passing address params.
If you want to delete existing address field from db,
In your terminal, do
rails g migration remove_address_from_tutors
Add the following code in your newly created migration file
class RemoveAddressFromTutors < ActiveRecord::Migration
def change
remove_column :tutors, :address
end
end
and then do rake db:migrate in your terminal
def create
#tutor = Tutor.new(tutor_params)
if #tutor.save
log_in #tutor
flash[:success] = "Congratulations! Your registration is successful!"
redirect_to #tutor
else
flash[:tutor] = #tutor
redirect_to new_tutor_path
end
end
Also make sure your validates_presence_of :address in your tutor model.

how do I style a ruby select in bootstrap?

I want to wrap this line of ruby in a select style in bootstrap but every time I do it breaks and lists all the generated options outside the form.
Here in the code I want to wrap
<%= f.label :user_description_id %>
<%= f.collection_select(:user_description_id, UserDescription.all, :id, :description, include_blank: true) %>
You should be doing somethin linke this:
<%= f.label :user_description_id %>
<%= f.collection_select(:user_description_id, UserDescription.all, :id, :description, {include_blank: true}, {class: "css-class"} ) %>
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

Do Devise forms require a label for each textfield?

Does Devise require a label for each textfield in its forms (ie: registration/new)?
When i delete the label in my code, ie:"<%= f.label :name %>", the textfield also disappears on the page. I want to show the textfield, but not the label.
devise/registrations/new.html.erb
<div class="row">
<div class="col-md-8">
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="form-group>
<%= f.label :name %>
<%= f.text_field :name, autofocus: true, class: 'form-control', placeholder: 'Name' %>
</div>
<div class="form-group>
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control', placeholder: 'Email' %>
</div>
<div class="form-group>
<%= f.label :password %>
<%= f.text_field :password, class: 'form-control', placeholder: 'Password' %>
</div>
<div class="form-group>
<%= f.label :password_confirmation %>
<%= f.text_field :password_confirmation, class: 'form-control', placeholder: 'Confirm password' %>
</div>
<div class="form-group">
<%= f.submit "Sign up", class: 'btn btn-success' %>
</div>
<div class="form-group">
<%= render "devise/shared/links" %>
</div>
<% end %>
</div>
</div>
After deleting label
Before deleting label
I've read its bad practice to exclude a label outside of a a textfield in a form, but Facebook does it ;)
I'm still new to RoR and any guidance is appreciated.
Does Devise require a label for each textfield
No.
Devise is a gem.
It's built using the same helpers and code as every other Ruby/Rails app. It does not have any special requirements to use labels.
In fact, the form helpers of Rails, and indeed every other helper, outputs vanilla HTML. If you can get away with not having labels in html, you can do it with your form helper.
Here's our code for a devise registration form (in HAML):
#app/views/devise/registrations/new.haml
.authenticate
= render "devise/shared/title"
= simple_form_for(User.new, as: resource_name, url: registration_path(resource_name)) do |f|
- if flash[:error]
.form-errors= flash[:error]
= f.error_notification
.form-inputs
= f.input :email, required: true, autofocus: true, placeholder: "Email"
%hr
= f.input :password, required: true, placeholder: "Password"
= f.input :password_confirmation, required: true, placeholder: "Confirm"
.form-actions
.bar
= link_to "", "#", rel: "submit", class: "ion-forward", title: "Register", data: { placement: "right" }
= render "devise/shared/links"
You should try the following:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.text_field :name, autofocus: true, class: 'form-control', placeholder: 'Name' %>
<%= f.text_field :email, class: 'form-control', placeholder: 'Email' %>
<%= f.text_field :password, class: 'form-control', placeholder: 'Password' %>
<%= f.text_field :password_confirmation, class: 'form-control', placeholder: 'Confirm password' %>
<%= f.submit "Sign up", class: 'btn btn-success' %>
<%= render "devise/shared/links" %>
<% end %>
Remove any HTML elements to make sure they're not preventing Rails from showing the elements properly. If it works, you'll be able to rebuild the HTML and CSS to see what the problem is.

How to embed additional code in link_to

I'm trying to embed additional code in a link_to but not sure how to get it to work. How do I go about doing that? I'm trying to get this to work:
<%= link_to image_tag("Favorite 3.png", class: "act_actions", title: "Unfavorite", alt: "Unfavorite") + <%= activity.votes.size %>, favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" %>
I would try to use linkt_to as block to gain readability
<%= link_to favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" do %>
<%= image_tag("Favorite 3.png", class: "act_actions", title: "Unfavorite", alt: "Unfavorite") %>
<%= activity.votes.size %>,
<% end %>
I hope that the above code works for you.
you can do it this way also.
<%= link_to "#{image_tag('Favorite 3.png', class: 'act_actions', title: 'Unfavorite', alt: 'Unfavorite')} #{activity.votes.size}".html_safe, favorite_activity_path(activity), method: :put, :remote => true, :class => "btn favorite" %>