我想在我的rails应用程序中添加一个链接,这将允许我的用户直接"喜欢"资源(属性).单击该链接会将相同内容添加到数据库中,而不向用户询问任何内容.
该like表:user_id,property_id
正如你所看到的,user_id并且property_id必须在link_toas params中.
routes.rb中:
resources :likes
resources :properties
Run Code Online (Sandbox Code Playgroud)
指数:
<%= link_to "Like this property", new_like_path(current_user, property.id) %> #does not work but you get the idea
Run Code Online (Sandbox Code Playgroud)
控制器:
def new
@like = Like.new
end
def create
@like = Like.new(like_params)
respond_to do |format|
if @like.save
format.html { redirect_to @like, notice: 'Like was successfully created.' }
format.json { render action: 'show', status: :created, location: @like }
else
format.html { render action: 'new' …Run Code Online (Sandbox Code Playgroud)