简体   繁体   English

Railsfields_for拉模型名称而不是参数id的ID

[英]Rails fields_for pulling model name instead of ID for params id

I'm using fields_for to create a page for editing multiple user objects in my app. 我正在使用fields_for创建一个页面来编辑我的应用程序中的多个用户对象。 I've been using this Railscast video as a point of reference. 我一直在使用此Railscast视频作为参考。 http://railscasts.com/episodes/198-edit-multiple-individually The issue I am running into is that rather than using the user id when generating the html, the form builder is inserting the user's name. http://railscasts.com/episodes/198-edit-multiple-individually我正在遇到的问题是,表单生成器正在插入用户名,而不是在生成html时使用用户ID。 I have no idea why. 我不知道为什么。 Below is my controller, view, and some of the html generated. 下面是我的控制器,视图以及一些生成的html。

View 视图

 Manage Your User Licenses
User License Management for <%= @org.title %>
<%= form_tag update_many_user_licenses_path, :method => :put do %>
  <% for user in @users %>
    <%= fields_for "users[]", user do |user_fields| %>
      <div><%= user.name %></div>
      <div><%= user_fields.label :active_license %>
      <%= user_fields.check_box :active_license %></div>
    <% end %>
  <% end %>
  <%= submit_tag "Mark as Complete" %>
<% end %>

Controller 调节器

class UserLicensesController < ApplicationController
  before_filter :authenticate_user!

  def manage
    @org = current_user.organization
    @users = @org.users
  end
  def update_many
    @users = User.find(params[:users])
    @users.each do |user|
      user.update_attributes!(params[:user].reject { |k,v| v.blank? })
    end
    flash[:notice] = "Updated users licenses."
    redirect_to manage_user_licenses_path
  end
end   

HTML HTML

<div>Tyrel Denison</div>
<div><label for="users_Tyrel-Denison_active_license">Active license</label>
<input name="users[Tyrel-Denison][active_license]" type="hidden" value="0" /><input   id="users_Tyrel-Denison_active_license" name="users[Tyrel-Denison][active_license]" type="checkbox" value="1" /></div>

While I'm not familiar with the LinkedIn API Gem, based on our discussion, it seems the to_param method is being overwritten in your User model. 尽管我对LinkedIn API Gem并不熟悉,但根据我们的讨论,似乎to_param方法已在您的User模型中被覆盖。

From the comments, this is apparently what your to_param method looks like: 从注释中可以看出,这显然是您的to_param方法的样子:

def to_param  # TODO USe something better than this plz  
    self.name.gsub(/\s+/, '-')  
end

You reported that rather than using the user id when generating the html, the form builder is inserting the user's name , and as you can see, this is precisely what is happening in that method. 您报告说,表单生成器没有在生成html时使用用户ID而是插入了用户名 ,正如您所看到的,这正是该方法中发生的事情。 Ultimately, fields_for calls to_param , and you don't have a choice in that matter. 最终, fields_for调用to_param ,您在这件事上别无选择。

I would be quite astonished that the LinkedIn gem required you to override that method, as it would most definitely break any model that used it, as you experienced (and I can't explain why the other developer decided to do that - it's possible they innocently wrote the method without realizing it already exists in the base class) 我会很惊讶,LinkedIn gem要求您重写该方法,因为它肯定会破坏您使用过的使用该模型的任何模型(而且我无法解释为什么其他开发者决定这样做—可能是因为天真地写了该方法,却没有意识到它已经存在于基类中)

I can't give you more detailed information without examining the system more closely, however I would suggestion trying to find how/where to_param is being used (assuming it is being called explicitly somewhere), and create a separate method instead. 在没有更仔细地检查系统的情况下,我无法提供更多详细的信息,但是我建议尝试查找使用to / to_param方式/位置(假设在某处显式调用了它),并创建一个单独的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM