I'm building a menu of categories that can be updated using :remote => true
in Rails without reloading the page.
My new
action successfully renders as javascript and the changes show up on the page however my create
action seems to render successfully but doesn't show up on the page. I can see the changes when I reload the page so I know the create action is working. It's just that the javascript doesn't seem to render the page properly. However, if I look at the response of the POST
request in Firebug, I can see the intended changes.
Please help. I've spent hours on StackOverflow already trying to find a solution.
My Categories controller
class CategoriesController < ApplicationController
def new
@category = Category.new()
end
def create
@category = Category.new(category_params)
if @category.save
redirect_to(:controller => 'apps', :action => 'index')
else
render('new')
end
@categories = Category.where(:parent_id => nil)
end
def update
@category.update_attributes(category_params)
end
private
def category_params
params.require(:categories).permit(:name, :parent_id)
end
end
My Apps Controller
class AppsController < ApplicationController
def index
@category = nil
@categories = Category.where(:parent_id => nil)
@apps = App.all
respond_to do |format|
format.html
format.json
format.js
end
end
def new
@app = App.new()
end
def create
@app = App.new(params[:id])
if @app.save
redirect_to(:action => 'index')
else
render('new')
end
end
private
def apps_params
params.require(:apps).permit(:name, :category, :image)
end
end
The Create form (partial)
<%= simple_form_for :categories, remote: true,
:url => url_for(:action => 'create', :controller => 'categories'),
:method => 'post' do |f| %>
<%= f.input :name %>
<% end %>
Create.js.erb
$('#categories-list').html("<%= j (render 'catList') %>");
$('#category-form').slideUp(350);
_catList.html.erb
<li class="pure-menu-heading">Categories</li>
<% @categories.each do |cat| %>
<li class="pure-menu-item"><a href="#" class="pure-menu-link"><span class="email-label-personal"></span><%= cat.name %></a></li>
<% end %>
Solved!
I found some guys to help me on another forum.
All I had to do was remove redirect_to(:controller => 'apps', :action => 'index')
in the categories controller
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.