Rails 4中的自动完成表单

时间:2015-04-13 02:49:32

标签: ruby-on-rails ruby jquery-ui jquery-ui-autocomplete

我正在尝试在Rails 4中的表单中创建一个自动填充字段。我按照README提供的相同说明正确完成了。

在README中,提到它适用于Simple表单,但我也看到它由form_for使用。在我的应用中,它会在自动填充文本字段中显示Array (#<Author:0x00000105e59588>),而不是从中选择的结果下拉列表。

非常感谢您的帮助。

帖子和作者模型

class Post < ActiveRecord::Base
  belongs_to :author
  validates :title, :body, presence: true
  validates :status, presence: true, inclusion: %w{Publish Draft}
  has_many :tags, as: :taggable
  attr_accessor :name
end

class Author < ActiveRecord::Base
  rolify
  include Tokenable
  has_secure_password
  validates :email, :password, presence: true, on: :create
  validates_confirmation_of :password
  has_many :posts, dependent: :nullify
end

帖子控制器

class PostsController < ApplicationController
  before_filter :authenticate_author, except: [:index, :show]
  before_filter :set_author, except: :index
  autocomplete :authors, :first_name, :full => true, :column_name => 'first_name'

  def new
    @post = @author.posts.build
  end

  def create
    @post = @author.posts.build(post_params)
    @post.tags << Tag.find_or_create_by(name: params[:post][:name])
    if @post.save
      flash[:success] = "Post published successfully."
      redirect_to posts_path
    else
      flash.now.alert = "Please check again."
      render "new"
    end
  end

  private
    def post_params
      params.require(:post).permit(:title, :body, :status, :name)
    end

    def set_author
      @author = current_user.present? ? current_user : Author.find(params[:id])
    end
end

路由文件

Rails.application.routes.draw do

  # You can have the root of your site routed with "root"
  root 'posts#index'
  get ':password_reset_token/edit', to: 'authors#edit_password', as: 'edit_password'
  get 'authors/:id/profile', to: 'authors#myprofile', :as => :myprofile
  resources :authors do 
    member do
      get 'posts', to: 'posts#myposts'
    end 
  end
  resources :sessions, only: [:new, :create, :destroy]
  get 'forgot_password', to: 'authors#forgot_password'
  post 'reset_password', to: 'authors#reset_password'
  resources :posts do
    get :autocomplete_authors_first_name, :on => :collection
  end
end

我的Application.js文件

//= require jquery
//= require jquery_ujs
//= require autocomplete-rails
//= require dataTables/jquery.dataTables
//= require_tree .

这是表格

<h2> Create a post </h2>
<%= form_for @post, method: :post do |f| %>
  <% if @post.errors.any? %>
    <% @post.errors.full_messages.each do |msg| %>
      <div class="error_messages">
        <%= msg %>
      </div>
    <% end %>
  <% end %>
  <div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :body %>
    <%= f.text_area :body %>
  </div>
  <div class="field">
    <%= f.label :author %>
    <%= f.autocomplete_field :author, autocomplete_authors_first_name_posts_path, :multiple => false, :"data-autocomplete-label" => "Sorry, nothing found."  %>
  </div>
  <div class="field">
    <%= f.label 'Tag' %>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :status %>
    <%= f.select :status, options_for_select(%w[Publish Draft]), selected: true %>
  </div>
  <div class="actions">
    <%= f.submit 'Post' %>
  </div>
<% end %>

0 个答案:

没有答案