我正在开发一个名为Languages_User的连接表,它拥有并属于许多语言和用户。用户是使用设计宝石创建的。我希望填写一个表单,并自动将user_id填充到Languages_User表中。我觉得我已经尝试了所有的东西,这似乎是一个简单的任务,但希望有人能给我一些有关错误的见解:
class LanguagesUsersController < ApplicationController
def new
@languages_user = LanguagesUser.new
#@user_id = current_user
end
def create
if user_signed_in?
#@user = current_user
@languages_user = LanguagesUser.new(languages_user_params)
#@languages_user.language_id = params[:languages_user][:language_id].to_i
#@user_id = user_id.to_i
respond_to do |format|
if @languages_user.save
format.html { redirect_to @languages_user, notice: 'Proficiency was successfully created.' }
format.json { render :show, status: :created, location: @languages_user }
else
format.html { render :new }
format.json { render json: @languages_user.errors, status: :unprocessable_entity }
end
end
else
puts 'You must be logged in'
redirect_to new_languages_user_path
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def set_languages_user
@languages_user = LanguagesUser.find(params[:id])
end
def languages_user_params
params.require(:languages_user).permit(:language_id, :level, users_attributes: [:id])
end
和_form.html.erb
<%= form_for(@languages_user, method: :post) do |f| %>
<% if @languages_user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@languages_user.errors.count, "error") %>prohibited this language from being saved:</h2>
<ul>
<% @languages_user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :level %><br>
<%= f.number_field :level %>
</div>
<div class="field">
<%= f.collection_select(:language_id, Language.order('language ASC').all, :id, :language) %><br>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这里的第一个问题,请原谅我,如果没有正确询问:)
答案 0 :(得分:1)
我已经通过添加:
解决了这个问题System
到我的控制器。