我不太了解为什么在URL甚至都没有更改的情况下,单击简单的Rails表单上的Submit按钮就会将页面刷新为“您要查找的页面不存在”。提交具有有效数据的表单似乎也不会在任何数据库中创建这样的条目。任何方向将不胜感激。
下面是/app/views/helps/new.html.erb
html, body {
margin: 0;
height: 100%;
overflow: hidden;
}
.container {
margin-top: 60px;
}
</style>
<% if logged_in? %>
<div class= "container">
<% provide(:title, "Submit Inquiry") %>
<br><h2><center>Submit Inquiry</center></h2>
<p></p>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(:helps, url: new_help_path) do |f| %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :text %>
<%= f.text_area :text, class: 'form-control' %>
<p></p>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
</div>
<% else %>
<div class="alert alert-danger"><br>
<br><%= link_to "Please log in to continue", root_path %>
</div>
<%end%>
下面是helps_controller
class HelpsController < ApplicationController
before_action :get_user, only: [:edit, :update]
before_action :valid_user, only: [:edit, :update]
def new
end
def create
@user= User.find(params[:id])
if current_user?(@user)
flash[:success] = "Inquiry submitted."
redirect_to new_help_path
else
flash.now[:danger] = "Only users can submit inquiries."
redirect_to root_url
end
end
private
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
def valid_user
unless (@user && @user.activated? &&
@user.authenticated?(:reset, params[:id]))
redirect_to root_url
end
end
end
以下是我的路线。rb
Rails.application.routes.draw do
root 'sessions#new'
get '/home', to: 'static_pages#home'
get '/add/parts', to: 'static_pages#part'
get '/add/projects', to: 'static_pages#project'
get '/add/vendors', to: 'static_pages#vendor'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
#users can generate new passwords (reset), and change them
resources :password_resets, only: [:new, :create, :edit, :update]
resources :helps, only: [:new, :create]
end
以下是我_header.html.erb
中的相关代码<body>
<header>
<ul class= "nav">
<li><a class="active" href="/home">Home</a></li>
<% if logged_in? %>
<li><%= link_to "Profile", edit_user_path(current_user.id) %></li>
<%end%>
<li id= "options">
<a href="#">Add</a>
<ul class= "subnav">
<li><%= link_to "Part", add_parts_path%></li>
<li><%= link_to "Project", add_projects_path%></li>
<li><%= link_to "Vendor", add_vendors_path%></li>
</ul>
</li>
<li><%= link_to "Help", new_help_path, method: :create %></li>
<li><%= link_to "Log Out", logout_path, method: :delete %></li>
<li id= "search">
<form action= "" method= "get">
<input type="text" name="search_text" id= "search_text" placeholder="Search Page"/>
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</li>
</ul>
</header>
</body>
下面是我的index.html.erb
<p id="notice"><%= notice %></p>
<h1>Posts</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.body %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% @helps.each do |help| %>
<%= help.email %>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Post', new_post_path %>
答案 0 :(得分:0)
您似乎在create方法中找到了一个用户,然后将其重定向到new_helps_path。
def新
@help = Help.new
结束
def创建
@ user = User.find(params [:id])
#@help(或任何命名的模型)= Help.new
如果current_user?(@ user)
@ help.create(电子邮件:params [:email])
flash [:success] =“已提交查询。”
redirect_to new_help_path
其他
flash.now [:danger] =“只有用户可以提交查询。
redirect_to root_url
结束
结束
在<%= form_for(@help, url: RAKE ROUTES AND PICK THE PATH WITH ACTION CREATE) do |f| %>
中,路径应该是您发布表单的位置,在这种情况下,它是带有POST的create方法
仅:帮助:[:创建,索引,显示,新建]
def index
@helps = Help.all
end
然后在index.html.erb
<% @helps.each do |help| %>
<%= help.email %>
<% end %>