Rails getter和setter函数不能与form一起工作



我的表单在Profile模型上看起来是这样的:

<%= form_for @profile do |f| %>
    <div class="form-group">
        <%= f.label :email, "Email" %>
        <%= f.text_field :email, class: "form-control" %>    
        <%= f.label :tag_list, "HELLO!" %>
        <%= f.text_field :tag_list %>
    </div>
    <%= f.submit "Submit", class: "btn btn-success" %>
<%end%>

我有一个Tag模型,我想把标签应用到它。

我的getter/setter函数:

def tag_list
  Rails.logger.error "names"
  tags.map(&:tag_name).join(", ")
end
def tag_list=(names)
  self.tags = names.split(",").map do |n|
    Rails.logger.error "names"
    self.tags.where(tag_name: n.strip).first_or_create!
  end
end

我的控制器:

def update
  redirect_to profile_path(params[:id])
end
def edit
end

它不会保存该配置文件的标记,也不会在日志文件中看到我的日志。

更新动作目前只做重定向,所以@profile是不变的。在调用@profile.update_attributes(params[:profile])或类似的方法之前,不会运行setter方法。

最新更新