I'm having problems with an if, elsif, else statment in html.erb. I've seen a lot of questions around the if/else statements in erb but none that include elsif so I thought I'd ask for help.
Here is my html.erb:
<% if logged_in? %>
<ul class = "nav navbar-nav pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu pull-right">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: "delete" %>
</li>
</ul>
</li>
</ul>
<% elsif has_booth?(current_user.id) %>
<ul>
<li>TEST</li>
</ul>
<% else %>
<ul class="nav navbar-nav pull-right">
<li><%= link_to "Sign Up", signup_path %></li>
<li><%= link_to "Log in", login_path %></li>
</ul>
<% end %>
Here is my has_booths method:
module BoothsHelper
def has_booth?(user_id)
Booth.exists?(user_id: user_id)
end
end
I would like the header nav to have three different types of content for different users. The logged in user, the logged in user that has created a booth, and the logged out user. So far, I can only seem to make 2 out of the three work. I tried changing
<% elsif has_booth?(current_user.id) %>
to
<% elsif logged_in? && has_booth?(current_user.id) %>
and that did not work either. Am I writing my statement correctly? Any thoughts appreciated. Thanks.
The problem is that your first condition is true, so it stops there. Your first condition:
<% if logged_in? %>
Even if they don't have a booth it will never reach the elsif because the first condition is true. You either need:
<% if logged_in? && has_booth?(current_user.id) %>
// code
<% elsif logged_in? && !has_booth?(current_user.id) %>
// code
<% else %>
// code
<% end %>
Or it might be a cleaner approach to separate them into two if/else:
<% if logged_in? %>
<% if has_booth?(current_user.id) %>
// code
<% else %>
// code
<% end %>
<% else %>
// code
<% end %>
An even cleaner approach would be to flatten the statement, handling the not logged-in condition first so you don't have to test it along with whether they have a booth:
<% if !logged_in? %>
// not logged in code
<% elsif has_booth?(current_user.id) %>
// logged in and has booth code
<% else %>
// logged in and does not have booth code
<% end %>
You could also have used unless logged_in?, but the else and elsif don't make as much sense, semantically, with unless and therefore it doesn't read as clearly.
Related
I have a model, Friends, that I want to iterate in a view, but I'm having trouble with because I'm having to use uneven column widths.
Normally, if the column widths were constant, I would do some thing like this:
<% #user.friends.each do |friend|%>
<div class="col-md-6">
<%= friend.name %>
</div>
<% end %>
However, because of the layout I'm working with, I need to put an offset in the first column:
<div class="col-md-5 col-md-offset-1">
and I don't want the offset in the second column:
<div class="col-md-6">
How can I iterate through #user.friends with alternating column widths?
you can iterate through the list using each_with_index and add conditional css class based on odd/even value of the index
<% #user.friends.each_with_index do |friend, index| %>
<div class="<%= (index).even? ? 'col-md-5 col-md-offset-1' : 'col-md-6' %>">
<%= friend.name %>
</div>
<% end %>
how about
<% #user.friends.each_with_index do |friend, index|%>
<% if index == 0 %>
<div class="col-md-5 col-md-offset-1">
<%= friend.name %>
</div>
<% else %>
<div class="col-md-6"> #whatever offset u need
<%= friend.name %>
</div>
<%end%>
The following will give the 1st, 3rd, 5th, etc.. friend the offset.
<% odd = true %>
<% #user.friends.each do |friend|%>
<%= raw(odd ? '<div class="col-md-5 col-md-offset-1">' : '<div class="col-md-6">') %>
<%= friend.name %>
</div>
<% odd = !odd %>
<% end %>
Making a basic phonebook app and made some changes while figuring out the has_many and belongs_to relationships. I must have broke something because I have no idea why I'm getting this error. When I access my root, I get the following-->
ActionController::UrlGenerationError in ContactsController#index
No route matches {:action=>"show", :controller=>"contacts"} missing required keys: [:id]
The error shows mistakes in lines:
app/views/contacts/index.html.erb:10:in `block in _app_views_contacts_index_html_erb___2771775118522806317_70170309989460'
app/views/contacts/index.html.erb:7:in `_app_views_contacts_index_html_erb___2771775118522806317_70170309989460'
This is my contacts/index.html.erb
<p id="notice"><%= notice %></p>
<% if user_signed_in? %>
<h1>Listing Contacts</h1>
<% #contacts = current_user.contacts %>
<% #contacts.each do |contact| %>
<div class="link row clearfix">
<h2>
<%= link_to contact.name, contact_path %>
</h2>
</div>
<% end %>
<% end %>
<%= link_to "New Contact", new_contact_path %>
<% else %>
<h5> Welcome. Make an account or sign in above! </h5>
<% end %>
This is my config/routes
Rails.application.routes.draw do
resources :controllers
devise_for :users
resources :contacts so
resources :numbers
end
end
end
This is my contacts/show.html.erb
<div class="page-header">
<h1><%= #contact.name %><br> </h1>
</div>
<p>
<strong>Name:</strong>
<%= #contact.name %>
</p>
<p>
<strong>Email:</strong>
<%= #contact.email %>
</p>
<br>
<%= link_to 'Edit', edit_contact_path(#contact) %> |
<%= link_to 'Back', contacts_path %>
The output of my rake routes:
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
As you can see, I have a route for contacts#show so that's not the mistake. I'm unclear as to what it could be. Any ideas?
Seems like you're missing :id from the contact_path.
In contacts/index.html.erb, change this:
<%= link_to contact.name, contact_path %>
to this:
<%= link_to contact.name, contact_path(contact.id) %>
Ok so Ive looked around at many answers posted on stackoverflow and many other sites because I know that this is a commonly asked question but I just cant seem to get it and would really appreciate it if someone could point me in the right direction. What Im trying to achieve here is really simple, Im trying to render a new form from another model, Post into my homepage, which is another controller that I created. So basically in my homepage it shows a list of all the recent Post that were created by users. Furthermore I wanted to user to be able to directly add a new Post from the homepage. Right now I have it displaying however clicking on the submit button does absolutely nothing.
Here is the form for POST
<div class="panel panel-primary">
<div class="container">
<div class="panel-heading">
<%= image_tag #post.user.avatar.url(:post_pic), class:"img-thumbnail" %>
<%= #post.user.name %>
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.file_field :avatar %>
</div>
</div>
</div>
<div class="panel-footer">
<div class="actions">
<%= f.submit %>
</div>
</div>
<% end %>
</div>
Here is my homepage controller
class HomeController < ApplicationController
def index
if user_signed_in?
#posts = Post.all
#post = current_user.posts.build
else
redirect_to new_user_registration_path
end
end
def create
#post = current_user.posts.build(post_params)
#post.save
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :content, :avatar)
end
end
Lastly my homepage index
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-5">
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<%= render 'posts/form' %>
<% #posts.each do |post|%>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<%= image_tag post.user.avatar.url(:post_pic), class:"img-thumbnail" %>
<%= post.user.name %>
</h3>
</div>
<div class="panel-body">
<%= post.content %></br></br>
<% if post.avatar.file? %>
<%= image_tag post.avatar.url(:test), class:"img-thumbnail" %></br>
<% end %>
</div>
</div>
<% end %>
</div>
</div>
Routes
Rails.application.routes.draw do
resources :posts
get 'users/index'
get 'users/show'
root 'home#index'
devise_for :users
So, you're doing like saving post data using another Controller(Home) instead of Post Controller, right?
Your partial form for post uses:
<%= form_for(#post) do |f| %>
// #post generated url (/posts)
// will look for create method in POST Controller
I suggest to put your saving codes in proper controller(posts), so you will prevent code duplication/redundancy.
TIP:
If you are worrying about redirection from Home view or from Posts view,
you can use params[:controller] for checking where the request came from, by just using if statement.
Finally, you can redirect it to proper page after saving.
<div class="related">
<% if $RelatedOne.Title != null %>
<a href="$Link" class="play">
<div class="clip" style="background-image:url('$RelatedOne.VideoImage.CroppedImage(215,120).URL')">
<img src="$RelatedOne.VideoImage.CroppedImage(215,120).URL" alt="$RelatedOne.Title">
</div>
<span class="overlay">
<span class="title vertical-align">$RelatedOne.Title</span>
<span class="play-button"><i class="fa fa-play"></i></span>
</span>
</a>
<% else %>
<% include RelatedVideos %>
<% end_if %>
When the title is there the if condition is working properly but for others the else section is not working.Could anyone please point out my mistake.
I> doubt you can explicitly check against null in the template. But isn't $Title just a Varchar? So it won't be null, just empty...
Have you tried this:
<% if $RelatedOne.Title %>
This would check for "not an empty string".
I am using devise and simple form with rails. I can't figure out why a field with errors div or any div for that matter is not being created when there is a failed validation. I have tried deleting everything after f.input :first_name and still no new divs. Let me know what other info you might need to help me figure out my problem.
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div id="sign-up" class="row">
<div class="col-md-6 col-md-offset-3">
<%= render 'shared/error_messages' %>
<%= simple_form_for(resource, :as => resource_name,
:url => registration_path(resource_name),
html: {class: "well"}) do |f| %>
<ul class="inline-layout">
<li>
<%= f.input :first_name, required: false,
placeholder: 'First name',
label: false,
error: false,
input_html: {class: 'form-control inherit-width'} %>
</li>
Here is the error_messages partial:
<% if #user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(#user.errors.count, "error") %>.
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Edit:
Looks like I am getting a new div class 'error' but I want it to be 'field_with_errors' I tried changing all instances of 'error' in the simple_form.rb file but it didn't change anything. I'll just have to change my css.