简体   繁体   English

Rails创建多个条目复选框

[英]Rails create multiple entries checkbox

I can't find a solution and I'm becoming a little desperate... 我找不到解决方案,我变得有些绝望了...

form: 形成:

<%= simple_form_for @skill_user, :url => create_new_skill_users_path do |f| %>

<% @skills.each do |skill| %>
<%= skill.name %>

Now...Additional fields...new in 现在...其他字段...新功能

<%= f.input :prio, collection: 1..5, :as => :radio_buttons %>
<%= f.text_field :remark%>



<%= check_box_tag "skill_ids[]", "#{skill.id}" %>
<%= hidden_field_tag("skill_id", "#{skill.id}") %>
<%= hidden_field_tag(:user_id, "#{@user.id}") %>
<% end %>

controller: 控制器:

def create_new_skill
params[:skill_ids].each do|skill_id|
    SkillUser.create(
         :user_id => user_id, :instruction_id => params[:instruction_id], :remark => ??, :prio => ??
    )
  end

Thanks so far for your support. 到目前为止,感谢您的支持。 Now I have a different question/two problems. 现在我有一个不同的问题/两个问题。 Would be nice if...you could help me again. 如果...您可以再帮助我,那会很好。

The radio button collection in the form in each line. 每行表格中的单选按钮集合。 But not usable because they act as one and not per line. 但不能使用,因为它们充当一个,而不是每行。 I need some additional parameter to point them to the skill.id of each line. 我需要一些其他参数来将它们指向每一行的skill.id。

And also how to fetch the values in the controller. 以及如何在控制器中获取值。

The remark text field. 备注文本字段。 I get "skill_user"=>{"remark"=>""} in the log. 我在日志中得到了“ skill_user” => {“备注” =>“”}。 Gets overwritten..needs somewhere a []. 被覆盖..需要在[]某处。 What is the best way? 什么是最好的方法?

I have tried: 我努力了:

<%= simple_fields_for "skill[]", @skill_user do |f| <%= simple_fields_for“技能[]”,@skill_user做| f | %> Gives me Log: "skill"=>[{"remark"=>"test"} %>给我日志:“ skill” => [{“” remark“ =>” test“}

controller :remark => params[:skill][:remark] => can't convert Symbol into Integer phh... 控制器:remark => params [:skill] [:remark] =>无法将Symbol转换为Integer phh ...

"text_field_tag[]" gives back a hash.. “ text_field_tag []”返回哈希值。

You should write 你应该写

 params[:skill_ids].each do|skill_id| 
  SkillUser.create(:user_id => params[:user_id], :skill_id => skill_id)
 end

in your controller's action. 在您的控制器的动作中。

As well your post action should be on create not on new, as per REST conventions 同样,根据REST约定,您的发布操作应在创建时而不是在新的基础上进行

As you are probably in the learning phase, I'll recommend you some tips and give you the answer. 由于您可能正处于学习阶段,因此我将向您推荐一些技巧并给出答案。

Name your actions and controller with a good name. 给您的动作和控制器起个好名字。 In that case, I guess a SkillController with an action new is a good choice. 在那种情况下,我猜一个带有新动作的SkillController是一个不错的选择。 In routes, use resources instead of match or get/post/put/delete for the most time. 在路线中,最多时间使用资源而不是match或get / post / put / delete。

 def new_sk
   params[:skill_ids].each do|skill_id| 
    SkillUser.create(
      :user_id => params[:user_id],
      :skill_id => skill_id,
      :name => params[:name]
    )
   end
 end

Is the answer. 是答案。 This code is very simple, what you are passing to the SkillUser.create is a HASH with the hash keys being user_id, skill_id and name, and their respective values are params[:user_id], params[:skill_id] and params[:name]. 此代码非常简单,您传递给SkillUser.create的是一个HASH,其哈希键为user_id,skill_id和name,其各自的值为params [:user_id],params [:skill_id]和params [:name] ]。

When you don't know the current state of how the "params" variable is(how it's hash values are nested and so on), I advise you to print it, like this: 当您不知道“ params”变量的当前状态(如何嵌套哈希值等等)时,建议您打印出来,如下所示:

p params.inspect

In the beginning of your action, then read what is printed on the terminal you are running your server. 在操作开始时,请阅读运行服务器的终端上打印的内容。 There is a gem called awesome_print that prints it with colors and is awesome(as the name suggests) when you are just trying to see what is really happening in the server-side. 有一个名为awesome_print的gem可以用颜色进行打印,并且在您尝试查看服务器端真正发生的事情时确实很棒(顾名思义)。

I also advise you to create a model with a good name. 我还建议您创建一个具有良好名称的模型。 "SkillUser" is wrong, it would be better if you had written UserSkill. “ SkillUser”是错误的,如果您编写了UserSkill会更好。 You are probably going to work with relations soon and I further advise you to read the Rails guides in that matter. 您可能会很快处理关系,我进一步建议您阅读有关此问题的Rails指南。

Rails guides are gold. 导轨是金。 You can find 90% of your questions answered there, with good explanations. 您可以在这里找到90%回答的问题,并且有很好的解释。

Have a good day. 祝你有美好的一天。

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

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