简体   繁体   English

Rails create action呈现为Javascript,但未显示在页面上

[英]Rails create action renders as Javascript but doesn't show up on page

I'm building a menu of categories that can be updated using :remote => true in Rails without reloading the page. 我正在构建一个类别菜单,可以在Rails中使用:remote => true进行更新,而无需重新加载页面。

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. 我的new动作成功地以javascript呈现,并且更改显示在页面上,但是我的create动作似乎成功呈现,但未显示在页面上。 I can see the changes when I reload the page so I know the create action is working. 我在重新加载页面时可以看到更改,因此我知道create动作正在起作用。 It's just that the javascript doesn't seem to render the page properly. 只是javascript似乎无法正确呈现页面。 However, if I look at the response of the POST request in Firebug, I can see the intended changes. 但是,如果我在Firebug中查看POST请求的响应,则可以看到预期的更改。

Please help. 请帮忙。 I've spent hours on StackOverflow already trying to find a solution. 我已经在StackOverflow上花费了数小时,已经在尝试寻找解决方案。

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 我的Apps控制器

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 Create.js.erb

$('#categories-list').html("<%= j (render 'catList') %>");
$('#category-form').slideUp(350);

_catList.html.erb _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 我要做的就是在类别控制器中删除redirect_to(:controller => 'apps', :action => 'index')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM