English 中文(简体)
获取rails表单中的关联值
原标题:get association values in rails forms

I m building an app where a User has tasks and a task has a location. The tasks and locations are in a nested_form using formtastic_cocoon, which is the formtastic gem with a jQuery extension.

location.address字段是一个自动完成的文本字段,用于搜索数据库中已存在的地址。因此,当用户选择地址时,表单中会填充一个隐藏的location_id。

我想做的是,当用户编辑任务时,我希望它显示当前选择的地址,但我看不到任何地方可以检索到该值。我可以得到location_id,就像在任务模型中一样,但我似乎无法得到相关的location.address。

模型是

class Task < ActiveRecord::Base
     attr_accessible :user_id, :date, :description, :location_id

     belongs_to :user
     has_one :location
end

class Location < ActiveRecord::Base
     attr_accessible :address, :city, :state, :zip

     has_many :tasks
end 

那么以我的形式,我有

  <div class="nested-fields"> 
     <%= link_to_remove_association "remove task", f %>
      <div class="searchAddress">
         < input type="text" value="HERE IS WHERE I WANT TO SHOW THE ADDRESS" >
     </div>
   <%= f.hidden_field :location_id %>
   <%= f.inputs :date, description %>
</div>

-----------经过编辑以包含所有formtastic代码---------------

form.html.erb
<%= semantic_form_for @user, :html=>{:multipart=true} do |form| %>

    <%= form.inputs :username, :photo %>


      <%= form.semantic_fields_for :tasks do |builder | %>

   <%= render  task_fields , :f => builder %>
  <% end %>
<% end %>

------- end edit -------------- I have tried outputing different manner of f , but don t see any reference to the associated locations, yet if I debug User.Task[0].Location outside of the form, I get the correct location details. How do I get that inside the form??

---------更新------------

离这件事越来越近了。原来我可以输出

<%= debug f.object %>

我得到了返回的任务对象。不幸的是,它不包括location对象,只包括location_id字段的值。

问题回答

你试过@task.location.address吗?

你的方法是正确的。但也有一些遗漏的概念宣告了关联。我想更改如下:

# app/models/task.rb

class Task < ActiveRecord::Base
  attr_accessible :user_id, :location_id, :date, :description

  belongs_to :user
  belongs_to :location
end

由于我们将位置外键存储在任务表中,因此最好在此处声明belongs_to关联。

# app/models/location.rb

class Location < ActiveRecord::Base
  attr_accessible :address, :city, :state, :zip

  has_many :tasks

  # this method will return full address of a location
  def full_address
    [address, city, state, zip].reject(&:blank?).join(", ")
  end
end 

然后,您需要添加所选位置的完整地址

# app/views/users/_task_fields.html.erb

<div class="nested-fields"> 
  <%= link_to_remove_association "remove task", f %>
  
  <div class="searchAddress">
    <input type="text" value="<%= f.object.location&.full_address %>">
  </div>
  
  <%= f.hidden_field :location_id %>
  <%= f.inputs :date, description %>
</div>





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签