<ol class="noDots">
<% # #screening.cinema.each do |screening| %>
<li>
<h3><%= screening.cinema.name %></h3>
</li>
<% #end %>
</ol>
Hi everyone,
There is an association between cinema and film through screening. Film model is separate and cinema is separate. Screening combines them together. In the cinema model, everything seems to work properly <%= screening.film.title %> works perfectly and displaying them in a loop works.
I want to create a dropdown of cinema.name with the link and it takes you there. The association has to exists as some movies in different cinemas.
Is that possible as the code above doesnt work for me.
Thanks in advance
<h3><%= screening.cinema.name %></h3>
This line should read...
<h3><%= screening.name %></h3>
And you could have this dropdown box in a form with a Submit button next to it to take you to that page...
Sounds like you are getting a lot "nil object" errors. Double check your controller to see what variables you are defining there that can be accessed in the views.
Eg, if you are setting #film, then you can traverse the associations with something like:
<% #film.screenings.each do |screening| %>
<li>
<h3><%= screening.cinema.name %></h3>
</li>
<% end %>
You should be able to alphabetize by cinema name as well, if you eager-load them when you grab the list of screenings:
<% #film.screenings.includes(:cinema).order("cinema.name ASC").each do |screening| %>
<li>
<h3><%= screening.cinema.name %></h3>
</li>
<% end %>
Hope that helps!
From what I see screen has_many :cinemas( not cinema has_many :screenings)
so
<% #screening.cinema.each do |cinema| %>
<li>
<h3><%= cinema.name %></h3>
</li>
<% end %>
please show the screening model and the cinema mode, but based on the gist..
Assuming
class Screening
belongs_to :film
belongs_to :cinema.
.....
end
class Film
has_many :screenings
has_many :cinemas, :through => :screenings
end
class Cinema
has_many :screenings,
has_many :films, :through => screenings
end
I think what you want is something along the lines of the following.
<ol class="noDots">
<% # #screenings.each do |screening| %>
<li>
<h3><%= screening.cinema.name %></h3>
</li>
<% #end %>
</ol>
Assuming that you set #screenings to subset of screenings you want in the controller..
This should work as it worked on your single screening in the comments.
In the controller
#screening = Screening.find(some_id)
In the view
<% #screening.cinema.each do |s| %>
<li>
<h3><%= s.cinema.name %></h3>
</li>
<% end %>
Related
I'm currently learning Ruby on Rails thanks to a website named codecademy, and I'm learning how to display informations from a database's array stocked into a variable
The exercice's correction is as shown below :
<div class="main movie-show">
<div class="container">
<div class="movie">
<!-- Display the movie info here -->
<div class="info">
<%= image_tag #movie.image %>
<h3 class="movie-title"><%= #movie.title %></h3>
<p class="movie-release-year"><%= #movie.release_year %></p>
<p class="movie-plot"><%= #movie.plot %></p>
</div>
</div>
<h2>Cast</h2>
<% #actors.each do |actor| %>
<div class="actor">
<%= image_tag actor.image %>
<h3 class="actor-name"><%= actor.first_name %> <%= actor.last_name %></h3>
<p class="actor-bio"><%= actor.bio %></p>
</div>
<% end %>
</div>
</div>
You can see in the "movie" part that they directly take the variable to display the information needed, while they stock all the "actor" 's part in another |actor| variable
My question is the following, as I didn't find any satisfying answer online, is it possible to use two variables the same way in the same file ? Like using
<% #movies.each do |m| %>
and
<% #actors.each do |a| %>
Will it work anyway ? Will there be an error?
You can use as many number of instance variables as you want in your view provided that they are properly defined in your controller code.
If you have defined both #actors and #movies instance variables in your controller action, then you can access then them in corresponding view. Remember: I wrote, corresponding view.
There is other way as well. For example, if you have defined relation between your Movie model and your Actor model, and the relation states that a movie can have many actors. In that case, you only need to instantiate #movies in your controller, and then you can access actors in the following way:
<% #movies.each do |movie| %>
<% movie.actors.each do |actor| %>
<%# All other relevant code %>
<% end %>
<% end %>
In case, you don't know about relations, you can define them in following way:
class Movie < ActiveRecord::Base
has_many :actors
end
class Actor < ActiveRecord::Base
belongs_to :movie
# actors table should have a column named 'movie_id' for this to work
end
I have this code which associates one table to another. Using Set it collects all data and only shows it once if there are other similar values.
genre_names = Set.new
<% #pm_relationships = PmRelationship.where(:people_id => #person.id) %>
<% #pm_relationships.each do |pm_relationship| %>
<% #movie=Movie.find(pm_relationship.movie_id) %>
<% #mg_relationships = MgRelationship.where(:movie_id => #movie.id) %>
<% #mg_relationships.each do |mg_relationship| %>
<% #genre=Genre.find(mg_relationship.genre_id) %>
<% genre_names.add(#genre.name) %>
<% end %>
<% end%>
# actual view code
<ul class="basic-info-genres">
<%= "<li>#{genre_names.to_a.join('</li><li>')}</li>".html_safe %>
</ul>
My problem here is how a link_to would work in the print code provided
<%= "<a><li><button><span>#{genre_names.to_a.join('</span></button></li></a><a><li><button><span>')}</span></button></li></a>".html_safe %>
How to make the above print to have this link to /genres/<%=#genre.id%>?
I've tried
<%= "<a href='/genres/#{#genre.id}'><li><button><span>#{genre_names.to_a.join('</span></button></li></a><a><li><button><span>')}</span></button></li></a>".html_safe %>
but this only links to the first genre shown
Any ideas?
Thanks
Add to genres the whole #genres, not only the #genre-name, then you can use the each-loop in the view code.
For the Controller (you should have your programmic logic there):
#genres = Set.new
#pm_relationships = PmRelationship.where(:people_id => #person.id)
#pm_relationships.each do |pm_relationship|
movie=Movie.find(pm_relationship.movie_id)
#mg_relationships = MgRelationship.where(:movie_id => movie.id)
#mg_relationships.each do |mg_relationship| %>
genre=Genre.find(mg_relationship.genre_id) %>
#genres.add(genre) %>
end
end
For the view:
<ul class="basic-info-genres">
<% #genres.each do |genre| %>
<li><%= link_to genre.genre_name, "#{root_url}genres/#{genre.id}" %></li>
<% end %>
</ul>
If I understand correctly, you want to link all genres that are associated with a movie.
To do this, in your controller, load the genres for a user's movies. With the correct association setup, this is as easy as #person.genres. So let's do that.
class Person < ActiveRecord::Base
has_many :pm_relationships
has_many :movies, through: :pm_relationships
has_many :genres, through: :movies
end
class PmRelationship < ActiveRecord::Base
belongs_to :person
belongs_to :movie
end
class Movie < ActiveRecord::Base
has_many :mg_relationships
has_many :genres, through: :mg_relationships
end
With that setup, in your controller, just setup an instance variable to list all genres for #person
#genres = #person.genres
Then in your view, use the block form of link_to so it's easier to code your html
<ul class="basic-info-genres">
<% #genres.each do |genre| %>
<%= link_to genre do %>
<li>
<button>
<span><%= genre.name %></span>
</button>
</li>
<% end %>
<% end %>
</ul>
Basically, you can't have any direct child element instead of li within ul. You will need to have <a> within li, however, we can apply css to <a> so that it looks like whole 'li' have the clickable link.
From your actual code, update the snippet.
<ul class="basic-info-genres">
<% genre_names.each do |genre_name| %>
<li>
<%= link_to genre_name, genre_path(#genre), :class => 'clickable-li' %>
</li>
<% end %>
</ul>
You need to set your <a> to display: block;
# css file
.clickable-li {
display: block;
}
I have looked on the internet and stared at my screen for hours now and hope that you guys can help me out and can stop me from thinking in circles :) I've already found http://railscasts.com/episodes/196-nested-model-form-part-1 and http://railscasts.com/episodes/197-nested-model-form-part-2 but still can't get my mind around it...
Context
I'm trying to develop a CMS where users can create dynamic forms, with their own specific questions, question help text and possible answers (in case of radio buttons/checkboxes)
I am able to create the questions and the forms and to show the question in the viewer, my next step is to allow other users to answer the questions, but can't figure out how to do that.
What I have so far:
Form model:
has_many :custom_questions
has_many :custom_themes
has_many :form_submissions
has_many :answers
accepts_nested_attributes_for :answers
Question model
belongs_to :custom_form
belongs_to :custom_theme
has_many :possible_answers
accepts_nested_attributes_for :possible_answers
has_many :answers
accepts_nested_attributes_for :answers
Answer model
belongs_to :custom_questions
belongs_to :possible_answers
belongs_to :form_submissions
and the viewer which I use to load the questions and input fields:
<div class='content custom_forms clearfix'>
<%= form_for #customForm do |f| %>
<ul>
<%= #customForm.custom_themes.order(:order).each do |theme| %>
<div class='align_element'>
<section class='form_theme_title clearfix'><%= raw theme.theme_title %></section>
<section class="form_theme_description clearfix"><%= raw theme.theme_description.linkify %></section>
</div>
<ul class='selfce-reviews-table clearfix'>
<%= render "reviews/tabHeader" %>
<li class='row clearfix'>
<ul>
<li class="clearfix">
<% theme.custom_questions.order(:order).each do |question| %>
<li class='column selfc'>
</li>
<li class='column rev'>
<div class='align_element'>
<section class='form_question clearfix'>
<%= raw question.question %>
</section>
<section class="form_description clearfix">
<%= raw question.description.linkify %>
</section>
<%= f.fields_for question.answers.build do |builder| %>
<%= builder.hidden_field :custom_question_id, value: question.id %>
<% if question.q_type == "textfield" or question.q_type.nil? %>
<%= builder.text_field :answer, class:'reviews-inputBox clearfix' %>
<% elsif question.q_type == "textarea" %>
<%= builder.text_area :answer, class:'reviews-inputTextarea clearfix tinymce' %>
<%= tinymce %>
<% elsif question.q_type == "checkbox" %>
<% elsif question.q_type == "radio-button" %>
<% end %>
<% end %>
</div>
</li>
<% end %>
</li>
</ul>
<%= f.submit %>
</li>
<% end %>
</ul>
<% end %>
</div>
What I want is to store each answer in the table "answers" in the fields:
- question_id which refers to the unique id of the question
- answer - the value of the input field
As you can see I use #customForm which is the generated form by the administrator. I assume I use it here in the wrong way, because I don't want to update the form but only the answers... so instead it should be something like form_for :answers but that doesnt seem to work..
So my first question:
The way I see it every answer is unique and should therefore be seen as a unique form, hence my nested forms.. However if I use my code right now every input field has the same id: "custom_form_answer_answer". Is there a way I don't have to use nested forms to load the input fields for each question, e.g. by giving each answer a unique id based on the question_id?
I can't hardcode the inputfield name since this can change any time the user creates a new field..
My second question
If there is a way to do that.. how can I save the answers then..
Please let me know if you need any addition information.. and please keep in mind I'm fairly new to rails... so still learning!
Thanks! =-)
I am trying to group a set of products (obtained typing a query) based on taxonomy(on of the attributes of a product)
My desired output is
Taxonomy 1
prod1 prod2 prod3 prod4
prod 5 ...
Taxonomy 2
pod6 prod7
Taxonomy 3
prod8 prod9..
I am using the following code in the view:
<% taxonomies.each do |taxonomy|%> #"taxomonies" is a set of unique taxonomies for retrieved products
<h1><%= taxonomy%></h1>
<ul>
<% collection.each_with_index do |product,i| %> #"collection" is the list of products retrieved
<li>
<%#ptaxon = product.get_taxonomy%>
<%if #ptaxon == taxonomy%>
<%code for listing product%>
<%end%>
</li>
<%end%>
</ul>
<%end%>
This groups the products based on taxonomies but the format is not what I desire. Could someone please point out my mistake.
EDIT: also tried using < br > , but doesn't help!
This is the output I'm getting. I want the taxonomies earrings, bracelets and necklaces to start from a new line.
Thanks
If you have your associations setup correctly, you can do it like this:
<% taxonomies.each do |taxonomy| %>
<%= taxonomy.name %>
<% taxonomy.products.each do |product| %>
<%= product.name %>
<% end %>
<% end %>
Models should be something like:
class Taxonomy
has_many :products
end
class Product
belongs_to :taxonomy
end
here is my models.
User
unit_id
Unit
block_id
Block
postalcode_id
Postalcode
neighbourhood_id
Neighbourhood
name
the relations is for all is top belongs to bottom
this is my current index.html.erb file, i wish to output the number of user in each neighbourhood.
<% provide(:title, 'Neighbourhoods') %>
<ul class="thumbnails">
<% #neighbourhoods.each do |neighbourhood| %>
<li class="span3">
<div class="thumbnail">
<div style="position:relative;">
<%= link_to "Join", '#', class: "btn-join" %>
<%= image_tag(neighbourhood.name+".jpg", alt: neighbourhood.name) %>
</div>
<h2 style="margin-bottom:0px"><%= neighbourhood.name.titleize %></h2>
<% neighbourhood.postalcodes.each do |postalcode| %>
<%= postalcode.blocks.map(&:block).join(", ") %>
<% end %>
<br>
<%= neighbourhood.streetname.titleize %>
</div>
</li>
<% end %>
</ul>
Thanks in advance.
Assuming a Neighborhood has_many Users:
<%= neighbourhood.users.size %>
Note that counting is a relatively slow option, so you can optionally cache the number of users for speed using counter_cache:
class User < ActiveRecord::Base
belongs_to :neighborhood, :counter_cache => true
end
Then in a migration:
add_column :neighborhoods, :users_count, :integer, :default => 0
Seems like an awfully deeply nested set of associations. You may want to take a second look at your models and see if you can 'trim them down' a little. Maybe something like just have a User and Unit model, then add block, postal code and neighbourhood to Unit where you could do Unit.block, and Unit.postal_code...ect.
That being said with your current configuration (assuming correct associations of has_many/belongs_to) you should be able to do something like:
Neighbourhood.postal_code.block.unit.users.count
Good luck!