ruby on rails 4 - How can I pull data in a controller and form for one model that is in a different model with out a foreign key relationship? -
ruby on rails 4 - How can I pull data in a controller and form for one model that is in a different model with out a foreign key relationship? -
i new rails , haven't done much info outside of model. have form references table controller files. want add together dropdown display list of projects project table user assign file project if want too. assigning project not requirement. file can unassigned project. have project_id column in file table projects assigned allowed null , did not build relationship because need have cases there none.
can please tell me how this?
when evaluate file, screen posts save or update button depending if has been saved in database.
at same time save , update pop on right want display list box of projects assign project button. if new list, if update either list because not assigned or display selected value in list if assigned, while allowing them alter list if desired.
in file controller method posts ui have code:
@file_alias_filedata = filealiasfiledata.all @projects = project.all for update
@projects = project.find(params[:id]) in form have code:
<p> <label> select project assign:</label> <br /> <%= select_tag 'projects', (@projects.present? ? options_for_select(@projects, @selected_project) : []) %> </p> the form runs in dropdown box:
#<project:0x))7ff531ab4518> can please help me figure out how accomplish task , why see unusual box value? doing wrong?
thank help!
assigning project not requirement. file can unassigned project. have project_id column in file table projects assigned allowed null
from docs:
belongs_to(name, scope = nil, options = {}) public specifies one-to-one association class. method should used if class contains foreign key. if other class contains foreign key, should utilize has_one instead. see activerecord::associations::classmethods’s overview on when utilize has_one , when utilize belongs_to.
methods added retrieval , query single associated object, object holds id:
association(force_reload = false) returns associated object. nil returned if none found.
likewise,
has_many(name, scope = nil, options = {}, &extension) public specifies one-to-many association. next methods retrieval , query of collections of associated objects added:
collection(force_reload = false) returns array of associated objects. an empty array returned if none found.
but belongs_to() , has_many() supposed create things more convenient you. not have utilize them.
next,
and why see unusual box value? doing wrong?
you see unusual value same reason next 2 loops display different things:
class dog attr_reader :name def initialize(name) @name = name end end @dogs = [ dog.new("sam"), dog.new("betty"), dog.new("pete"), ] @dogs.each {|dog| puts dog} @dog_names = @dogs.map {|dog| dog.name } @dog_names.each {|dog_name| puts dog_name} --output:-- #<dog:0x0000010099a308> #<dog:0x0000010099a2b8> #<dog:0x0000010099a268> sam betty pete you see same result if next in view:
<div> <%= select_tag "dog", options_for_select(@dogs) %> </div> <div> <%= select_tag "dog_name", options_for_select(@dog_names) %> </div> if read docs here:
http://apidock.com/rails/actionview/helpers/formoptionshelper/options_for_select
...you see several examples, should create clear options_for_select() takes argument is:
an array of strings. a hash both keys , values strings. an enumerable iterates on strings. etc.do see pattern? it's strings! options_for_select() needs argument consists of strings. if argument not collection of strings, e.g. array of project objects, options_for_select() tries convert objects strings calling to_s() on objects. , default to_s() method object#to_s() inherited objects , produces string containing class name , object_id.
forms ruby-on-rails-4 models html-select
Comments
Post a Comment