1

提交表单时我需要重定向到用户/索引页面,但浏览器显示相同的页面,删除所有表单并显示这个奇怪的地址:

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

我的用户/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

哇!原来问题出在 tag<form>上。没有它一切正常,但为什么呢?Bootstrap 说:将 .form-horizo​​ntal 添加到表单中

4

3 回答 3

0

redirect_to错了,它应该重定向到一个路径(url)。

尝试:

redirect_to users_path
于 2013-08-16T12:24:23.780 回答
0

您可以直接重定向到一个动作也只需使用

redirect_to :action => :index
于 2013-08-16T12:38:24.130 回答
0

问题出在您使用的两个标签中<form>form_for此外,建议在标签之外或之前呈现错误消息。对于添加类或任何 html 属性可以添加到form_for或类似的标签,:attr_name => "value"所以如果你想添加class到假设myclass你可以这样做:class => "myclass"

于 2013-08-19T04:18:13.127 回答