【问题标题】:Rails 4 - select_tag does not pass id for fieldRails 4 - select_tag 不为字段传递 id
【发布时间】:2015-10-09 22:54:31
【问题描述】:

我无法将 Lecture_id 传递给创建的对象: 基本上,它应该根据下拉菜单创建带有 Lecture_id 的项目。它似乎没有传递数据。

例如,如果我添加 它将传递数据

  <%= simple_form_for(@project) do |f| %>
   <%= f.error_notification %>

    <div class="form-inputs">
      <%= f.input :title %>
      <%= select_tag(:lecture_id, options_for_select(@lecture_options)) %>
      <%= f.input :company_name %>
      <%= f.input :phone_number %>
      <%= f.input :body %>
   </div>

   <div class="form-actions">
     <%= f.button :submit %>
  </div>
<% end %>

控制器:

   def new
     @lecture_options = Lecture.all.map{|u| [u.title, u.id]}
     @project = Project.new
     respond_to do |format|
        format.html
        format.js
    end
    authorize @project
   end 

 def create 
     @project = current_user.projects.build(project_params)
    if @project.save 
        flash[:success] = "You have successfully created a project."
        redirect_to profile_path(current_user)
     else 
        render action: 'new'
     end
   authorize @project 
 end


   def project_params
       params.require(:project).permit(:company_name, :phone_number, :body, :user_id, :title, :lecture_id)
    end

型号:

 Lecture.rb 
    has_many :projects  

Project.rb
 class Project < ActiveRecord::Base
     belongs_to :user
     belongs_to :lecture
  end

【问题讨论】:

  • 你能把你的整个form_for代码放上去吗?
  • 我刚刚做到了。例如,如果我添加 它将传递数据
  • 只要&lt;%= f.select(:lecture_id, options_for_select(@lecture_options)) %&gt;
  • 有效!感谢朋友的帮助

标签: ruby-on-rails forms ruby-on-rails-4 select


【解决方案1】:

您应该将 select_tag 更改为:

<%= select_tag("project[lecture_id]", options_for_select(@lecture_options)) %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-07
    • 2010-12-22
    • 2018-08-10
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多