小编Dev*_*sch的帖子

Rails 4 - Link_to动作用params创建

我想在我的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)

ruby-on-rails link-to

3
推荐指数
1
解决办法
3362
查看次数

标签 统计

link-to ×1

ruby-on-rails ×1