I have this segment of code,
<form>
ID of event: <input type="text" name="ID" placeholder="E.g. 123">
<input type="submit" value="Submit">
</form>
<% #events.each do |ev| %>
How do i make this work? I'm wanting to have it so the id that is typed in the text box shows the related stuff from the events table in the rest of the page.
I was under the impression that it would be something like this #events.id do |ev| ?but i'm still a noob at rails :)
Thanks for any help Please be gentle!
So i had done this all wrong!
i had to add this in my controller
def index
#events = Event.where(id: params[:id])
#id = params[:id]
end
and put this in to search for the ID
<%= form_tag({controller: "event", action: "index"}, method: "get") do %>
<%= label_tag(:id, "Input the ID to display:") %>
<%= text_field_tag(:id) %></p>
<%= submit_tag("Submit ID") %>
<% end %>
Sorry for the confusion but i thought it best to add the answer on how i fixed it.
Related
I have a simple structure of contest:
The contest has multiple questions
Each question has multiple answers
possible
For this I feel like what I've created is a good structure:
I am trying to get the answers of a user for each question from the form in my view to my controller. Long story short, I was unable to use the <%= collection_radio_buttons ... %> because the method wouldn't be right. There is not one column in my model for each answer to each question. answer_option is not a column in my questions table it's an association because it's another table... (or would you know how to help on that?)
So I've bypassed this issue by creating loops on answers_options of each question and using html inputs and labels, like this:
<%= simple_form_for #contest, url: contest_send_quizz_path(#contest), method: :post do |f| %>
<%= f.fields_for :questions do |q| %>
<% #questions.each do |question| %>
<h4 class="mt-5"><%= question.title %></h4>
<% question.answer_options.each do |answer_option| %>
<div class="inputGroup">
<input type="radio" name=<%= answer_option.question_id %> value="<%= answer_option.answer %>" id=<%= answer_option.id %> />
<label for=<%= answer_option.id %>><%= answer_option.answer %></label>
</div>
<% end %>
<% end %>
<% end %>
<div class="mt-3">
<span class="button-effect-2">
<%= f.button :button, "Envoyer", type: :submit, class:"text-white" %>
</span>
</div>
<% end %>
However now the problem is fetching those values in the controller. It seems with this question that I'd have to get it with params[:something] and that :something being the name of the input. Is that right? And now that I know that, does putting params[:name] (which is the same for all concerned radio buttons of one question) directly get the checked radio, or is there another thing to do?
Here is what I have for now, there is stuff to ignore since the structure of the rest of my code is bigger than just the contest. This is in the ContestsController:
def show
#contest = Contest.find(params[:id])
authorize #contest
#time_left = seconds_to_units(#contest.deadline - Time.now)
#is_done = #contest.deadline < Time.now
if #is_done
get_winner
end
#questions = #contest.questions.includes([:answer_options])
end
def send_quizz
#contest = Contest.find(params[:contest_id])
#questions = #contest.questions.includes([:answer_options])
authorize #contest
current_user.contests << #contest
user_choice = # TODO : Get checked radio value from view
user_contest = current_user.contests.select { |contest| contest.title == #contest.title }.first
user_contest.questions.each do |question|
question.user_answer = user_choice
end
# TODO : make sure every questions were answered before submitting request
redirect_to contests_path
flash[:notice] = "Ta réponse a été prise en compte"
end
So is there a way to get this value, or should I change my db structure so that each question has one column for each answer? Or maybe another solution? Thanks !
EDIT:
I also tried replacing this:
<input type="radio" name="<%= answer_option.question_id %>" value="<%= answer_option.answer %>" id="<%= answer_option.id %>" />
<label for="<%= answer_option.id %>"><%= answer_option.answer %></label>
With this:
<%= q.check_box :answer_option, name:answer_option.question_id, id:answer_option.id %>
<%= q.label :answer_option, answer_option.answer, for:answer_option.id %>
And getting the value in the controller with user_choice = params[:answer_option] but when I replaced check_box with radio_button, it messed up the name, id etc. values AND I couldn't select anymore.
EDIT 2:
By adding this structure:
<%= q.radio_button :answer_option, answer_option.answer %>
<%= q.label :answer_option, answer_option.answer %>
It works (no errors), however the name is set automatically and to something not specific to each question i.e. contest[questions][answer_option] and the label's for is set to contest_questions_answer_option, therefore clicking on the label does not link to the checkbox.
Managed to retrieve the value of the checkbox with this structure for the radio-buttons:
<%= q.radio_button answer_option.question_id, answer_option.answer %>
<%= q.label "#{answer_option.question_id}_#{answer_option.answer.parameterize.underscore}", answer_option.answer %>
And the controller:
user_choices = params[:contest][:questions]
user_contest.questions.each do |question|
question[:user_answer] = "#{user_choices[:'#{question.id}']}"
question.save
end
I am trying to build a simple quiz app, I already have one table for questions and another for answers (answers each have a question_id tag to pull up the multiple choice options for each question).
I have listed out all my multiple choice questions on one page, and I want only the answers that correspond to each question to display below the question, however each MCQ is currently listing the full answer table instead of just the matching answers.
I think it has to do either with my view or controller code that's causing this behaviour (when I change the question ID in my controller to just a single question, the 4 possible answers appear correctly, but when I change it to #questions = Question.all, then the full answer table returns).
Or it might have to do with using <% #question_answers.each do |question_answers| %> wrongly.
My pages_controller.rb code:
class PagesController < ApplicationController
def show
end
def home
end
def challenge
#questions = Question.all
#question_answers = QuestionAnswer.where(question_id: #questions.pluck(:id))
# #questions = Question.where(id: 3)
end
end
My view file at pages/challenge.html.erb:
<h1>Challenge</h1>
<% #questions.each do |question| %>
<div class="container">
<p><%= question.question %></p>
<form>
<% #question_answers.each do |question_answers| %>
<input type="radio" name="gender" value=<%= question_answers.answer %>><%= question_answers.answer %><br>
<% end %>
<input type="submit" value="Submit">
</form>
</div>
<% end %>
I want only the answers that correspond to each question to display below the question, any ideas what I'm getting wrong here?
You are wrong with this code:
<% #question_answers.each do |question_answers| %>
<input type="radio" name="gender" value=<%= question_answers.answer %>><%= question_answers.answer %><br>
<% end %>
question_answers is all records from answer table because you assigned it to QuestionAnswer.where(question_id: #questions.pluck(:id))
You should query only questions that belongs to answer. Assume you have two model and set up association like this:
class Question
has_many :question_answers
end
class QuestionAnswer
belongs_to :question
end
Then in your view you can get questions belongs to an answer like this:
<% #questions.each do |question| %>
<div class="container">
...
<% question.question_answers.each do |question_answer| %>
<input type="radio" name="gender" value=<%= question_answer.answer %>><%= question_answer.answer %><br>
<%>
...
</div>
<% end %>
In controller, you only need:
#questions = Question.includes(:question_answers).all
includes(:question_answers) is used to avoid N+1 queries when loading answers for each question.
Have you specified the relationship between the models question and answer.
If so, in the view try giving this
<% #questions.each do |question| %>
<div class="container">
<p><%= question.question %></p>
<form>
<% question.question_answers.each do |question_answers| %>
<input type="radio" name="gender" value=<%= question_answers.answer %>><%= question_answers.answer %><br>
<% end %>
<input type="submit" value="Submit">
</form>
</div>
<% end %>
when you give question.question_answers.each the answers related to that question will be pulled.
I have a resource Invoice and every invoice has an edit path:
http://localhost:3000/invoices/1/edit
On my index page where I will all my invoices I want to have an input field and a submit button to jump right to the edit page for a certain invoice:
<form class="form-inline" action="???" method="get">
<div class="form-group">
<input type="text" class="form-control date" placeholder="Invoice ID" name="id" >
</div>
<button type="submit" class="btn btn-info">Show</button>
</form>
How do I have to define the action so that if I enter for example 1 and click on Show it goes right to my edit method?
I think you're looking for something like this in your html
<%= form_tag find_invoice_url do |f| %>
<%= f.text_field :invoice_number =>
<%= f.submit "Search" =>
<= end =>
then you would need to add a route
post '/invoices/find_invoice', to: "invoices#find_invoice",
as: "find_invoice"
then in your controller something like
def find_invoice
invoice = Invoice.find_by_id(params[:invoice_number])
if invoice
redirect_to invoice_url(invoice.id) #whatever your edit route is
else
redirect_to root_url #anywhere really
end
end
if you are iterating through all the invoices, you wouldn't need an input field.
assuming that you have an invoice obhect, you would just do something like
<%= form_tag edit_invoice_path(invoice), method: "get" do -%>
<%= submit_tag "Show", class="btn btn-info" %>
<% end %>
You may want the button to say edit instead - that's more of a ux "principle of least surprise" issue
I have this line in controller/new for multiple records form:
#students = Array.new(quantity) {#stream.students.build}
View file:
<%= #errors.inspect %>
<%= form_for :students_list, {:url => create_students_list_stream_url(#stream) } do |students_list_form| %>
<% i=0 %>
<% #students.each do |student| %>
<% i=i+1 %>
<%= students_list_form.fields_for student do |student_form|%>
<%= render :partial => "students/add_students_to_stream", locals: {:student=>student, :student_form=>student_form, :i=>i} %>
<% end %>
<% end %>
<%= students_list_form.submit "Добавить студентов" %>
<% end %>
This generates looking rather right HTML, for one input:
<input id="name-field-1" type="text" name="students_list[student][account_attributes][name]" />
...
Note that it can be multiple objects per page, such as this input, as I want to create list of records.
But, as I can see from my server logs, params, which Rails gets, include only last record.
For example, I have forms for two 'students' on page.
<input id="name-field-1" type="text" name="student[student][account_attributes][name]" />
<input id="name-field-2" type="text" name="student[student][account_attributes][name]" />
And input there names John and Peter, my server logs will show:
"students_list"=>{"student"=>{"account_attributes"=>{"name"=>"Peter"}}}
and thats it.
In that case you must be adding multiple="multiple" and make name type as array.
html.
<input id="name-field-1" type="text" multiple="multiple" name="student[student][account_attributes][name][]" />
rails.
<%= form.text_field :object, id: "id", multiple: true %>
So that would return all the elements as Array for a same field name.
Fields are named exactly the same and do not have array sign in name, that's why browser overwrites them.
To make browser post array - fields should end up named students_list[][student][account_attributes][name] (note the additional [], by placing it in different parts of name you can control which elements are grouped)
So try fields_for 'students_list[]', ...
For my form I am using my Product model:
class Product < ActiveRecord::Base
attr_accessible :purchase_date, :send_to_data
end
On my form I have the :purchase_date working correctly when I create multiple products but also want to make the radio_button_tag do the same:
<%= form_tag create_multiple_products_path, :method => :post do %>
<%= date_select("product", "purchase_date") %>
<%= radio_button_tag(:send_to_data, 1) %>
<%= radio_button_tag(:send_to_data, 0) %>
<% #products.each_with_index do |product, index| %>
<%= fields_for "products[#{index}]", product do |up| %>
<%= render "fields", :f => up %>
<% end %>
<% end %>
<%= submit_tag "Done" %>
<% end %>
This didn't work for me, my database doesn't flag as either false or true.
I think the problem lies in the params of the "send_to_data". Unlike the "purchase_date" it isn't finding the object (product).
{"product"=>{"purchase_date(2i)"=>"12", "purchase_date(3i)"=>"11", "purchase_date(1i)"=>"2011"},
"send_to_data"=>"1",
"products"=>{"0"=>{"product_name"=>"Test", "price"=>"23", "product_store"=>"13", "exact_url"=>""},
"1"=>{"product_name"=>"", "price"=>"", "product_store"=>"", "exact_url"=>""},
"2"=>{"product_name"=>"", "price"=>"", "product_store"=>"", "exact_url"=>""},
"3"=>{"product_name"=>"", "price"=>"", "product_store"=>"", "exact_url"=>""},
"4"=>{"product_name"=>"", "price"=>"", "product_store"=>"", "exact_url"=>""}}, "commit"=>"Done"}
Is there a way to map it to the object like the purchase date does?
As far as I can see you confuse FormHelper and FormTagHelper.
You use Form tag helper which, according to the documentation
(FormTagHelper) provides a number of methods for creating form tags that doesn’t rely
on an Active Record object assigned to the template like FormHelper does.
This mean for ActiveRecord-based form you need to use use FormHelper (and its radio_button helper method).
Code with radio_button_tag form tag helper
<%= radio_button_tag(:send_to_data, 1) %>
<%= radio_button_tag(:send_to_data, 0) %>
generates the following HTML:
<input id="send_to_data_1" name="send_to_data" type="radio" value="1" />
<input id="send_to_data_0" name="send_to_data" type="radio" value="0" />
And code with radio_button form helper
<%= radio_button("product", :send_to_data, 1) %>
<%= radio_button("product", :send_to_data, 0) %>
generates:
<input id="product_send_to_data_1" name="product[send_to_data]" type="radio" value="1" />
<input id="product_send_to_data_0" name="product[send_to_data]" type="radio" value="0" />
Hope this helps!
check this page form_helpers first, here, the right way should be
<%= radio_button_tag(:send_to_data, 1) %>
<%= radio_button_tag(:send_to_data, 0) %>