Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 为Rails 4中的Ajaxified表单创建表单_中的新对象_Jquery_Ruby On Rails_Ajax_Forms_Ruby On Rails 4 - Fatal编程技术网

Jquery 为Rails 4中的Ajaxified表单创建表单_中的新对象

Jquery 为Rails 4中的Ajaxified表单创建表单_中的新对象,jquery,ruby-on-rails,ajax,forms,ruby-on-rails-4,Jquery,Ruby On Rails,Ajax,Forms,Ruby On Rails 4,使用Rails 4,制作一个简单的单页应用程序 我的根目录设置为索引页,在那里我显示一系列非常短的文章。另一方面,我有一个表格,以便有人可以很容易地提交一个新的职位 我决定对表单提交进行Ajaxify,这样新的帖子就可以添加到列表的顶部,而无需重新加载页面,表单就会被清除 关于我的表格的一个注意事项是:每个帖子都属于一家公司,因此我在表格中有。在创建操作中,我对该公司调用first\u或\u create,因此如果该公司不存在,则在提交时创建该公司并与新帖子相关 最终,我必须在我的表单中显式地创

使用Rails 4,制作一个简单的单页应用程序

我的根目录设置为索引页,在那里我显示一系列非常短的文章。另一方面,我有一个表格,以便有人可以很容易地提交一个新的职位

我决定对表单提交进行Ajaxify,这样新的帖子就可以添加到列表的顶部,而无需重新加载页面,表单就会被清除

关于我的表格的一个注意事项是:每个帖子都属于一家公司,因此我在表格中有
。在创建操作中,我对该公司调用
first\u或\u create
,因此如果该公司不存在,则在提交时创建该公司并与新帖子相关

最终,我必须在我的表单中显式地创建新对象,而不是将局部变量传递给我的partials并在表单中使用裸词。我试图理解为什么会这样,以及我如何最终实现一种更为普遍接受的方法

在实现下面的最终解决方案之前,我试图让它这样工作:在posts控制器中的索引操作中,不仅声明
@post=post.new
@company=company.new
,而且还声明
@new\u company=company.new
@new\u post=post.new
。然后在我的create.js.erb文件中,当重新呈现表单时,将
@new_company
@new_form
作为本地变量传递,如
$(“.js ref form”).html(“”)
。这似乎是更标准的方法,但直到我放弃了实例变量和局部变量,并在表单中显式创建了新对象,我才能让它工作

posts_controller.rb:

class PostsController < ApplicationController

  respond_to :html, :js

  def index
    @company = Company.new
    @post = Post.new
    @posts = Post.all
  end

  def create
    @company = Company.where(name: post_params[:company]).first_or_create
    @post = @company.posts.build(company_id: @company.id, details: post_params[:details], link: post_params[:link],
                                         expiration: post_params[:expiration], code: post_params[:code], limit: post_params[:limit])
    if @post.save
      flash[:success] = "Post submitted!"
    else
      flash[:danger] = "Problem submitting post. Please try again."
    end

    respond_with(@post) do |f|
      f.html { redirect_to root_path }
    end
  end
型号:

post.rb

class Post < ActiveRecord::Base
  default_scope -> { order('created_at DESC') }
  validates :details, presence: true, length: { maximum: 140 }

  belongs_to :company
class Post{order('created_at DESC')}
验证:详细信息,状态:true,长度:{最大值:140}
属于:公司
company.rb

class Company < ActiveRecord::Base
  has_many :posts, dependent: :destroy
end
class公司
好的,算了。为了能够将局部变量传递到我的表单partial并在表单中使用裸词,我必须在
posts#create
操作中为新帖子和新公司对象创建实例变量,而不是像以前那样在index操作中创建

然后在我的index.html.erb文件中,我可以在create.js.erb中执行

<% if @post.valid? %>
  $(".js-posts").prepend("<%= escape_javascript(render(@post)) %>");
  $(".js-post-form").html("<%= escape_javascript(render partial: 'form', locals: { post: @new_post, company: @new_company }) %>");
<% else %>
  $(".js-post-form").html("<%= escape_javascript(render partial: 'form', locals: { post: @post, company: @company }) %>");
<% end %>

现在我不必在表单中显式创建对象。尽管任何进一步的改进建议仍然受到欢迎。

我可以看看你的
路线吗。rb
?当然,我会把它添加到上面和模型中,你有
公司有很多:帖子
帖子属于:公司
?你的观点。。。他们在
视图/帖子中
?嗯。。。我什么都没有。一切看起来都很标准=/
CompanyPosts::Application.routes.draw do
  resources :posts

  root to: 'posts#index'
end
class Post < ActiveRecord::Base
  default_scope -> { order('created_at DESC') }
  validates :details, presence: true, length: { maximum: 140 }

  belongs_to :company
class Company < ActiveRecord::Base
  has_many :posts, dependent: :destroy
end
<% if @post.valid? %>
  $(".js-posts").prepend("<%= escape_javascript(render(@post)) %>");
  $(".js-post-form").html("<%= escape_javascript(render partial: 'form', locals: { post: @new_post, company: @new_company }) %>");
<% else %>
  $(".js-post-form").html("<%= escape_javascript(render partial: 'form', locals: { post: @post, company: @company }) %>");
<% end %>
<%= form_for post, html: { class: 'form-horizontal' }, remote: true do |f| %>
  <h2>Add Post</h2>
  <%= render 'shared/error_messages' %>

  <%= f.label company, "Company" %>
  <%= fields_for company do |company| %>
      <%= company.text_field :name, class: 'form-control' %>
  <% end %>
  # The rest of the form is the same as above...

class PostsController < ApplicationController

  respond_to :html, :js

  def index
    @company = Company.new
    @post = Post.new
    @posts = Post.all
  end

  def create
    @company = Company.where(name: company_params[:name]).first_or_create
    @post = @company.posts.build(post_params)
    @new_post = Post.new
    @new_company = Company.new

    if @post.save
      flash[:success] = "Post submitted!"
    else
      flash[:danger] = "Problem submitting post. Please try again."
    end

    respond_with(@post) do |f|
      f.html { render :index }
    end
  end

  private

    def post_params
      params.require(:post).permit(:details)
    end

    def company_params
      params.require(:company).permit(:name)
    end
end