我和f.submit有一个小问题。我已经创建了一个用户表单。一旦填写,可以在Employeur和Prestataire之间进行选择。通过单击相应按钮,您应该被重定向到employeur或prestataire表单以继续订阅。但是我总是被重定向到Prestataire表单,即使我点击了employeeur按钮。用户控制器似乎存在以下问题:
if params[:commit] == "employeur"
或在用户表单中:
<div class="actions">
<%= f.submit 'Employeur', :name => "Employeur" %>
<%= f.submit 'Prestataire', name: 'prestataire' %>
我在这里也试过
<%= f.submit 'Employeur', {:name => "submit", :value => "Employeur" }%> but it didn't work.
用户控制器:
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
if params[:commit] == "Employeur"
format.html { redirect_to new_user_employeur_path(user_id: @user), notice: "Renseignez vos informations d'employeur" }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: "Renseignez vos informations de prestataire" }
format.json { render action: 'show', status: :created, location: @user }
end
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update(user_params)
if params[:commit] == "employeur"
format.html { redirect_to new_user_employeur_path(user_id: @user), notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { redirect_to new_user_prestataire_path(user_id: @user), notice: "User was successfully updated." }
format.json { head :no_content }
end
else
format.html { render action: 'edit' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
如果你能解开这个谜团,也帮助我理解f.subit的第一个参数之间的区别,:name,:value I&#39;我将感激不尽。
答案 0 :(得分:0)
"Employeur"
不等于"employeur"
。挑选一个或另一个应该使这项工作。
答案 1 :(得分:0)
你应该这样做:
if params[:commit] == "Employeur"
而不是:
if params[:commit] == "employeur"