[英]Rails 500 Server Error, Ajax Request to form_for partial
我正在创建一个博客,我正在尝试创建一个仪表板,用户可以在其中 select new 在侧边菜单中,并在仪表板页面上呈现创建新文章表单。 我正在尝试使用 ajax 请求来呈现表单来完成此操作。 但是,当我有form_for Article.new时,表单会呈现,但是当我提交时,什么也没有发生。 我意识到我的错误并尝试了form_for 文章,但现在我收到 500 服务器错误。 我还尝试在我的仪表板 Controller create_new_article 操作中创建@article = Article.new 并传递form_for @article ,但我仍然收到 500 错误。 有人可以帮我调试为什么会这样吗? 我不断收到弃用警告,指出“弃用] 主线程上的同步 XMLHttpRequest 已弃用,因为它对最终用户的体验产生不利影响。” 我不确定这是否与它有关。 谢谢 !
<div class="row fill-screen dashboard">
<div class="column-1">
<%= render 'side_menu' %>
</div>
<div class="column-10">
<div id="content"></div>
</div>
<div class="column-1">
</div>
</div>
class DashboardController < ApplicationController
def home
end
private
def render_partial
respond_to do |format|
format.html {}
format.js
end
end
def show_all_articles
render_partial
end
def create_new_article
render_partial
end
end
<div class="side-menu">
<div>
<h2>Articles</h2>
<%= link_to "Show", show_all_articles_path, remote: true %>
<%= link_to "New", create_new_article_path, remote: true %>
</div>
</div>
$('#content').html("<%= escape_javascript(render :partial => 'create_new_article')%>");
<%= tinymce_assets %>
<form>
<%= form_for Article do |f| %>
<%= f.label :title, 'Title' %>
<%= f.text_field :title %>
<%= f.label :thumbnail, 'Thumbnail URL'%>
<%= f.text_field :thumbnail%>
<%= f.label :description, 'Description' %>
<%= f.text_field :description %>
<%= f.label :content, 'Content' %>
<%= f.text_area :content, :class => 'tinymce'%>
<%= f.submit %>
<% end %>
</form>
<%= tinymce %>
class ArticlesController < ApplicationController
skip_before_action :admin, except: [:show]
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
flash[:notice] = "Article was successfully created"
redirect_to article_path(@article)
else
render dashboard_path
end
end
def index
@articles = Article.all
end
private
def article_params
params.require(:article).permit(:title, :description)
end
def show
@article = Article.find(params[:id])
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
flash[:notice] = "Article was updated"
redirect_to article_path(@article)
else
flash[:notice] = "Article was not updated"
render 'edit'
end
end
#Articles
resources :articles
#Dashboard
get 'dashboard', to: 'dashboard#home', as: 'dashboard'
get 'show_all_articles', to: 'dashboard#show_all_articles'
get 'create_new_article', to: 'dashboard#create_new_article'
ActionView::Template::Error (undefined method `to_key' for #<Class:0x00007f897f5ddaf8>
Did you mean? to_query):
1: <%= tinymce_assets %>
2: <form>
3: <%= form_for Article do |f| %>
4:
5: <%= f.label :title, 'Title' %>
6: <%= f.text_field :title %>
我找到了一个修复程序,但我不完全确定我理解为什么它现在可以工作,因为我是 Rails 新手。
#controller
def create_new_article
@article = Article.new
end
#view
<%= form_with model: @article do |f| %>
#view
<%= form_with model: Article do |f| %>
#view
<%= form_with model: Article.new do |f| %>
#routes
post 'create_new_article', to: 'articles#create'
这最终允许我使用 ajax 请求在我的仪表板 controller 上创建一个文章表单。 仍然因为我是 Rails 新手,我不确定这是否是最好的解决方案。 谢谢 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.