I have a question which should not be so difficult but I didn't find something similar and I'm not a rails expert. I want to change the names into the database trough a collection_select without adding new data.
I have these two models:
class Plan < ActiveRecord::Base
belongs_to :patient
has_many :structures,dependent: :destroy
end
########
class Structure < ActiveRecord::Base
belongs_to :plan
end
I upload some files which save data in plan and structures e.g plan name,..
I now need a form for #plan to fill in the kind of diagnose and I want also to change the names of the structure with some default name I decided (since structure names from uploaded files are different between file to file but have the same meaning )
<%= form_for #plan do |f| %>
<%= javascript_include_tag "plans_DefaultstructureNames.js" %>
<p><strong>Please select the diagnose:</strong><br>
<%= f.collection_select :diagnose, ['SRS', 'SRT', 'SBRTLung', 'SBRTLiver', 'SBRTPancreas', 'SBRTProstate'], :to_s, :to_s, :include_blank => true,prompt: true%>
</p>
<div id="SrsSrtDefault">
<p>
<strong>BODY</strong>
<%= f.collection_select :plan,:structure,#plan.structure.name , :to_s, :to_s, :include_blank => true,prompt: true%>
</p>
<p>
<strong>PTV1</strong>
<%= f.collection_select :plan,:structure,#plan.structure.name , :to_s, :to_s, :include_blank => true,prompt: true%>
</p>
</div>
Imagine that #plan.structure.name contains BOD and PTV1. I want that the user changes BOD to BODY trough a collection select but I really don't have clou..
Hope it's clear..Cheers
Related
In my rails application the following relationships exist:
user belongs_to :team ,
element belongs_to :team ,
task belongs_to :element
A user can then create a task which belongs to an element.
When a user is creating a task, they can select the element they would like the task to belong to. How do I show only the elements that belong to the current_user's team? I am using Devise to get the current_user.
The samples below do not work.
<div class="field">
<%= form.label :element_id %>
<%= form.select :element_id, options_for_select(Element.current_user.team_id.map{|s|[s.title, s.id]}),{ :multiple => true} %>
</div>
I also tried to call the method below from the tasks_controller.rb file but it didnt work either
def new
#task = Task.new
#tasks_element_dropdown = Element.current_user.team_id.map{|s|[s.title, s.id]}
end
In the tasks/_form.html.erb file, I called the method with the code below
<%= form.select :element_id, options_for_select(#tasks_element_dropdown),{ :multiple => true} %>
When I tried the example below it does work but it displays all elements, and I only want the elements that belong to the user's team to display
<div class="field">
<%= form.label :element_id %>
<%= form.select :element_id, options_for_select(Element.all.map{|s|[s.title, s.id]}),{ :multiple => true} %>
</div>
The problem is in the following code, which should return the elements:
Element.current_user.team_id.map{|s|[s.title, s.id]}
Since you have a user, you can get the team: current_user.team
And then get the elements of the team (as long as has_many :elements is defined in the Team class): current_user.team.elements
Thus, the final code should be something like:
<div class="field">
<%= form.label :element_id %>
<%= form.select :element_id, options_for_select(current_user.team.elements.map{ |s| [s.title, s.id] }),{ :multiple => true} %>
</div>
In my rails project I have two models, Car Make & Car Model, with a 1:M relationship (i.e. one Audi has many Audi models).
In my Views page, I want a form with two input fields for car make & car model. Ideally, I will be able to input a car make (i.e. Audi) and the second input field will have a drop down menu with all the models available for the make (2016 Audi A6, 2017 Audi A7).
I've set up all the relations and in the models I have saved a foreign key of the make.
currently in _form.html.erb I have
<div class="field">
<%= f.label :make_id, "Make:"%><br>
<%#= f.number_field :make_id %>
<%= f.collection_select :make_id, Make.all,
:id,:makes_info, {:include_blank => 'Please Select'} %>
</div>
<div class="field">
<%= f.label :model_id, "Model:" %><br>
<%= f.collection_select :model_id, Model.all,
:id,:model_info, {:include_blank => 'Please Select'} %>
</div>
If you want it to truly be dynamic, you would need to use an AJAX request to update the second select after the first is picked. You'd also need to use the options_for_select method inside of the select tag
Some more info to accompany what was already provided.
It's known as dynamic select boxes:
#config/routes.rb
resources :makes do
get :models, on: :collection #-> url.com/makes/models
end
#app/controllers/makes_controller.rb
class MakesController < ApplicationController
def models
#make = Make.find(params[:make][:make_id])
respond_to do |format|
format.js
end
end
end
#app/views/makes/models.js.erb
$select = $("select#models");
$select.empty();
<% #make.models.each do |model| %>
$select.append($('<option>').text(<%=j model.name %>).attr('value', <%= model.id %>));
<% end %>
#views
<%= f.collection_select :make_id, Make.all, :id, :makes_info, {include_blank: 'Please Select'}, { data: { remote: true, url: make_models_path }} %>
<%= f.collection_select :model_id, Model.all, :id,:model_info, {include_blank: 'Please Select'}, { id: "models" } %>
I'm new on rails and need people to give me some explications about rails
I wanna create an startups list app.
Startup can have many categories and categories can have many startups. I've used nifty scaffold to generate startups and categories. this is my model file for startups:
class Annuaire < ActiveRecord::Base
attr_accessible :name, :slogan, :description, :url, :email, :telephone, :image, :cp, :ville, :pays
belongs_to :categorie
end
the folow is for categories
class Categorie < ActiveRecord::Base
attr_accessible :name, :description
has_many :annuaires, dependent: :destroy
end
I also add an foreign key , is that enough? when i buid my database model on paper i find that i need a third table which make association between categories and startups. He will just got an ID and two foreign key , one for categories and one for startups. Could you please tell me if i'm on good way and give me a good advice about that work?
I also try to modify startup form . I add a select tag which will get all categories name from categorie table and show it to allow select by user :
<%= form_for #annuaire do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :slogan %><br />
<%= f.text_field :slogan %>
</p>
<p>
<%= select_tag "categorie", options_from_collection_for_select(#categories, "id", "name") %>
</p>
It dont work, i got the folowwing issue:
undefined method `map' for nil:NilClass
Extracted source (around line #12):
9
10
11
12
13
14
15
<%= f.text_field :slogan %>
</p>
<p>
<%= select_tag "categorie", options_from_collection_for_select(#categories, "id", "name") %>
</p>
<p>
<%= f.label :description %><br />
Can someone help me please? sorry for my english, i'm french
love the way rails is but i'm alone and it is little bit hard to understand all rails methods
What does #categories contain? Did you assign anything to it in your controller?
In Ruby, the first instance of #foo will cause a #foo to exist, and give it a nil value, if no #foo existed before. So the downstream error, if the code then calls #foo.map, is not "foo does not exist", but "nil has no method map".
Hi im using Nested_forms gem for a app, everything is working fine.. Im following the documentation here ...
My form is saving data to database, i can create infinite number of extra fields as i require.
The only problem is when i want to populate the list for example to Edit, then i canĀ“t populate again the list with all the values the user have previously selected, just the 1st value is there , the 2nds select box that should appear, appear transparent.. i leave an image , because english is not my lenguage y probably suck describing it
EDIT: I think the problem is on the loop , because first time when you submit it look like this..
And after saving, and lunching the form again to edit. this is what you get.
Here is the code in there.
<div id="nacionalidad">
<%= f.fields_for :citizens do |citizen_form| %>
<div>
<%= citizen_form.label :citizen, t('generales.citizen') %>
<%= citizen_form.select :country_id , Country.all.collect {|p| [ t("generales."+p.iso), p.id ] }.sort_by {|label,code| label}, { :include_blank => true } , { :class => 'pca33' } %>
<div id="delerr"><%= citizen_form.link_to_remove t('generales.delete') %></div>
</div>
<% end %>
<%= f.link_to_add t('generales.add'), :citizens %>
</div>
And the model
class Citizen < ActiveRecord::Base
attr_accessible :country_id
belongs_to :player
belongs_to :country
end
You might be going about this the wrong way. In my opinion it's much easier to use multiple-select fields and has_many relations. Then everything just works magically!
Form:
<%= select_tag :countries, options_from_collection_for_select(Country.all, 'id', 'name'), :multiple => true %>
Model:
class Citizen < ActiveRecord::Base
attr_accessible :country_id
belongs_to :player
has_many :countries
end
And then if you'd like, you can use another javascript library to make your multiselects more user-friendly:
Select2: http://ivaynberg.github.io/select2/
ChosenJS: http://harvesthq.github.io/chosen/
Have a page where there are multiple input fields of the same thing, Posts. Right now, when a user enters in a question for, let's say 3 fields, the only one that saves to the database is the last one. Whereas, it should save all three and give them each it's own post_id. Also; if the user doesn't enter anything in for the other fields, it should not save in the database either.
new_step_4_html.erb
<%= form_for(#post) do |f| %>
<%= f.text_field :content %>
<%= f.text_field :content %>
<%= f.text_field :content %>
<% end %>
projects_controller.rb
def new_step_4
#post = Post.new
end
Right now, all it does is submit one :content field, obviously because they all share the same id/value. Unfortunately, the Railscasts #197 applies for nested forms, so the javascript and helper stuff he does all applies for nested. I would think this is something simple. Person from IRC mentioned I could do some sort of '3.times' code into the view file or something?
First of all you will probably have to edit the model of you post.
post.rb
has_many :contents, :dependent => :destroy
accepts_nested_attributes_for :contents
You will need another model to store the content fields.
so first generate a model
rails g model content post_id:integer body:text
the model
content.rb
belongs_to :post
Now, in stead of doing <%= f.text_field :content %> a few times, let rails create them, because now you basically let them overwrite each other.
3.times do
content = #post.content.build
end
the form view will be something like this:
<%= form_for #post do |f| %>
<%= f.fields_for :contents do |builder| %>
<%= builder.label :body, "Question" %><br />
<%= builder.text_area :body, :rows => 3 %><br />
<%= end %>
<p><%= f.submit "Submit" %></p>
<% end %>
I did not test this code, but the idea should be correct. Let me know if you need more info.