我有一个注册和登录的应用程序。登录后,用户可以看到数据库表的内容。现在我希望在登录后,用户还可以通过表单将数据插入另一个表中。由于我想在登录后添加此容量,因此我在会话部分中编写了必要的代码。但它显示以下错误:
ActionController :: SessionsController中的ParameterMissing #create
param丢失或值为空:信息
我的app / controllers / session_controller.rb文件是:
class SessionsController < ApplicationController
def new
super
@info = Information.new
end
def index
@tm
user = User.authenticate(params[:email], params[:password])
@data = Information.all
@num = Information.count
@tym = Time.now.getutc
end
def create
user = User.authenticate(params[:email], params[:password])
@data = Information.all
@num = Information.count
@tym = Time.now.getutc
if user
session[:user_id] = user.id
# redirect_to root_url, :notice => "Logged in!"
# this part is for showing the content of the information table
respond_to do |format|
format.html
format.xml { render xml: @data }
@info = @num
@info = Information.new(information_params)
@info.save
@currentUser = current_user.id
@usr = User.find_by(id: @currentUser)
@other_usr = User.where.not(id: @currentUser)
@usr.send_password_reset_1
@other_usr.each(&:notloggedin)
end
else
flash.now.alert = 'Invalid email or password'
render 'new'
end
end
def information_params
params.require(:information).permit(:age, :name)
end
def refresh_page
redirect_to :back
end
def destroy
@currentUser = current_user.id
@usr = User.find_by(id: @currentUser)
@usr.send_password_reset_0
session[:user_id] = nil
redirect_to root_url, notice: 'Logged out!'
end
end
我的app / views / sessions / create.html.erb文件是: -
<h4>Database contents here:</h4>
<%= @currentUser %></br>
<% @tm=Time.new %>
<%= @tm.strftime("%H:%M:%S") %>
<table>
<tr>
<th>ID</th>
<th>AGE</th>
<th>NAME</th>
</tr>
<% @data.each do |data| %>
<tr>
<td><%= data.id %></td>
<td><%= data.age %></td>
<td><%= data.name %></td>
</tr>
<% end %>
</table>
<div>
<%= form_tag 'refresh_page' do %>
<%= button_tag type: 'submit' do %>
<%= content_tag :div do%>
<h4>View Data </h4><h4><%= @num %></h4>
<% end %>
<% end %>
<% end %>
</div>
<!-- form for adding data into the information database -->
<%= form_for @info do |f| %>
<% if @info.errors.any? %>
<div class="error_messages">
<h2>Form is invalid</h2>
<ul>
<% for message in @user.errors.full_messages %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :age %><br />
<%= f.text_field :age %>
</p>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p class="button"><%= f.submit %></p>
<% end %>
我在这里使用名为User and Information的2个表。 &#39;用户&#39; table保存有关登录凭据的所有信息,我想在&#39;信息&#39;中添加记录。表格通过表格。
我的routes.rb文件是:
Rails.application.routes.draw do
get 'info_view/new'
get 'sessions/new'
#get 'users/new'
#get 'sign_up' => 'users#new' , :as => 'sign_up'
#root :to => 'users#new'
#resources :users
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
root :to => "users#new"
resources :users
resources :sessions
get 'refresh_page' => 'sessions#refresh_page'
post 'refresh_page' => 'sessions#refresh_page'
end
答案 0 :(得分:0)
我认为你让create
和new
感到困惑。您将表单放在create
视图中,该视图仅在处理控制器中的create
端点后呈现。但由于没有传递给create
(因为你还没有打过表格),所以它产生了错误。
将表单移至new.html.erb
,然后转到/new
路径即可访问该表单。之后,您的表单应将提交路由到您的create
端点。 (此时你可以渲染一个create
视图,它只是确认创建了对象,或者你想要的任何东西。)