我需要在提交表单时redirect_to用户/索引页面,但浏览器显示相同的页面,删除所有表单并显示这个奇怪的地址:
http://localhost:3000/signup?utf8=%E2%9C%93&authenticity_token=aKZ7qWC9ZUMyFXYNxvVfL%2F4BpTSB0sZtpm1a4AWNvWE%3D&user%5Bsecondname%5D=&user%5Bfirstname%5D=&user%5Bpatronymic%5D=&user%5Bdateofbirth%283i%29%5D=&user%5Bdateofbirth%282i%29%5D=&user%5Bdateofbirth%281i%29%5D=&user%5Blogin%5D=&user%5Bemail%5D=&user%5Bpassword%5D=&user%5Bpassword_confirmation%5D=&commit=%D0%97%D0%B0%D1%80%D0%B5%D0%B3%D0%B8%D1%81%D1%82%D1%80%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%D1%81%D1%8F
当我用空表单提交它时没有相同的事情发生(没有错误列表) 虽然RSpec说所有测试都通过......
我的USERS_CONTROLLER.RB:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
redirect_to action: :index
else
render 'new'
end
end
def index
@users = User.all
end
def destroy
@user = User.find(params[:id])
@user.destroy
redirect_to action: :index
end
end
我的USERS / NEW.HTML.ERB
<% provide(:title, 'Sign up') %> <h1>Регистрация</h1>
<div class="container">
<form class="form-signin form-horizontal">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<div class="control-group">
<div class="controls">
<%= f.text_field :secondname, :class => "input-block-level", :placeholder => "Фамилия" %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= f.text_field :firstname, :class => "input-block-level", :placeholder => "Имя" %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= f.text_field :patronymic, :class => "input-block-level", :placeholder => "Отчество" %>
</div>
</div>
<%= f.label :dateofbirth, :"Дата рождения", :class => "control-label" %>
<div class="control-group">
<div class="controls">
<%= f.date_select :dateofbirth, start_year: 1933, end_year: 2006, :order => [:day, :month, :year], :prompt => { :day => 'День', :month => 'Месяц', :year => 'Год' } %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= f.text_field :login, :class => "input-block-level", :placeholder => "Логин" %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= f.text_field :email, :class => "input-block-level", :placeholder => "Email" %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= f.password_field :password, :class => "input-block-level", :placeholder => "Пароль" %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= f.password_field :password_confirmation, :class => "input-block-level", :placeholder => "Подтверждение пароля" %>
</div>
</div>
<div class="control-group">
<div class="controls">
<%= f.submit "Зарегистрироваться", :id => "register", :class => "btn btn-info" %>
</div>
</div>
<% end %>
</form> </div>
我的_ERROR_MESSAGES.HTML.ERB:
<% if @user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(@user.errors.count, "error") %>.
</div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
USER_PAGES_SPEC.RB的一部分
describe "signup" do
before { visit signup_path }
let(:submit) { "register" }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "user_firstname", with: "Ivan"
fill_in "user_secondname", with: "Ivanov"
fill_in "user[patronymic]", with: "Ivanovich"
select "2000", from: "user[dateofbirth(1i)]"
select "November", from: "user[dateofbirth(2i)]"
select "11", from: "user[dateofbirth(3i)]"
fill_in "user[login]", with: "IvanIvanov"
fill_in "user[email]", with: "Ivan@gmail.com"
fill_in "user[password]", with: "foobar"
fill_in "user[password_confirmation]", with: "foobar"
end
it "should create a user" do
expect { click_button submit }.to change(User, :count).by(1)
end
end
哇!事实证明问题出在标记<form>
中。没有它一切都很好,但为什么呢? Bootstrap说:将.form-horizontal添加到表单
答案 0 :(得分:0)
您的redirect_to
错误,应该重定向到路径(网址)。
尝试:
redirect_to users_path
答案 1 :(得分:0)
您可以直接重定向到某个操作,也可以使用
redirect_to :action => :index
答案 2 :(得分:0)
问题出在您使用的<form>
的两个标签中。另外,建议在form_for
标记之外或之前呈现错误消息。对于添加类或任何html属性,可以添加到form_for
或类似的标签,例如:attr_name => "value"
,因此如果您想添加class
以假设myclass
,您可以像:class => "myclass"
那样{1}}