English 中文(简体)
将索引页面请求放入铁路
原标题:Put Request From Index Page In Rails

我正在制作一个网站, 里面有一堆照片, 每张照片下都有一张Uppote的按钮。 我希望该按钮能够提交 PUT 请求, 更新数据库中的字段 。 目前我有一个代码 :

<% @users.each do |user| %>
    <%= image_tag user.avatar.url(:thumb) %>
    <% form_for (@user) do |f| %>
    <input name="commit" type="submit" value="Upvote">
    <% end %>
<% end %>

我希望当用户点击上调的按钮时,它会调用 PUT 请求, 更新值, 而不是重定向到同一页。 我无法找到如何做到这一点 。

最佳回答

首先,您的代码错误: 将 form_for( @ user) 替换为 for_for( user)

其次,由于 user 对象已经存在, 它默认为 PUT 。 所以您不需要做任何工作 。

问题回答

如果您想要上传一个用户( 不是图片? ), 然后将此放入您的 user_ controller. rb 文件 :

def upvote
  @user = User.find(params[:user])
  @user.upvote
end

在您的应用程序/ 模型/ 用户. rb 中

def upvote
  self.votes += 1
  self.save
end

在您的配置/ 路由. rb 中

resources :user do
  put :upvote
end

没有测试,但应该给你一个起点。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...