I have the following loop to create a select dropdown using ERB. It is working correctly.
<%= f.select(:player_id) do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
My question is how do I add a class to the select element?
I have tried the following:
<%= f.select(:player_id), class: "form-control" do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
and
<%= f.select(:player_id), { class: "form-control" } do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
I have seen questions similar to this, but none that use a loop like the example above.
This should work for you.
<%= f.select :player_id, options_for_select( #players.collect { |player| ["#{player.first_name} #{player.last_name}", player.id] } ), class: 'form-control' %>
This is what I ended up doing:
<%= f.select :player_id, options_for_select( #players.collect { |player| ["#{player.first_name} #{player.last_name}", player.id] } ), {}, { class: 'form-control' } %>
I think problem with your code is f.select(:player_id).
You are calling the select method with only one parameter :player_id. So you can pass other options like this.
<%= f.select :player_id, class: "form-control" do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
Related
Unexpected end when end exists, But I don't see where the issue is.
My terminal keeps giving me the error: ActionView::SyntaxErrorInTemplate (Encountered a syntax error while rendering template:
my profile.html.erb
<% if #users.image.present? %>
<%= image_tag #users.image %>
<% end %>
<strong><h1><%= #users.full_name %></h1></strong>
<% if user_signed_in? %>
<% if #user == current_user %>
<%= link_to"Edit Profile", edit_user_registration_path(#user) %>
<% if user_signed_in? && !#user? %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
<% end %>
<% end %>
<div> <%= #users.posts.count %> Posts </div>
<p><%= #users.full_name %></p>
<p><%= #users.description %></p>
<p><%= link_to #users.website if #users.website.present? %></p>
<%= #posts.each do |post|%>
<%= image_tag post.image %>
<% end %>
<% if user_signed_in? && #user == current_user %>
<%= link_to"Edit Profile", edit_user_registration_path(#user) %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
now it's ok :D
Seems like issue is here
<p><%= link_to #users.website if #users.website.present? %></p>
Change it to
<p><%= link_to 'User Website', #users.website if #users.website.present? %></p>
I have seen two mistakes, please correct them. first there is nothing #user? that is biggest error, another might not be problem but there is no space <%= link_to"Edit Profile" Please try following code I have changed things and reformat it properly.
<% if #users.image.present? %>
<%= image_tag #users.image %>
<% end %>
<strong><h1><%= #users.full_name %></h1></strong>
<% if user_signed_in? %>
<% if #user == current_user %>
<%= link_to "Edit Profile", edit_user_registration_path(#user) %>
<% if user_signed_in? && !#user %>
<% if current_user.following?(#user) %>
<%= link_to"Unfollow", follows_path(user_id: #user.id), method: :delete %>
<% else %>
<%= link_to"Follow", follows_path(user_id: #user.id) %>
<% end %>
<% end %>
<% end %>
<% end %>
<div> <%= #users.posts.count %> Posts </div>
<p><%= #users.full_name %></p>
<p><%= #users.description %></p>
<p><%= link_to #users.website if #users.website.present? %></p>
<%= #posts.each do |post|%>
<%= image_tag post.image %>
<% end %>
I am using a theme that has very specific layouts and I wanted to make a failsafe way to you rails forms.
I have a layout app/views/shared/forms/fields/_layout.html.erb
<div class="js-form-message mb-4">
<div class="js-focus-state input-group u-form">
<div class="input-group g-brd-primary--focus">
<%= yield(:field) %>
</div>
</div>
</div>
And I have two partials.
1-st partial: app/views/shared/forms/fields/_email.html.erb
<% form = locals[:form] %>
<% locals[:required] = locals[:required].nil? ? true : locals[:required] %>
<% locals[:placeholder] = locals[:placeholder] || t('forms.shared.email.placeholder') %>
<%= render layout: "shared/forms/fields/layout", locals: locals do %>
<% content_for(:field) do %>
<%= form.email_field :email,
placeholder: locals[:placeholder],
class: "form-control g-py-15 g-px-15",
"data-error-class"=>"u-has-error-v1-3",
"data-success-class"=>"u-has-success-v1-2",
"data-msg-email" => t('forms.shared.email.validate'),
"data-msg" => t('forms.shared.required'),
autofocus: locals[:autofocus],
required: locals[:required] %>
<% end %>
<% end %>
2-nd partial: app/views/shared/forms/fields/_login.html.erb
<% form = locals[:form] %>
<% locals[:required] = locals[:required].nil? ? true : locals[:required] %>
<% locals[:placeholder] = locals[:placeholder] || t('forms.shared.login.placeholder') %>
<%= render layout: "shared/forms/fields/layout", locals: locals do %>
<% content_for(:field) do %>
<%= form.email_field :login,
placeholder: locals[:placeholder],
class: "form-control g-py-15 g-px-15",
"data-error-class"=>"u-has-error-v1-3",
"data-success-class"=>"u-has-success-v1-2",
"data-msg" => t('forms.shared.required'),
autofocus: locals[:autofocus],
required: locals[:required] %>
<% end %>
<% end %>
And when I do this:
<%= render "shared/forms/fields/email", locals: {form: f} %>
<%= render "shared/forms/fields/login", locals: {form: f} %>
I get
Email Field
Email Field/Login Field
I found out that content_for 'appends' the block that you give it and then when I yield the whole block is returned.
The first time there is nothing in content_for(:field) and it appends to it Email Field. But the second time it does not clear its content and just appends Login Field to it.
I am thinking of adding additional complexity to layout.html.erb so just keeping it inline isn't an option.
Is there a way to tell to the layout only to yield the 'newest' value of content_for.
EDIT:
I wrote a method to flush after an yield, suggesting that the same key would be used again:
def yield_and_flush!(content_key)
view_flow.content.delete(content_key)
end
content_for has flush option to reset previous content:
<% content_for :field, flush: true do %>
new content here
<% end %>
The solution was this to write an yield_and_flush! method. I saw the solution here
def yield_and_flush!(content_key)
view_flow.content.delete(content_key)
end
I've been working on a problem for a while and I feel like my solution should be right, but it keeps giving me "freeze; end" error.
I am doing a restaurant search and I am trying to let users set parameters to help narrow down the restaurants they want.
Below is my html code:
<!DOCTYPE html>
<html>
<body>
<%= form_tag home_path do %>
<%= label_tag(:cuisine, "Cuisine:" %>
<%= text_field_tag (:cuisine) %>
<%= label_tag(:zipcode, "Zipcode:" %>
<%= text_field_tag (:zipcode) %>
<%= label_tag(:lower, "lowerScore:" %>
<%= text_field_tag (:lower) %>
<%= label_tag(:higher, "higherScore:" %>
<%= text_field_tag (:higher) %>
<%= submit_tag("Search") %>
<% end %>
<% #my_search.each do|search| %>
<%= search.name %>
<% end %>
</body>
</html>
and in my welcomes_controller in the def home:
#my_search = Restaurant.joins(:inspection).where(cuisine: params[:cuisine], zipcode: params[:zipcode], totalscore: params[:lower..:higher])
Why am I receiving errors from this?
Thank you for everyone's help so far, I think its all coming together now
please change this
#my_search = Restaurant.joins(:inspection).where(cuisine: params[:cuisine}, zipcode: params[:zipcode], totalscore: params[:lower..:higher])
to this
#my_search = Restaurant.joins(:inspection).where(cuisine: params[:cuisine], zipcode: params[:zipcode], totalscore: params[:lower..:higher])
secondly this label_tag does not seem right. where is the end of this (
for example change this
label_tag(:cuisine, "Cuisine:"
to this
label_tag :cuisine, "Cuisine:"
I am doing a basic validation for a bank entry and am trying to display the errors on submit. Here is my validation and html.
controller:
class PagesController < ApplicationController
def index
#guestbook_entries = GuestbookEntry.all
#guestbook_entry = GuestbookEntry.new
render "welcome"
end
end
class GuestbookEntry < ActiveRecord::Base
validates :body, presence: :true
end
This is my html:
<% form_for #guestbook_entry do |f| %>
<% if f.errors.any? %>
<% f.errors.full_messages.each do |m| %>
<li><%= m %></li>
<% end %>
<% end %>
<%= f.label :body, "Guestbook Entry:" %>
<%= f.text_area :body %>
<%= f.submit "Submit" %>
<% end %>
Any ideas?
I have a nested form.
Right now I want to arrange the layout with some CSS but I am facing trouble allocating dom ids to the form.
This is the subject controller.
I want to allocate lesson_type as seen in line 5 as the dom id.
1 def index
2 #subjects = Subject.all
3 #subject = Subject.new
4 lecture = #subject.lessons.build
5 lecture.lesson_type = "lecture"
lecture.lesson_groups.build
lecture.destroy
tutorial = #subject.lessons.build
tutorial.lesson_type = "tutorial"
tutorial.lesson_groups.build
tutorial.destroy
laboratory = #subject.lessons.build
laboratory.lesson_type = "laboratory"
laboratory.lesson_groups.build
laboratory.destroy
respond_to do |format|
format.html # index.html.erb
format.json { render json: #subjects }
format.js
end
end
The following is the form.
<%= nested_form_for(#subject, :remote=>true) do |f| %>
<% if #subject.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#subject.errors.count, "error") %> prohibited this subject from being saved:</h2>
<ul>
<% #subject.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :subject_code %><br />
<%= f.text_field :subject_code %>
</div>
<%= f.fields_for :lessons do |lesson| %>
<%= lesson.label :lesson_type %><br/>
<%= lesson.text_field :lesson_type, :readonly=>true%><br/>
<%= lesson.label :name %><br/>
<%= lesson.text_field :name %><br/>
<%= lesson.fields_for :lesson_groups do |lesson_group| %>
<%= lesson_group.label :group_index %><br/>
<%= lesson_group.text_field :group_index %>
<%= lesson_group.link_to_remove "Remove this task" %>
<% end %>
This is the div where I want to add an id to.
<%= f.fields_for :lessons do |lesson| %>
<%= lesson.label :lesson_type %><br/>
<%= lesson.text_field :lesson_type, :readonly=>true%><br/>
<%= lesson.label :name %><br/>
<%= lesson.text_field :name %><br/>
I have tried out the following but it did not worked.
<div id = "<%= :lesson_type%>">
Would appreciate it if someone could help me out thanks.
sorry..
#controller
def index
...
lecture.lesson_type = #lesson_dom_id = "lecture" # line 5
...
end
#view
<div id="<%= #lesson_dom_id %>">