Tôi đã thành công có thể thực hiện việc bổ sung các mục nghệ sĩ mới mà đã không được bao gồm trong Ryan Bates railscast # 258 http://railscasts.com/episodes/258-token-fieldsRails: Sử dụng jquery tokeninput (railscast # 258) để tạo mục mới
Vì vậy, nói cách khác, người dùng có thể nhập tên nghệ sĩ sẽ tự động hoàn thành bằng cách sử dụng jquery tokinput. Tuy nhiên, tôi muốn các kết quả tự động hoàn thành chỉ hiển thị tên nghệ sĩ được tạo bởi người dùng cá nhân đó.
Điều này có hợp lý không? Một ví dụ tốt hơn và dễ hiểu hơn sẽ là cho một collection.rb, nơi người dùng tạo các bài viết và chỉ định một 'bộ sưu tập' cho bài đăng thuộc về, nhưng họ chỉ có thể thêm bài đăng vào bộ sưu tập mà họ tạo ra.
Đây là dạng bài với artist_tokens như một thuộc tính ảo:
<%= form_for @post, :validate => true, :html => {:multipart => true} do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :title, 'Title:' %><br />
<%= f.text_field :title %><br />
<%= f.label :artist_tokens, "Artists" %><br />
<%= f.text_field :artist_tokens, "data-pre" =>
@post.artists.map(&:attributes).to_json %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
này thấy các nghệ sĩ bởi giá trị nhập vào trường artist_tokens về hình thức bưu điện, và tôi cũng nói thêm tùy chọn "Add {params [: q]} "để thêm mục mới.
class ArtistsController < ApplicationController
def index
@artists = Artist.where("name like ?", "%#{params[:q]}%")
results = @artists.map(&:attributes)
results << {:name => "Add: #{params[:q]}", :id => "CREATE_#{params[:q]}_END"}
respond_to do |format|
format.html
format.json { render :json => results }
end
end
Tôi đã thêm mã bổ sung để phân tích cú pháp 'mới' theo id và sau đó tạo một nghệ sĩ mới với chúng. Sau đó, các nghệ sĩ được gán lại.
post.rb
def artist_tokens=(ids)
ids.gsub!(/CREATE_(.+?)_END/) do
Artist.create!(:name => $1).id
end
self.artist_ids = ids.split(",")
end
Mọi thứ đều hoạt động tuyệt vời ngoại trừ khả năng thu hẹp kết quả json chỉ bởi mục nhập của current_user. Tôi sẽ đi đâu để tới đó? Tôi có cần lưu user_id của người tạo mục trong bảng không? Tôi có thể làm cái này như thế nào?
EDIT: hiệp hội cho các mô hình
# app/models/user.rb
class User < ActiveRecord::Base
has_many :posts
has_many :artists, :through => :posts
end
# app/models/post.rb
class Post < ActiveRecord::Base
belongs_to :user
has_many :artisanships
has_many :artists, :through => :artisanships
end
# all/models/artist.rb
class Artist < ActiveRecord::Base
has_many :artisanships
has_many :users, :through => :artisanships
has_many :posts, :through => :artisanships
end
# app/models/artisanship.rb
class Artisanships < ActiveRecord::Base
belongs_to :post
belongs_to :artist
has_one :user, :through => :post
end
EDIT: posts_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user!, :only => [:create, :edit, :update, :destroy]
before_filter :authorized_user, :only => [:destroy, :edit, :update]
def create
@user = current_user
@post = current_user.posts.build(params[:post])
if @post.save
flash[:success] = "Post created!"
redirect_to root_path
else
@feed_items = current_user.feed.paginate(:per_page => "10", :page => params[:page])
render 'pages/home'
end
end
def index
@posts = Post.paginate(:page => params[:page])
end
def show
@post = Post.find(params[:id])
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(post_path(@post), :notice => 'Post was successfully updated.') }
else
format.html { render :action => "edit" }
end
end
end
def destroy
@post.destroy
redirect_to root_path
end
def likers
@title = "Likers"
@post = Post.find(params[:id])
@likers = @post.likers.paginate(:page => params[:page])
render 'show_likers'
end
def search
if params[:q]
query = params[:q]
@search = Post.search do
keywords query
end
@posts = @search.results
end
end
private
def authorized_user
@post = Post.find(params[:id])
redirect_to root_path unless current_user?(@post.user)
end
Edit: Đã cố gắng alias_method_chain để thiết lập thuộc tính user_id bài đầu tiên. (Không làm việc để sửa chữa các mục NULL db) referneced từ: Rails 3: alias_method_chain still used?
def attributes_with_user_id_first=(attributes = {})
# Make sure not to accidentally blank out the important_attribute when none is passed in
if attributes.include?(:user_id)
self.user_id = attributes.delete(:user_id)
end
self.attributes_without_user_id_first = attributes
end
alias_method_chain :attributes=, :user_id_first
'collection.rb' bạn đã đề cập trước đó nhưng bỏ qua nó sau. 'Post' và' Nghệ sĩ' có liên quan như thế nào? chính xác là bạn muốn làm gì? – rubish
Bài đăng và nghệ sĩ có bảng tham gia được gọi là artisanships (sử dụng artist_ids và post_ids). Nếu bạn đã quen thuộc với jquery tokeninput, tôi đã thêm khả năng thêm các mục mới. Tôi muốn tự động hoàn thành chỉ bao gồm các mục do người dùng tạo. –
Collection.rb không tồn tại nó chỉ là một ví dụ để hiểu rõ hơn cách tôi muốn sử dụng jquery tokeninput, bạn có thể nghĩ về các bộ sưu tập thay cho các nghệ sĩ. Người dùng tạo một bài đăng và các loại trong tên bộ sưu tập, nếu đó là một bộ sưu tập mới, họ chọn "add:" tên bộ sưu tập mới "nếu đó là một bộ sưu tập cũ mà họ chỉ chọn. Nếu người dùng tạo bộ sưu tập họ chỉ có thể chọn từ bộ sưu tập của họ –