I've made my own app basing on the railstutorial.org website, and i'm now on chapter 11. Everything's fine, i've learned a lot from this tutorial and now i'm continuing work on my app and i'm actually on model "Artists" where every user can create new artist ex.Michael Hartl ;) and add their most popular quotations. The problem is to allow users to follow their favourite artists and see quotations in feed, just like Microposts feed from railstutorial. Artist and User are two different models, and railstutorial dosn't explaing how to make the "follow system" for that. It's like subscribing channels on YouTube etc. Can someone explain me how to get this working? What must i change in code?
我已经基于railstutorial.org网站创建了自己的应用程序,现在我正在第11章。一切都很好,我从本教程中学到了很多东西,现在我继续在我的应用程序上工作,我就是实际上在模特“艺术家”,每个用户都可以创建新的艺术家ex.Michael Hartl;)并添加他们最受欢迎的语录。问题是允许用户关注他们喜爱的艺术家并查看Feed中的引用,就像来自railstutorial的Microposts feed一样。艺术家和用户是两种不同的模型,而铁路教程并没有解释如何制作“跟随系统”。这就像在YouTube上订阅频道等。有人可以解释我如何让这个工作吗?我必须在代码中改变什么?
Answer:
回答:
The button:
按钮:
<%= form_for(current_user.userartists.build(followed_id: @artist.id)) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>
Controller
调节器
class UserartistsController < ApplicationController def create @artist = Artist.find(params[:userartist][:followed_id]) current_user.follow!(@artist) respond_to do |format| format.html { redirect_to @artist } format.js end end end
1 个解决方案
#1
0
You should set up a Artist model and a intermediate model called UserArtist
(or UserFollowsArtist
) where you will store all the matches between users and artists.
您应该设置一个Artist模型和一个名为UserArtist(或UserFollowsArtist)的中间模型,您将在其中存储用户和艺术家之间的所有匹配项。
class User < ActiveRecord::Base
has_many :user_artists
has_many :artists, :through => :user_artists
end
class Artist < ActiveRecord::Base
has_many :user_artists
has_many :users, :through => :user_artists
end
class UserArtist < ActiveRecord::Base
belongs_to :user
belongs_to :artist
end
Now you can call @user = User.first
to get the first user, and @user.artists
to get the list of artists the @user
is following.
现在,您可以调用@user = User.first获取第一个用户,使用@ user.artists获取@user所关注的艺术家列表。
You will have to create a separate controller called UserArtistsController
where you will have actions create
and possibly destroy
(if the user wishes to unfollow the artist).
您将不得不创建一个名为UserArtistsController的独立控制器,您将在其中创建并可能销毁操作(如果用户希望取消关注艺术家)。
In your routes.rb
:
在您的routes.rb中:
resources :user_artists, :only => [:create, :destroy]
resources:user_artists,:only => [:create,:destroy]
I guess the follow button
will be on the Artists
show page so you should have something like this in your view:
我想跟随按钮将在艺术家展示页面上,所以你应该在你的视图中有这样的东西:
<%= button_to "Follow artist", {:controller => :user_artists,
:action => 'create', :artist_id => params[:id] }, :method => :post %>
And in your controller:
在你的控制器中:
class UserArtistsController < ActionController
def create
@user_artist = UserArtist.create(:user_id => current_user.id, :artist_id => params[:artist_id])
@artist = Artist.find(params[:artist_id])
if @user_artist.save
redirect_to @artist
else
flash[:alert] = "Something went wrong, please try again"
redirect_to root_path
end
end
end
Don't forget to create a migration for the Artist
and UserArtist
. UserArtist
table should contain a user_id
and a artist_id
.
不要忘记为Artist和UserArtist创建迁移。 UserArtist表应包含user_id和artist_id。
#1
0
You should set up a Artist model and a intermediate model called UserArtist
(or UserFollowsArtist
) where you will store all the matches between users and artists.
您应该设置一个Artist模型和一个名为UserArtist(或UserFollowsArtist)的中间模型,您将在其中存储用户和艺术家之间的所有匹配项。
class User < ActiveRecord::Base
has_many :user_artists
has_many :artists, :through => :user_artists
end
class Artist < ActiveRecord::Base
has_many :user_artists
has_many :users, :through => :user_artists
end
class UserArtist < ActiveRecord::Base
belongs_to :user
belongs_to :artist
end
Now you can call @user = User.first
to get the first user, and @user.artists
to get the list of artists the @user
is following.
现在,您可以调用@user = User.first获取第一个用户,使用@ user.artists获取@user所关注的艺术家列表。
You will have to create a separate controller called UserArtistsController
where you will have actions create
and possibly destroy
(if the user wishes to unfollow the artist).
您将不得不创建一个名为UserArtistsController的独立控制器,您将在其中创建并可能销毁操作(如果用户希望取消关注艺术家)。
In your routes.rb
:
在您的routes.rb中:
resources :user_artists, :only => [:create, :destroy]
resources:user_artists,:only => [:create,:destroy]
I guess the follow button
will be on the Artists
show page so you should have something like this in your view:
我想跟随按钮将在艺术家展示页面上,所以你应该在你的视图中有这样的东西:
<%= button_to "Follow artist", {:controller => :user_artists,
:action => 'create', :artist_id => params[:id] }, :method => :post %>
And in your controller:
在你的控制器中:
class UserArtistsController < ActionController
def create
@user_artist = UserArtist.create(:user_id => current_user.id, :artist_id => params[:artist_id])
@artist = Artist.find(params[:artist_id])
if @user_artist.save
redirect_to @artist
else
flash[:alert] = "Something went wrong, please try again"
redirect_to root_path
end
end
end
Don't forget to create a migration for the Artist
and UserArtist
. UserArtist
table should contain a user_id
and a artist_id
.
不要忘记为Artist和UserArtist创建迁移。 UserArtist表应包含user_id和artist_id。