- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚完成了 Ruby on Rails 3 教程。最后一章相当复杂。整本书的教程基本上创建了一个用户可以发布微博的网站。此外,每个用户都可以关注任何其他用户,然后以下用户的微博将显示在原始用户的微博提要中。
我的问题与为什么RelationshipsController 中的create 操作的params 变量包含一个二维数组有关。
这是代码。
用户
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship", dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
end
class Micropost < ActiveRecord::Base
attr_accessible :content
belongs_to :user
end
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
end
<%= form_for(current_user.relationships.build(followed_id: @user.id), remote: true) do
|f| %>
<div><%= f.hidden_field :followed_id %></div>
<%= f.submit "Follow", class: "btn btn-large btn-primary" %>
<% end %>
class RelationshipsController < ApplicationController
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html { redirect_to @user }
format.js
end
end
end
SampleApp::Application.routes.draw do
resources :users do
member do
get :following, :followers
end
end
resources :sessions, only: [:new, :create, :destroy]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
root to: 'static_pages#home'
end
最佳答案
简短的回答:
这不是一个二维数组,它是一个嵌套的关联数组。您使用它的原因是为了获得您真正想要的领域。
长答:
教程假设:当用户点击关注按钮时,目标是调用 current_user.follow!(other_user)
.我将引导您了解代码是如何实现这一点的。所有的魔法都在关系 Controller 和 View 中的 form_for 函数中。
首先,您为新的关系制作表格。因为它是一个嵌套资源,所以您通过关联来构建它。
current_user.relationships.build
current_user.relationships.build(followed_id: @user.id)
class Relationship < ActiveRecord::Base
attr_accessible :followed_id
current_user.follow!(other_user)
。当点击跟随按钮时。所以我们将followed_id作为隐藏字段传入,以便在
params
中访问它在 Controller 中,但用户在 View 本身中也看不到它。
<%= f.hidden_field :followed_id %>
params[:relationship]
follow!
.这很简单。只需从 id 中找到数据库中的用户即可。如何获得followed_id?它是表单中关系对象的一个属性。
params[:relationship][:followed_id]
params[:user][:name]
关于ruby-on-rails-3 - 一个二维数组作为参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508903/
SELECT ID, AppID, Description, Min([Transaction Date]) AS TransactionDate FROM AppProsHist WHERE [De
目前我正在创建规则,该规则应该检查方法是否包含 @Test 和 @TestInfo 注释。如果确实如此,@TestInfo 不应有空参数 testCaseId。 有几种不同的可能方法来填充 testC
是否可以设置参数值,使其在 where 子句中始终结果为 true? 作为示例,考虑一个查询: SELECT name FROM student WHERE class=@parameter; 现在我
我是 JPA 的新手,这是我的查询之一,我有几个参数作为查询的一部分,任何参数都可以为空值 @Query(value = "SELECT ord.purchaseOrderNumber,ord.sal
LOD 参数对 texturelod 取什么值? ?我发现的规范根本没有提到它。它是百分比还是带有百分比的索引值。如果是后者,有没有办法获得纹理具有的 mipmap 数量,以便我能够使用百分比? 最佳
我希望此代码替换现有的 URL 参数“aspid”,但它的作用是在现有的 id 上添加另一个 id。有人可以帮忙吗? $(document).ready(function() { function
在 Spring-boot 项目中,我尝试将 Date 对象作为请求参数传递,并收到此错误: Parameter value [1] did not match expected type [java
在我们的 Jenkinsfile 中,我们有很多参数(参数化构建),在这种情况下,我想检查每个参数是否已切换并对其进行操作。这些参数具有相似的名称,但以不同的小数结尾,因此我想迭代它们以实现此目的。
我的模板之一中有类似于以下内容的内容: 但是 Freemarker 不高兴并给了我: Exception in thread "main" freemarker.core.ParseExceptio
我正在从表单向重定向 servlet 发送一个 post 请求。然后,Servlet 将表单写入其响应 (getWriter) 对象。该表单包含许多隐藏字段。我使用 javascript 提交此表单(
我正在创建一个 JavaScript 组件,我正在根据 jQuery 结果创建该组件的实例,但是,我传递到构造函数中的 DOM 元素虽然在我单步执行调用代码中的循环时已填充,但在传递给构造函数时是未定
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我对 javascript 有疑问。 假设我有这样的 javascript 函数: function show_popup(id) { alert(id); } 编
我目前正在尝试抓取嵌入式 m3u8 url 路径以进行自学。 到目前为止,我设法确定请求会生成带有 m3u8 信息的 json 响应。 例如,https://headlines.yahoo.co.jp
谷歌地图 API 需要这样的参数: NSString *urlString=@"http://maps.google.com/maps?saddr=43.2923,5.45427&daddr=43.4
“parameterValue”是在 Javascript 中的事件上传递的默认参数吗?谁能解释一下这个值从何而来。 Load Ajax content 我发现它在以下文章中使用 - http://w
我有一个 .SWF 电子邮件提交表单。背景颜色通过以下方式设置: `` 并嵌入: `` 是否可以将鼠标悬停在对象或包含的 div 上来更改这些值?即#ffffff 非常感谢! 最佳答案 将 wmode
假设我想用指数函数拟合两个数组x_data_one和y_data_one。为此,我可以使用以下代码(其中 x_data_one 和 y_data_one 被赋予虚拟定义): import numpy
有什么方法可以填充 parameters在基于外部属性文件内容的 Liquibase 变更日志文件中? 例如,我希望能够说: 并将 table.name 的值和
我必须按原样发送参数值 'AbCd/EfgH'。但是 Angular 将 '/' 转义为 %2F。我无法控制 URL。 解决这个问题的最佳方法是什么? 我不想强制 Angular 停止对所有其他 UR
我是一名优秀的程序员,十分优秀!