关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
5年前关闭。
Improve this question
在使用设计创建用户后(在 View /页面中,使用助手)我重定向他们以在 Papas Controller 中创建模型条目,如果他们的角色是“Papa”,或者如果他们的角色是“Babysitter”,则在 Babysitter Controller 中创建一个模型条目。
问题是我有一个奇怪的语法错误,因为两个 Controller (爸爸和保姆)实际上是相等的。它给了我:
SyntaxError in PapasController#new /Users/Seba/Desktop/PP/laguarderia/app/controllers/papas_controller.rb:75: syntax error, unexpected keyword_end, expecting end-of-input
但不适用于 BabysitterController#new
这是 Controller :
class PapasController < InheritedResources::Base
before_action :set_papa, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, except: [:new]
# GET /postulantes
# GET /postulantes.json
def index
@papas = Papa.all
end
# GET /postulantes/1
# GET /postulantes/1.json
def show
end
# GET /postulantes/new
def new
@papa = Papa.new
end
# GET /postulantes/1/edit
def edit
end
# POST /postulantes
# POST /postulantes.json
def create
@papa = Papa.new(papa_params)
@papa.user_id = current_user.id
respond_to do |format|
if @papa.save
format.html { redirect_to @papa, notice: '¡Tu postulación fue recibida con exito! Nos contactaremos contigo dentro de los próximos días.' }
format.json { render action: 'show', status: :created, location: @papas }
else
format.html { render action: 'new' }
format.json { render json: @papas.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /postulantes/1
# PATCH/PUT /postulantes/1.json
def update
respond_to do |format|
if @papa.update(papa_params)
format.html { redirect_to @papa, notice: 'Postulante was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @papa.errors, status: :unprocessable_entity }
end
end
end
# DELETE /postulantes/1
# DELETE /postulantes/1.json
def destroy
@papa.destroy
respond_to do |format|
format.html { redirect_to papas_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_papa
@papa = Papa.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def papa_params
params.require(:papa).permit(:nombre, :apellido1, :apellido2, :rut, :cumple, :estadocivil, :email, :cel, :fijo, :ciudad, :comuna, :calle, :numero, :depto, :profesion, :empresa, :cargo, :colegiohijos, :cantidadhijos, :edadhijos, :necesidad, :comentario, :user_id)
end
end
end
这是我的 helper :
module PagesHelper
def resource_name
:user
end
def resource
@resource = current_user || User.new
if @contact.save
if current_user.role == "Papa"
flash[:success] = "Thanks! I'll be in touch soon!"
redirect_to :action => 'papas/new'
elsif current_user.role == "Babysitter"
flash[:success] = "Thanks! I'll be in touch soon!"
redirect_to :action => 'babysitters/new'
else
flash[:success] = "hola"
redirect_to :action => 'home'
end
else
redirect_to :action => 'home'
end
end
def devise_mapping
@devise_mapping ||= Devise.mappings[:user]
end
end
我意识到出了什么问题。它不是 Controller ,它是由该 Controller 控制的呈现形式中缺少的一端......你错过的愚蠢的事情:/
最佳答案
错误
我不知道您对 Rails 的经验如何,但通常,您收到的错误基本上意味着您没有关闭特定的代码块。
这可能是关闭括号的问题,包括某种变量声明,或者通常只是没有所需的 end
定义
--
Controller
首先,您使用的是 inherited_resources
但我不明白你为什么要设置像 new
这样的方法, show
, edit
, set_papa
等Inherited Reources
意味着你不必设置那些 -
Class PapasController < InheritedResources::Base
before_filter :authenticate_user!, except: [:new]
# POST /postulantes
# POST /postulantes.json
def create
@papa = Papa.new(papa_params)
@papa.user_id = current_user.id
respond_to do |format|
if @papa.save
format.html { redirect_to @papa, notice: '¡Tu postulación fue recibida con exito! Nos contactaremos contigo dentro de los próximos días.' }
format.json { render action: 'show', status: :created, location: @papas }
else
format.html { render action: 'new' }
format.json { render json: @papas.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /postulantes/1
# PATCH/PUT /postulantes/1.json
def update
respond_to do |format|
if @papa.update(papa_params)
format.html { redirect_to @papa, notice: 'Postulante was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @papa.errors, status: :unprocessable_entity }
end
end
end
# DELETE /postulantes/1
# DELETE /postulantes/1.json
def destroy
@papa.destroy
respond_to do |format|
format.html { redirect_to papas_url }
format.json { head :no_content }
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def papa_params
params.require(:papa).permit(:nombre, :apellido1, :apellido2, :rut, :cumple, :estadocivil, :email, :cel, :fijo, :ciudad, :comuna, :calle, :numero, :depto, :profesion, :empresa, :cargo, :colegiohijos, :cantidadhijos, :edadhijos, :necesidad, :comentario, :user_id)
end
end
--
修复
我认为问题与 3
end
有关s 你在类(class)结束时有: end
end
end
我认为您正在尝试关闭
private
block ,当它不需要结束时。删除 end
之一在这里 - (给你 2
) - 它应该适合你
关于ruby-on-rails - Redirect_to 从设计创建到模型创建 - 使用帮助程序 - 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24668162/