将RoR注册脚本添加到引导模式 [英] Adding a RoR registration script into a bootstrap modal
问题描述
所以我试图创建一个注册脚本内Bootstrap模态
,但它失败。
同一脚本在其赢得的页面上工作
这里有一些关于使用bootstrap的特别的东西吗?
错误:以下是模态内容
< div class =modal fadeid =register-modaltabindex = - 1>
< div class =modal-dialog>
< div class =modal-content>
< div class =modal-header>
< button type =buttonclass =closedata-dismiss =modalaria-label =关闭>< span aria-hidden =true>& times; ; / span>< / button>
< h4 class =modal-title>注册< / h4>
< / div>
<%= form_for(@user)do | f | %>
< div class =modal-body>
< div class =form-group>
< label for =inputEmailclass =col-sm-2 control-label>电子邮件< / label>
< div class =col-sm-10>
<%= f.text_field:first_name,:placeholder => 名字%>
< / div>
< / div>
< div class =form-group>
< label for =inputPasswordclass =col-sm-2 control-label> Password< / label>
< div class =col-sm-10>
<%= f.text_field:last_name,:placeholder => 姓氏%> < / div>
< / div>
< div class =form-group>
< label for =inputPasswordclass =col-sm-2 control-label> Password< / label>
< div class =col-sm-10>
<%= f.email_field:email,:placeholder => 电子邮件%>
< / div>
< / div>
< div class =form-group>
< label for =inputPasswordclass =col-sm-2 control-label> Password< / label>
< div class =col-sm-10>
<%= f.password_field:password,:placeholder => 密码%>
< / div>
< / div>
< div class =modal-footer>
<%= f.submit创建帐户,类:btn btn-primary btn-submit%>
< / div>
<%end%>
< / div>
my Users controller
class UsersController< ApplicationController
def new
@user = User.new
end
end
和用户模型
class User< ApplicationRecord
has_secure_password
end
我的db:为我的用户迁移文件
class CreateUsers< ActiveRecord :: Migration
def change
create_table:users do | t |
t.string:first_name
t.string:last_name
t.string:email
t.string:password_digest
t.timestamps
end
end
end
p>
Rails.application.routes.draw do
root'home#index'
get' signup'=> 'users#new'
resources:users
end
在Ruby中,你可以引用一个实例变量,即使它没有被初始化:
:001:0> @foo
=> nil
这里发生了什么。如果你想对一堆动作使用相同的形式,而不用担心控制器是否设置了 @user
实例变量,你只需使用 ||
(or)
operator:
<%= form_for(@user || User.new)do | f | %>
#...
<%end%>
so i am trying to create a signup script within a Bootstrap Modal but it fails out.
the same script works on a page of its won, just not sure why it doesnt work here.
is there something special about using bootstrap?
Here is the Error:
Below is the Modal Contents
<div class="modal fade" id="register-modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Register</h4>
</div>
<%= form_for(@user) do |f| %>
<div class="modal-body">
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<%= f.text_field :first_name, :placeholder => "First name" %>
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<%= f.text_field :last_name, :placeholder => "Last name" %> </div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<%= f.email_field :email, :placeholder => "Email" %>
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<%= f.password_field :password, :placeholder => "Password" %>
</div>
</div>
<div class="modal-footer">
<%= f.submit "Create an account", class: "btn btn-primary btn-submit" %>
</div>
<% end %>
</div>
Here is my Users controller
class UsersController < ApplicationController
def new
@user = User.new
end
end
and the user model
class User < ApplicationRecord
has_secure_password
end
and finally my db:migrate file for my users
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :password_digest
t.timestamps
end
end
end
One more bit of is my routes
Rails.application.routes.draw do
root 'home#index'
get 'signup' => 'users#new'
resources :users
end
In Ruby you can reference a instance variable even if it has not been initialized:
irb(main):001:0> @foo
=> nil
Which is exactly what is happening here. If you want to use the same form on a bunch of actions without worrying if the controller sets the @user
instance variable you can just use the ||
(or)
operator:
<%= form_for(@user || User.new) do |f| %>
# ...
<% end %>
这篇关于将RoR注册脚本添加到引导模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!