I’m developing question and answers application, where a user attempts to answer questions, I am looking to define a custom method rather than doing coding in the controller methods, creating a separate method that can be called to check the answer the user submitted against the answers database table, where the question_id and the user_input is the same as the Answers database.
Can anyone guide me with where I am going wrong or is there a better approach to checking the user submitted input against the database and then inputting the submission into the database.
Submissions form
<%= form_for :submission :controller => :submissions :action => 'check_answer' do |f| %>
<%= f.text_field :contnet, :value => '' %>
<%= f.submit 'Submit Answer' %>
<% end %>
Submissions_controller
def check_answer
answer = Answer.find_by(question_id: params[:question_id][:content])
if answer.present?
#insert into submissions table, display correct and add to scoreboard
else
redirect_to competitions_path
end
end
Answers Table :
create_table "answers", force: :cascade do |t|
t.string "content"
t.integer "question_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Question_table :
create_table "members_questions", force: :cascade do |t|
t.string "title"
t.integer "category_id"
t.integer "point_id"
t.integer "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
end
Related
<div class="field">
<%= form.label :user_id%>
<%= form.collection_select :user_id, User.all, :id , :emailaddress%>
How can I modify the select to only select the email address of users with their admin role as True?
Here is my schema..
create_table "courses", force: :cascade do |t|
t.integer "coursenumber"
t.integer "user_id"
t.string "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_courses_on_user_id"
create_table "users", force: :cascade do |t|
t.string "firstname"
t.string "lastname"
t.string "title"
t.integer "officenumber"
t.string "emailaddress"
t.integer "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "admin"
I think you can use the where method, as you would normally:
User.where(admin: true)
<%= form.collection_select :user_id, User.where(admin: true), :id , :emailaddress%>
You also could create a scope in your model named admins, and then just call User.admins or something like that.
I'm new to Rails so maybe this is a stupid question. I'm struggling to get a attribute from another model in a nested form. Let me explain:
I have 3 models linked each other. Poi -> PoiDescription <- DescriptionType
One Poi can have multiple descriptions and a description have only one description type. I'm creating the PoiDescriptions inside the Poi form with a nested form. Every thing is working well, but now, inside the fields_for I want a label before the textarea with the name of the description type. But I don't know how to get it...I can't do something like 'p.description_type.name' so how can I get that attribute?
Here is my code:
Poi Controller
def new
#poi = Poi.new
#descriptions = DescriptionType.all
#descriptions.each do |d|
#poi.poi_descriptions.new(description_type_id: d.id)
end
end
Poi form
<%= form_with model: #poi do |f| %>
...
<div class="col-md-12">
<%= f.fields_for :poi_descriptions do |p| %>
<%= p.hidden_field :description_type_id %>
<%= p.text_area :description %>
<% end %>
</div>
...
Schema
create_table "description_types", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "poi_descriptions", force: :cascade do |t|
t.text "description"
t.bigint "poi_id"
t.bigint "description_type_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["description_type_id"], name: "index_poi_descriptions_on_description_type_id"
t.index ["poi_id"], name: "index_poi_descriptions_on_poi_id"
end
create_table "pois", id: :serial, force: :cascade do |t|
t.text "name"
t.float "longitude"
t.float "latitude"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "monument_id"
t.integer "beacon_id"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.index ["beacon_id"], name: "index_pois_on_beacon_id"
t.index ["monument_id"], name: "index_pois_on_monument_id"
end
Hope you can help me! Thanks in advance.
To get from the form_builder_object to the actual object you can do p.object.description_type.name
If you just want that as a title and not as an input field you can either:
Add it in a p or span tag and make it look like a label with css or
Add a label_tag with a custom name and title as you need. https://apidock.com/rails/ActionView/Helpers/FormTagHelper/label_tag
I have two tables, accounts and items and I would like to show the buisness_name instead of the idfrom the accounts table on the view/items/show.html.erb page.
Currently I have no associations between the models, but I have the account_id column in the items table.
create_table "accounts", force: :cascade do |t|
t.string "buisness_name"
t.string "web_site"
t.string "phone_number"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "items", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "image"
t.decimal "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "category_id"
t.json "attachments"
t.integer "account_id"
end
I'm getting the account_id via this: <%= #item.account_id %>, but I would like to show the buisness_name instead.
Try something like this
class Item < ActiveRecord::Base
belongs_to :account
end
Into the view
<% if #item.account %>
<%= #item.account.buisness_name %>
<% end %>
Should be business_name, as #Sergio Tulentsev already told you
I updated my answer because I noticed from the table that the account_id has not a not null constraint
If you have
<%= #item.account_id %>
The horrible way to get the account would be
<%= Account.find_by(id: #item.account_id).try(:buisness_name) %>
Much smarter would be
class Item
belongs_to :account
delegate :buisness_name, to: :account, prefix: true, allow_nil: true
And then in the view...
<%= #item.account_buisness_name %>
I have a season model and a anime model. I have it set so i have spring, summer, fall and winter through season. Each anime will belong to spring, summer, fall or winter. In my home page i want to display a section for all the seasons showing the anime that it belongs to. I can display the name of all the seasons and i can make all the anime show in localhost:3000/seasons/spring if it belongs to spring. I cant figure out how to display the anime that belongs to each season(spring, summer, fall, winter) in the home page.
This is how my anime in db looks like
create_table "animes", force: true do |t|
t.string "title"
t.string "type_of"
t.integer "episodes"
t.string "status"
t.date "started_at"
t.date "ended_at"
t.string "producers"
t.string "duration"
t.string "rating"
t.text "description"
t.integer "user_id"
t.string "slug"
t.datetime "created_at"
t.datetime "updated_at"
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.string "source_name"
t.string "source_link"
t.string "crunchyroll"
t.integer "season_id"
t.integer "genre_id"
t.integer "studio_id"
t.string "header_file_name"
t.string "header_content_type"
t.integer "header_file_size"
t.datetime "header_updated_at"
end
This is how my season table looks like
create_table "seasons", force: true do |t|
t.string "name"
t.string "slug"
t.datetime "created_at"
t.datetime "updated_at"
end
Anime Model
class Anime < ActiveRecord::Base
belongs_to :season
end
Season model
class Season < ActiveRecord::Base
has_many :animes
end
You have some options here, depending on what you want to show
simple list with attributes
<% Anime.includes(:season).each do |anime| %>
<p><%= anime.title %> -- <%= anime.season.name %></p>
<% end %>
grouped by season
<% Season.scoped.each do |season| %>
<h3><%= season.name %></h3>
<% season.animes.each do |anime| %>
<p><%= anime.title %></p>
<% end %>
<% end %>
I have 3 tables- OwnerofProperty , Property and Ticket. I want to make a form using form_for to represent property booking; can I make a form to retrieve data from Property where the submit button saves the data in the Ticket table? I am asking because I have no idea if that can be possible or how to make it.
Note: I have only created the relations :
OwnerofProperty one-to-many Property
Property one-to-one Ticket
I need this form just to make a user able to see the avaliable properties and can book only one , how to make this form ?
Schema.rb for the three models :
create_table "owners", :force => true do |t|
t.string "f_name"
t.string "l_name"
t.string "address"
t.string "tel_no"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "properties", :force => true do |t|
t.string "p_street"
t.string "p_city"
t.string "postcode"
t.string "property_type"
t.integer "rooms"
t.integer "rent"
t.integer "owner_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "properties", ["owner_id"], :name => "index_properties_on_owner_id"
create_table "tickets", :force => true do |t|
t.string "city"
t.string "street"
t.string "ticket_type"
t.integer "rooms"
t.integer "rent"
t.integer "property_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "tickets", ["property_id"], :name => "index_tickets_on_property_id"
Yes, it is possible.
let's see ticket_controller.rb
def new
#property = Property.find 20 #20 is property id
#properties = Property.all
##ticket = Ticket.new
end
now in view (where you want to create form):
<%= form_for #ticket do |f| %>
<%= f.select :property_id, #properties.collect {|p| [ p.name, p.id ] }%> <!-- just an example, Ticket model has a field named "property_id" -->
<%= f.submit %>
<%= end %>
this form submits to create action of ticket_controller. And you are able to get all data as params and save it to table.
def create
#ticket = Ticket.new(params[:ticket])
#ticket.save
respond_to do |format|
format.html{redirect_to( your_desired_path)}
end
end