I have a drop down of options that links to the names in a profiles table. They are linked by a key named "profile_id". When I select on of these options and display it in the show page. It shows me the profile_id, but I want to show the name. Can anyone tell me how to do this.
The Form View Selection looks like this:
<div class="field">
<%= f.label :profile %><br>
<%= f.collection_select :profile_id, Profile.all, :id, :name, {:prompt => 'Please select an Aritst or Band'} %>
</div>
The Show View looks like this:
<p>
<strong>Artist:</strong>
<%= #album.profile_id %>
</p>
The schema is like this:
create_table "albums", force: :cascade do |t|
t.string "title"
t.date "released"
t.string "genre"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "profile_id"
t.string "image"
end
create_table "profiles", force: :cascade do |t|
t.string "name"
t.date "born"
t.string "bio"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image"
end
The form does give me the correct name options so the tables are linking up correctly. But when I view the results, it gives me the profile id and not the name I selected. Now I am aware that is because I have specified for it to do this. But how do I get it to show the name. Can it be done in the show view code or does it have to be converted in the controller or model somewhere.
I intend on that selected option also being a link back to the profile. If you know this also, that would help me out big time.
As always, all help is greatly appreciated.
Change your show action to
<p>
<strong>Artist:</strong>
<%= #album.profile.name %>
</p>
Related
A really noob question, but i try with different options and I dont get it.
I Have a form for a new "Cancha" and a field that references to "Mmpp", In the field mmpp_id i need to see a list with the differents kinds of "Mmpp" (already created some options), right now if I put for example "1" i get Mmpp:0x00007f7fb1600378 in the show, Already change the show to <%= #cancha.mmpp.nombre %> and I get exactly what I want in the show, but the problem is in the form I need a list to pick.
Thanks in advance
<%= form.text_field :mmpp_id, class: 'form-control' %>
create_table "canchas", force: :cascade do |t|
t.string "nombre"
t.string "descripcion"
t.integer "capacidad"
t.boolean "operativa"
t.bigint "mmpp_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["mmpp_id"], name: "index_canchas_on_mmpp_id"
create_table "mmpps", force: :cascade do |t|
t.string "nombre"
t.string "descripcion"
t.integer "densidad"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end
This will create a dropdown
<%= form.select :mmpp_id, Mmpp.all.map { |r| [r.nombre, r.id] } %>
This was simpler and with the same html classes of the other inputs, form.select work but give some style problems.
<%= f.association :mmpp_id, label_method: :nombre, value_method: :id %>
So I have a scaffold for both blogs and post_category.
I made an association with both of them. Here's my schema:
create_table "blogs", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "slug"
t.integer "status", default: 0
t.bigint "post_category_id"
t.index ["post_category_id"], name: "index_blogs_on_post_category_id"
t.index ["slug"], name: "index_blogs_on_slug", unique: true
end
create_table "post_categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
So simply I created a couple of post categories and when I try to make an association with blog items so whenever I create a new blog I can display a select statement and choose my preferred category for the blog item I am trying to create but I don't know how to display all categories on all forms and on the index.html.erb file:
<div class="field">
<%= form.label :category %>
<%= form.collection_select :post_category, PostCategory.all %>
</div>
How can I achieve this? And make sure that it saves the data as well?
as per this reference, the format is
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
and for your code, it can be like this
<%= form.collection_select(:post_category, :post_category_id, PostCategory.all, :id, :name, prompt: true) %>
update 2:
inside your blog_controllers
# white list parameter
def blog_params
params.require(:blog).permit(
:post_category_id,
... others fields
)
end
I am trying to add a relational field drop down select to the existing table. My current Schema
create_table "hardwares", force: :cascade do |t|
t.string "serialnumber"
t.string "modelnumber"
t.string "modeltype"
t.string "location"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "poc_id"
t.index ["poc_id"], name: "index_hardwares_on_poc_id"
end
create_table "pocs", force: :cascade do |t|
t.string "name"
t.string "address"
t.string "facility"
t.string "phone"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
The index field in the hardwares table was created when I ran the migration.
rails g migration addPocReferencesToHardwares poc:references
which generated the migration file below as well as the index in the schema
class AddPocReferencesToHardwares < ActiveRecord::Migration[5.1]
def change
add_reference :hardwares, :poc, foreign_key: true
end
end
After setting up the relationship, I would like to be able to select a POC for the hardware by name as a drop down of all available POC's.
I added this to the Hardware form:
<div class="field">
<%= form.label "POC" %>
<%= form.collection_select(:poc_id, Poc.all, :id, :name, { :prompt => 'Select a POC', :selected => #poc.poc_id }, { class: 'form-control' }) %>
</div>
The error I get is " undefined method `poc_id' for nil:NilClass". How do I allow a drop down selection for adding a POC to the hardware?
Issue was the fact that I had set the relationship as POC belongs to Hardware, thus in the Form field it needed to be:
<div class="field">
<%= form.label "POC" %>
<%= form.collection_select(:poc_id, Poc.all, :id, :name, { :prompt => 'Select a POC', :selected => #hardware.poc_id }, { class: 'form-control' }) %>
</div>
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 %>