<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".
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 %>
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.
I have a set up where Goals have Steps and Steps have Tasks. In my view I am showing a goal, if that goal has steps then I can pull those and show them as well. I am having a hard time showing the tasks for each individual step now. I can get the task associated with the proper step so part of this is working, but I cannot access any of the data, name and description specifically for the task.
I am just unsure how to create this in the controller. I have cruised around the guide trying to find a solution and tried things like
#goal.steps.tasks
#step.tasks
I just cannot find anything that clicks for creating the variable in the controller so I can access it.
goals_controller.rb show method
def show
#goal = Goal.find(params[:id])
#steps = #goal.steps(page: params[:page])
# #tasks = ?
end
goal.rb
class Goal < ActiveRecord::Base
has_many :steps, dependent: :destroy
has_many :tasks, through: :steps
end
step.rb
class Step < ActiveRecord::Base
belongs_to :goals
has_many :tasks, dependent: :destroy
default_scope -> { order(created_at: :desc) }
end
task.rb
class Task < ActiveRecord::Base
belongs_to :steps
end
views
goals/show view
<p>
<% if #goal.steps.any? %>
<!-- Projects Row -->
<div class="row">
<%= render #steps %>
</div>
<!-- /.row -->
<% else %>
No steps for this goal.
<% end %>
</p>
the partial rendered for the steps
<div id="steps-<%= step.id %>" class="col-md-3 portfolio-item not completed" style="background-color:#e26a5c;border-radius:10px;border:1px solid #6cbdc4;margin:0 5px 5px 0;">
<h3>
<p>In order to achieve this goal I need to <strong><%= step.name %></strong> in <strong><%= time_ago_in_words(step.deadline) %></strong></p>
</h3>
<img src="/img/icon-edit.png">
<% if step.tasks.any? %>
<%= render "tasks/task" %>
<% else %>
<%end %>
</div>
<% else %>
<div id="steps-<%= step.id %>" class="col-md-3 portfolio-item completed" style="background-color:#6cbdc4;border-radius:10px;border:1px solid #e26a5c;margin:0 5px 5px 0;">
<h3>
<p>In an effort to achieve this goal I needed to <strong><%= step.name %></strong> in <strong><%= time_ago_in_words(step.deadline) %></strong></p>
</h3>
<img src="/img/icon-edit.png">
</div>
Tasks/task is the partial I am trying to pull in, I can link to it on the correct step and everything, but it says undefined method when I put task.name or task.description in.
Try this in your goals_controller.rb:
def show
#goal = Goal.find(params[:id])
#steps = #goal.steps(page: params[:page]).includes(:tasks)
end
Then in your view you could have something like this:
<ul>
<% #steps.each do |step| %>
<li>
<%= step %>
<p>Tasks:</p>
<ul>
<% step.tasks.each do |task| %>
<li><%= task %></li>
<% end %>
</ul>
</li>
<% end %>
</ul>
For more info on includes: http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations
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.
I want to check – in the template .ss file – the page name or title in order to show a different image. I'd like to do something like that:
<% if $SiteConfig.Title == 'video' %>
<img src="themes/blackcandy/images/image.jpg" />
<% else if $SiteConfig.Title == 'bio' %>
<img src="themes/blackcandy/images/image1.jpg" />
...
...
<% end_if %>
The code above of course doesn't work. How do I do it? Is there a more efficient way of doing it?
Thanks a lot.
Mauro
If you want to check it throu the title use $Title (without the SiteConfig controller)?
<% if $Title == 'video' %>
<img src="themes/blackcandy/images/image.jpg" />
<% else if $Title == 'bio' %>
<img src="themes/blackcandy/images/image1.jpg" />
<% end_if %>
If you want to check PageType use ClassName
<% if ClassName = PageType1 %>
<img src="themes/blackcandy/images/image1.jpg" />
<% end_if %>
If you want to check throu Url use UrlSegment (see Clints answer)
It's best to use URLSegment for this.
<% if URLSegment = video %>
<img src="themes/blackcandy/images/video-image.jpg" />
<% else_if URLSegment = bio %>
<img src="themes/blackcandy/images/bio-image.jpg" />
<% end_if %>
Thanks to banal at: http://www.silverstripe.org/themes-2/show/11325
assuming you're on silverstripe 2.4:
<% control SiteConfig %>
<% if Title = video %>
video
<% else_if Title = bio %>
bio
<% end_if %>
<% end_control %>
note not to put quotes around the values (bio instead of 'bio').
i supposed the following to be valid too:
<% if SiteConfig.Title = bio %>
but for some reason it doesn't work - not sure about this.