【发布时间】: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代码放上去吗? -
我刚刚做到了。例如,如果我添加 它将传递数据
-
只要
<%= f.select(:lecture_id, options_for_select(@lecture_options)) %> -
有效!感谢朋友的帮助
标签: ruby-on-rails forms ruby-on-rails-4 select