Reputation: 11
I am creating a blog and I am trying to create a dashboard where the user can select new in the side menu and it renders the create new article form on the dashboard page. I am trying to accomplish this using an ajax request to render the form. However when I have form_for Article.new the form renders but when I submit nothing happens. I realized my mistake and tried form_for Article but now I am getting a 500 Server error. I tried also creating @article = Article.new in my Dashboard Controller create_new_article action and passing form_for @article but I am still getting the 500 error. Can someone please help me debug why this is happening? I keep getting deprication warnings stating "Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience." I'm not sure if that has anything to do with it though. Thanks !
<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 %>
Upvotes: 0
Views: 108
Reputation: 11
I found a fix but I am not entirely sure I understand why its working now as I am new to 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'
This finally allowed me to create an Articles form on my dashboard controller using an ajax request. Still as I am new to rails I am not sure if this is the best solution to do so. Thanks !
Upvotes: 0