I have been trying to come up with a database call that would give me everything from the choices column from the questionnaires table. I was able to iterate and list the choices themselves but they have their own index value
The code #choices = Questionnaire.select(:choices).all[0] gives me just the first one
I want the database call to be #choices = Questionnaire.select(:choices).all[i] that would allow me to access all of them regardless. I have tried loops that I can think of. If anyone out there are available please help me out. I have struggled all day on this. Thanks!
Questionnaire Controller
class QuestionnairesController < ApplicationController
def index
#questions = Questionnaire.find(params[:category_id])
#params[:category_id]= <%=category.id%>
#category = Category.find(params[:category_id])
#videos = VideoClue.find(params[:category_id])
render :show
###render :show Renders Html page
end
def choose_answer
#questions = Questionnaire.find(params[:id])
#choices = Questionnaire.select(:choices).all
render :choose_answer
end
end
end
Choose_answer.html
<h1>Congrats You Hit The Choices Page!</h1>
<%= semantic_form_for #questions.choices do |c| %>
<%= c.inputs do |e| %>
<%= c.input :answer, :as => :check_boxes , :collection => #choices%>
<% end %>
<% end %>
Seed
Questionnaire.create({question: "In that year did MTV (Music Television) premiere and what was the first music video the channel aired?",
choices:'1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin, 1981 The Buggles Video Killed The Radio Star', correct_answer:"1981 The Buggles 'Video Killed The Radio Star' ", category_id:1})
Questionnaire.create({question: "This sitcom featured four girls living under one roof. They attended the same boarding school, ran a shop together and reside in a town called Peekskill." , choices:'Designing Women, The Facts of Life, Girlfriends', correct_answer:'The Facts of Life', category_id: 2})
Questionnaire.create({question: "This martial arts film premiere in 1985 which featured a young man who studies Bruce Lee's techniques while on the search for his master. This was set in New York City." , choices:'The Last Dragon, The Karate Kid, Big Trouble in Little China', correct_answer:'The Last Dragon', category_id: 3})
Questionnaire.create({question:"This game launched in 1991 on Sega Genesis which the player's mission is to collect as many golden rings as possible", choices:'Battletoads, Sonic The Hedgehog, Jewel Master', correct_answer: "Sonic The Hedgehog", category_id:4})
Questionnaire.select('choices').all returns
[<Questionnaire:0x007fbc2c9fa728
id: nil,
choices: "1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin, 1981 The Buggles Video Killed The Radio Star">,
<Questionnaire:0x007fbc2c9fa138 id: nil, choices: "Designing Women, The Facts of Life, Girlfriends">,
<Questionnaire:0x007fbc2ca01dc0 id: nil, choices: "The Last Dragon, The Karate Kid, Big Trouble in Little China">,
<Questionnaire:0x007fbc2ca00f88 id: nil, choices: "Battletoads, Sonic The Hedgehog, Jewel Master">]
choices: '1982 Michael Jackson Bille Jean, 1984 Madonna Like a virgin,
1981 The Buggles Video Killed The Radio Star'
The first thing to think about is why do you store all the possible choices as a single string? How can you know where does the text for the first option ends and where does the second one begin?
So, your first step should be to divide a single choices-containing string to, ehm, array of choices. This can be done by
database re-engineering (switching from string to array type);
splitting initial string (but in case some of your choices contain comma, you're gonna be in a bad situation)
Questionnaire.first.choices.split(',')
# => ["1982 Michael Jackson Bille Jean", "1984 Madonna Like a virgin", "1981 The Buggles Video Killed The Radio Star"]
After doing that you should be able to iterate choices in your view (or rails console). This step differs based on which option you've chosen earlier.
Related
I'm brand new to ruby and am currently building a rails application that needs to pull from a Tumblr blog. I have read through several questions here, online tutorials and Tumblr's API docs and after two weeks, I'm stuck.
https://www.tumblr.com/docs/en/api/v2
I make the call using Tumblr in my controller
require 'json'
require 'tumblr_client'
client = Tumblr::Client.new :consumer_key => 'consumerkey'
#post = client.posts 'crossfitinterchange.tumblr.com', :type => 'text', :limit => 1, :filter => 'html'
Then this is returned (short snippet)
{"blog"=>{"title"=>"Today's Workout", "name"=>"crossfitinterchange", "posts"=>118, "url"=>"http://crossfitinterchange.tumblr.com/", "updated"=>1444971275, "description"=>"", "is_nsfw"=>false, "ask"=>false, "ask_page_title"=>"Ask me anything", "ask_anon"=>false, "share_likes"=>true, "likes"=>0}, "posts"=>[{"blog_name"=>"crossfitinterchange", "id"=>131266609424, "post_url"=>"http://crossfitinterchange.tumblr.com/post/131266609424/friday-oct-16th-wod", "slug"=>"friday-oct-16th-wod", "type"=>"text", "date"=>"2015-10-16 04:54:35 GMT", "timestamp"=>1444971275, "state"=>"published", "format"=>"html", "reblog_key"=>"OWmgAbMO", "tags"=>[], "short_url"=>"http://tmblr.co/ZE0_uk1wG6O4G", "recommended_source"=>nil, "recommended_color"=>nil, "highlighted"=>[], "note_count"=>0, "title"=>"Friday Oct 16th WOD", "body"=>"<p>[STRENGTH]</p>\n\n<p>5 x 3 Push Press to find 3RM</p>\n\n<p>* complete 5 ring rows after each set</p>\n\n<p><br/>\n[CONDITIONING]</p>\n\n<p>7 mins EMOM:</p>\n\n<p>5 sit ups<br/>\nME Thrusters (115/80#) for duration of round</p>\n\n<p><br/>\n- REST 2 mins -</p>\n\n<p><br/>\n2 mins AMRAP Box Jump Overs (24/20")</p>\n\n<p><br/>\nScore is total Thrusters + Box Jump Overs</p>", ...
In my view I have
<% #post.each do |p| %>
<%= p.title %>
<%= p.body %>
<% end %>
I get an error that title and body are undefined.
I'm getting the JSON returned via the API, now how do I grab the title and body of the post?
Your #post object contains JSON data. So, you don't need to loop through that i.e. no need to use .each on that object.
To grab the title and the body from the #post JSON object, you should do:
title = #post['blog']['title']
body = #post['blog']['body']
Not sure where to start with this so here goes.. I am building a small blog in which the date of each post is displayed, overtime there will be many blog posts per month, for which i would like to group together by the month it was published.
I want to display it like this in the view
Archives
January 2013
February 2013
March 2013
etc
When i click on a given month the idea is it will take me to all the posts that where published within that month.
So far I can group all the posts by month and year
#posts_by_month = Post.all.group_by { |post| post.created_at.strftime("%B %Y") }
In my view i then render like so
<% #posts_by_month.each do |m| %>
<%= m %>
<% end %>
Which returns this in the view
["July 2013", [#<Post id: 1, title: "Ruby News", comments: "dsfdsfdsfdsfdsfds", category_id: 1, user_id: 1, created_at: "2013-07-26 07:10:25", updated_at: "2013-07-26 07:19:27", photo_file_name: "pf-7.jpg", photo_content_type: "image/jpeg", photo_file_size: 162495, photo_updated_at: "2013-07-26 07:19:26">]]
So at the moment i have a hash where the month/year is the key and then all my posts in an array, is that correct?
All i want to display is the Month/Year and then click that month to be taken to all the posts for that month
Any help appreciated
EDIT
ok silly me, forgot my basics on key/value pairing, i have got just the date to display
<% #posts_by_month.each do |m,p| %>
<%= link_to m %>
<% end %>
Now i just need to be able to click the link to see all posts for that month
You could do
= link_to m, posts_path(:month => m)
Now in posts#index, fetch the posts based on params[:month]
if params[:month]
date = Date.parse("1 #{params[:month]}") # to get the first day of the month
#posts = Post.where(:created_at => date..date.end_of_month) # get posts for the month
else
#posts = Post.all
end
I'm trying to make a simple movie database using Rails 4.0.0 as a learning project. I'm particularly interested in using scaffolding as much as possible, as this is one of the features that drew me to RoR in the first place.
Yes, I do realize the potential risks (Box 1.2.Scaffolding: Quicker, easier, more seductive), but I promise I won't have my project go public before I really understand what's going on beneath the hood. Right now I'm more in "evaluating technologies for my next super duper project"-mode.
Here's what I've got so far:
rails g scaffold Person name:string
rails g scaffold Movie name:string
Now, I could've done something like
rails g scaffold Movie name:string person_id:integer
instead, but I want a movie to be associated with both a director and an actor. (Next step is to make an association that relates multiple actors to a single movie, but I'm not quite there yet.)
So, I headed over to the blog post Creating Multiple Associations With the Same Table, describing pretty much what I need. It's a somewhat old post, so things might have changed now - I don't know. Anyway. This how I changed the models:
class Person < ActiveRecord::Base
has_many :movies
end
and
class Movie < ActiveRecord::Base
belongs_to :director_id, :class_name => 'Person', :foreign_key => 'person_id'
belongs_to :actor_id, :class_name => 'Person', :foreign_key => 'actor_id'
end
and finally the magical
rake db:migrate
Starting the WEBrick by running rails s in the console, I open my browser and start registering people
The time has come to start registering movies. According to previous questions here and here I have to make a migration script in order to create the necessary database fields. So this is what I did:
rails g migration AddPersonIdsToMovies director_id:integer actor_id:integer
I also updated the app/views/movies/_form.html.erb to
<%= form_for(#movie) do |f| %>
<% if #movie.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#movie.errors.count, "error") %> prohibited this movie from being saved:</h2>
<ul>
<% #movie.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :director_id %><br>
<%= f.select :director_id, Person.all.collect {|x| [x.name, x.id]}, {}, :multiple => false %>
</div>
<div class="field">
<%= f.label :actor_id %><br>
<%= f.select :actor_id, Person.all.collect {|x| [x.name, x.id]}, {}, :multiple => false %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
When I create a new movie, the view shows up fine and the select inputs works fine. However, the data in the director and actor field isn't persisted. I ran rails console and looked at the newly created movie:
irb(main):004:0> mov = Movie.first
Movie Load (0.2ms) SELECT "movies".* FROM "movies" ORDER BY "movies"."id"
ASC LIMIT 1 => #<Movie id: 1, name: "No such movie", created_at:
"2013-08-02 17:02:12", updated_at: "2013-08-02 17:02:12",
director_id: nil, actor_id: nil>
which is kind'a disappointing with no director or actor info.
Update
Based on #Mattherick's suggesition, I edited the private part of the movies_controller.rb to this:
# Never trust parameters from the scary internet, only allow the white list through.
def movie_params
params.require(:movie).permit(:name, :director_id, :actor_id)
end
Unfortunately, when I post a new movie I get
Person(#70319935588740) expected, got String(#70319918738480)
Extracted source:
# POST /movies.json
def create
#movie = Movie.new(movie_params)
respond_to do |format|
if #movie.save
and the request data goes as
{"utf8"=>"✓",
"authenticity_token"=>"???",
"movie"=>{"name"=>"Romantic Comedy",
"director_id"=>"2",
"actor_id"=>"1"},
"commit"=>"Create Movie"}
Update 2
I tried to create a new Movie in the rails console, like this:
irb(main):001:0> movie = Movie.new(name: "My fine movie", director_id: "1", actor_id: "2")
ActiveRecord::AssociationTypeMismatch: Person(#70311109773080) expected, got String(#70311102311480)
which is what you'd expect from the POST to the controller. This made me test what happened if I excluded the quotation marks for director_id and actor_id. So I did
irb(main):005:0> movie = Movie.new(name: "My fine movie", director_id: 1, actor_id: 2)
ActiveRecord::AssociationTypeMismatch: Person(#70282707507880) expected, got Fixnum(#70282677499540)
Still using the console, I decided to create an actor and a director
director = Person.new(name: "Woody Allen")
director.save
actor = Person.new(name: "Arnold Schwarzenegger")
actor.save
and then I did
movie = Movie.new(name: "I'd like to see that", director_id: director, actor_id: actor)
movie.save
which worked like a charm (output omitted for brevity). So the whole question boils down to "How can I pass a Person as the argument to director_id and actor_id through the web interface?"
If I had a single field in Movies called person_id: integer, I believe that rails would've inferred that I'm not trying to pass a string containing the id of a person, but rather I'm trying to pass an entire person object.
Update 3
I tested my suspicion that rails understands how to deal with posts when the foreign key column is named after the pattern [table]_id. So I created a new project with a Continent model and a Country model, where rails g scaffold Country name:string continent_id:integer. I changed my Country view to include
<div class="field">
<%= f.label :continent_id %><br>
<%= f.select :continent_id, Continent.all.collect {|x| [x.name, x.id]} %>
</div>
instead of the default numeric field. The continent_id is still posted a string:
Started POST "/countries" for 127.0.0.1 at 2013-08-03 10:40:40 +0200
Processing by CountriesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"???", "country"=>{"name"=>"Uganda", "continent_id"=>"4"}, "commit"=>"Create Country"}
yet rails understood that continent_id was the identifier of an entry in the Continent table.
Sadly, the inferrer mechanism doesn't work in my original case, as I have two associations from Movie to the Person table. I need to somehow make sure rails understand that there is a mapping from director_id to a Person.
Update 4
According to some sources, it seems as I need to refine the Person model further. So I did
class Person < ActiveRecord::Base
has_many :directed_movies, :class_name => 'Movie', :foreign_key => 'director_id'
has_many :acted_movies, :class_name => 'Movie', :foreign_key => 'actor_id'
end
but still not fixing my problems.
I'm kind'a lost. Can anyone give me a few pointers on what I'm missing here? Can anyone help me map from director_id to person_id? Thanks!
Ok, so I finally got it. I don't think this is the correct way to go about this, but at least it solved the problem. As I mentioned in update 2, I was able to create a Movie object in the irb, and so I asked the question "How can I pass a Person as the argument to director_id and actor_id through the web interface?"
According to the sources here and elsewhere, rails should've understood the has_many and belongs_to class methods. However, I just can't seem to get it to work.
So I hacked the create method in movies_controller.rb to read like this:
def create
director = Person.where(:id => movie_params[:director_id]).first
actor = Person.where(:id => movie_params[:actor_id]).first
#movie = Movie.new(name: movie_params[:name], director_id: director, actor_id: actor)
respond_to do |format|
if #movie.save
format.html { redirect_to #movie, notice: 'Movie was successfully created.' }
format.json { render action: 'show', status: :created, location: #movie }
else
format.html { render action: 'new' }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
This is certainly not as elegant as I'd like it to be, and I don't think it is the RoR way to do things. Unfortunately it is the only thing I got working so far, but if anyone else can make a multiple association to the same model using rails 4, please do give me a heads up! :)
This one is driving me crazy. I've got a nested relationship between two models in my project, and I decided I did not want it to be shallow, since the child object (years) has no meaning outside the context of the parent (festivals).
So I sort of de-shallowed the relationship wherever I could find a reference to it, but I find myself unable to access the page to create a new child object.
Here's the url as I understand it should be: /festivals/1/years/new
from routes.rb:
resources :festivals do
resources :years
end
From years_controller.rb:
# GET festivals/1/years/new
# GET festivals/1/years/new.json
def new
#festival = Festival.find(params[:festival_id])
#year = #festival.years.build
respond_to do |format|
format.html # new.html.erb
format.json { render :json => #year }
end
end
And the button users press to get to the New page (on the Show page for the parent object):
<%= link_to 'Add Year', new_festival_year_path(#festival), :class => 'btn' %>
That takes the user to the correct URL, but I get:
No route matches {:action=>"show", :controller=>"years", :festival_id=>#<Festival id: 7, name: "Improganza", founded: nil, logo: "", mission: "This is that one that people spend a lot of money t...", city: "Honolulu", state_code: "HI", country_code: "US", created_at: "2013-07-26 14:49:19", updated_at: "2013-07-26 14:49:19">}
I created a new Rails project and set up scaffolds using Akria Matsuda's nested_scaffold gem, just to compare that output with my code... the resulting files look as I've shown here. I have no idea what I might be missing.
Just for good measure, the output of my rake routes:
festival_years GET /festivals/:festival_id/years(.:format) years#index
POST /festivals/:festival_id/years(.:format) years#create
new_festival_year GET /festivals/:festival_id/years/new(.:format) years#new
edit_festival_year GET /festivals/:festival_id/years/:id/edit(.:format) years#edit
festival_year GET /festivals/:festival_id/years/:id(.:format) years#show
PUT /festivals/:festival_id/years/:id(.:format) years#update
DELETE /festivals/:festival_id/years/:id(.:format) years#destroy
festivals GET /festivals(.:format) festivals#index
POST /festivals(.:format) festivals#create
new_festival GET /festivals/new(.:format) festivals#new
edit_festival GET /festivals/:id/edit(.:format) festivals#edit
festival GET /festivals/:id(.:format) festivals#show
PUT /festivals/:id(.:format) festivals#update
DELETE /festivals/:id(.:format) festivals#destroy
GET /festivals(.:format) festivals#index
POST /festivals(.:format) festivals#create
GET /festivals/new(.:format) festivals#new
GET /festivals/:id/edit(.:format) festivals#edit
GET /festivals/:id(.:format) festivals#show
PUT /festivals/:id(.:format) festivals#update
DELETE /festivals/:id(.:format) festivals#destroy
Try this:
<%= link_to 'Add Year', new_festival_year_path(#festival.id, :class => 'btn' %>
or
<%= link_to 'Add Year', new_festival_year_path({festival_id: #festival.id}, :class => 'btn' %>
according to the error you're getting
:festival_id=>#<Festival id: 7, name: "Improganza", founded: nil, logo: "", mission: "This is that one that people spend a lot of money t...", city: "Honolulu", state_code: "HI", country_code: "US", created_at: "2013-07-26 14:49:19", updated_at: "2013-07-26 14:49:19">}
the router is getting your whole festival param as the input for :festival_id
I think you are merging together the #new and #year actions in the years_controller and that might be causing some problems.
# GET festivals/1/years/new
# GET festivals/1/years/new.json
def new
#festival = Festival.find(params[:festival_id])
#year = #festival.years.build
end
def create
#festival = Festival.find(params[:festival_id])
#year = #festival.years.create(...)
#...fill in the rest of the method...
end
You also should update your link:
<%= link_to 'Add Year', new_festival_year_path(festival_id: #festival), :class => 'btn' %>
I created a short quiz on nested resources that might be helpful.
The answer was fairly silly. In my Rails server log (which I need to train myself to pay more attention to), I saw the some lines indicating a problem in line 63 of my _form.html.erb partial.
That line was:
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
festival_year_path(#festival), :class => 'btn' %>
Oops. Why I ever decided the "Cancel" button should take you to a year (that, of course, would not exist) is beyond me. I changed it to festival_path(#festival) and it's all good.
Thanks, everyone, for your help. I'm a newcomer to StackOverflow and to Rails in general. It really makes me feel welcome that I got such quick responses!
I have code that looks like this:
#all_ratings.each do |rating|
= check_box_tag "ratings[#{rating}]", session[:checkbox][rating], :id => "ratings_#{rating}"
...
= submit_tag 'Refresh', :id => "ratings_submit"
By saving the state of which checkboxes were clicked I hoped to be able to pre-check the boxes that were clicked after the request had gone through and the page reloads. The problem that I am having is that the code above doesn't seem to work. I'm not sure if it's because I have the :id => "ratings_#{rating}" bit at the end (which is required for this assignment). I checked out the rails api here, but that was as clear as mud. Thanks in advance for the help!
Cheers
(Disclaimer: This code is for HW 2 for Coursera's Software as a Service course - I have finished the bulk of the logic for the HW, but this last bit is beyond me and seems to be more of an idiosyncracy than a major topic, hence I am posting it here.)
What boolean variable did you add? Considering I have a model with has_many ratings, say Video, and have #video defined, I'd do
= check_box_tag "ratings[#{rating}]", session[:checkbox][rating], #video.ratings.include?(rating), :id => "ratings_#{rating}".
The #video.ratings.include?(rating) part prechecks the ratings associated to the current video. What are the associations related to your Rating model?