gpt4 book ai didi

ruby-on-rails - 带有验证的 form_for 未正确调用 "create"操作

转载 作者:数据小太阳 更新时间:2023-10-29 08:07:36 25 4
gpt4 key购买 nike

我有一个表单,我希望用户回答每个问题,所以我添加了一个模型级验证。我想如果它通过验证,它应该重定向到另一个页面调用“场景”(我还没有完成它的 View 所以它应该显示模板丢失)。如果未通过验证,则应重新渲染到新页面,保留已填写的信息,并显示验证错误。

但无论我是否填写这些字段,当我点击提交按钮时,它总是显示索引页面而不是"new"或“场景”(应该是缺少模板)。它似乎忽略了我在“创建”操作中写的内容,并且从未调用过“创建”。

我使用 rails c 并插入一条新记录来测试验证。它运行良好,所以我想我的模型和验证没有问题。

我还尝试让 form_for 直接重定向到“场景”,以确保它能很好地用于 form_for,并且它确实显示我缺少模板,因此“创建”本身可能存在一些问题。我真的不知道出了什么问题。

<%= form_for @subject, :url => { :controller => 'appstores', :action => 'scenario' } do |f| %>

有个类似的问题:rails form_for never invokes the create controller action to use redirect_to .我曾尝试使用“respond_with”,但没有用。并检查我的 Controller 是否命名为 appstroes_controller.rb,资源为 routes.rb 中的 appstores。

我用的是rails 4.2.4、ruby 2.0.0和bootstrap 3,不知道是不是版本问题。

任何帮助将不胜感激,谢谢!

app/controller/appstores_controller.rb

class AppstoresController < ApplicationController
def index
end

def new
@subject = Subjectinfo.new
end

def create
@subject = Subjectinfo.new(params[:subjectinfo])
if @subject.save
redirect_to :action => "scenario"
else # if not pass DB validation
render :action => :new
end
end

def scenario
end
end

app/view/appstores/new.html.erb

<%= form_for @subject, :url => { :controller => 'appstores', :action => 'create' } do |f| %>    
<form class="form-horizontal">
<div class="form-group">
<%= f.label :username, "User Name:", :class=>"control-label", :for=>"username" %>
<% if @subject.errors[:username].presence %>
<span class="model-error"><%= @subject.errors[:username].join(", ") %></span>
<% end %>
<%= f.text_field :username, :autocomplete=>"off", :placeholder=>"User Name", :class=>"form-control", :id=>"username" %>
</div>
<div class="form-group">
<%= f.label :mobile_user, "Are you a mobile device user?", :class=>"control-label", :for=>"mobile_user" %>
<% if @subject.errors[:mobile_user].presence %>
<span class="model-error"><%= @subject.errors[:mobile_user].join(", ") %></span>
<% end %>
<div class="radio radio-primary">
<%= f.radio_button :mobile_user, "1", :id=>"mobile_user_1" %>
<%= f.label :mobile_user, "Yes", :class=>"control-label", :for=>"mobile_user_1" %>
</div>
<div class="radio radio-primary">
<%= f.radio_button :mobile_user, "0", :id=>"mobile_user_0" %>
<%= f.label :mobile_user, "No", :class=>"control-label", :for=>"mobile_user_0" %>
</div>
</div>
<div class="text-center">
<%= f.submit "NEXT", :class => "btn btn-default btn-outline btn-lg" %>
</div>
</form>
<% end %>

app/modle/subjectinfo.rb

(to support "attr_accessible" for rails 4, I had put "gem 'protected_attributes'" in my Gemfile)

    class Subjectinfo < ActiveRecord::Base
validates_presence_of :username, :mobile_user
attr_accessible :username, :mobile_user
end

config/routes.rb

AppStore::Application.routes.draw do
match ':controller(/:action(/:id(.:format)))', :via => :all
root :to => "appstores#index"
resources :appstores
get "appstores/scenario"=>"appstores#scenario"
end

rake routes

              Prefix Verb   URI Pattern                            Controller#Action
/:controller(/:action(/:id(.:format))) :controller#:action
root GET / appstores#index
appstores GET /appstores(.:format) appstores#index
POST /appstores(.:format) appstores#create
new_appstore GET /appstores/new(.:format) appstores#new
edit_appstore GET /appstores/:id/edit(.:format) appstores#edit
appstore GET /appstores/:id(.:format) appstores#show
PATCH /appstores/:id(.:format) appstores#update
PUT /appstores/:id(.:format) appstores#update
DELETE /appstores/:id(.:format) appstores#destroy
appstores_rfscenario GET /appstores/rfscenario(.:format) appstores#rfscenario

顺便说一句,这是我在终端看到的,这是我填写所有字段时的结果。

Started POST "/appstores" for 127.0.0.1 at 2015-11-03 22:25:54 +0800
Processing by AppstoresController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7WV4Iw/0uHNSnuXpr8qa39oFEF9gZfKm8EyHGQna0o0=", "subjectinfo"=>{"username"=>"a1", "mobile_user"=>"1"}, "commit"=>"NEXT"}
Rendered appstores/index.html.erb within layouts/application (12.6ms)
Completed 200 OK in 91ms (Views: 88.0ms | ActiveRecord: 0.0ms)

这是当我将它们留空时,但无论它是否为空,它总是渲染到索引......

Started POST "/appstores" for 127.0.0.1 at 2015-11-03 22:25:54 +0800
Processing by AppstoresController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7WV4Iw/0uHNSnfuXpr8qa39oFEF9gZfKm8EyHGQna0o0=", "subjectinfo"=>{"esearch"=>""}, "commit"=>"NEXT"}
Rendered appstores/index.html.erb within layouts/application (12.6ms)
Completed 200 OK in 91ms (Views: 88.0ms | ActiveRecord: 0.0ms)

最佳答案

删除行 match ':controller(/:action(/:id(.:format)))', :via => :all来自你的 routes.rb文件,永远不要再使用它。此模式匹配任何路由并且它位于第一个位置,因此文件中的其他路由根本没有机会。

关于ruby-on-rails - 带有验证的 form_for 未正确调用 "create"操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com