Rails:分析页面上的用户更改以插入数据库

发布于 2024-10-12 13:33:46 字数 363 浏览 1 评论 0原文

我似乎无法提出简洁的问题,但无论如何:

我目前尝试编写一个应用程序,允许用户自定义保存他们的状态。但是,我不知道如何将自定义信息传递给控制器​​操作。

我不需要使用 link_to :controller => "controller, :action => "create", :params # => paramters

我可以轻松地将状态作为 JSON 对象传递,但是我如何传递自定义?

a) 我让用户在其中选择两个人许多人可能喜欢以这种方式显示:A、B 和其他 6 个人都喜欢这样,所以我必须能够更新类似的数组

b) 我希望允许用户删除评论,因此我。必须传递剩余评论的数组

有人有建议吗?

I dont seem to be able to formulate concise question, but anyways:

I currently try to write an app which allows users to customizable save their statusses. However, I dont know how to pass the customized information to the controller action.

Dont I have to use link_to :controller => "controller, :action => "create", :params # => paramters

I can easily pass the status as a JSON object, but how do i pass the customizations?

a) i let the users choose the two persons among the many people that may like to be shown in the manner: Person A, Person B and 6 other people like this. so i would have to be able to update the like array

b) I want to enable the user to delete comments, hence I will have to pass the array of the remaining comments.

Anyone got suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅语花开 2024-10-19 13:33:46

老实说,我不确定我是否理解这个问题,但让我尝试一下。

假设您有充满用户的数据库。因此,我们有(注意,只定义了最少的字段):

用户模型:

# fields name:string
class User < ActiveRecord::Base
  has_many :statuses
end

状态模型:

# fields status:string
class Status < ActiveRecord::Base
  belongs_to :user
  has_may :status_comments
end

StatusComment 模型:

# fields comment:string
class StatusComment < ActiveRecord::Base
  belongs_to :status
end

然后您应该有一个文本字段,您可以在其中写入更改视图中的状态。这实际上创建了可以评论的新状态(以便您可以将其与其他状态区分开)。

现在假设您的控制器中有 @user = User.find(params[:id])

那么在你看来,你应该做类似的事情。我还没有测试过它,但它应该有效,或者至少让你知道该怎么做。

<h1><%= @user.name %></h1> 
<%= form_for(@user.status.new) do |f| %>
  <%= f.text_field :status, :value => @user.status.last.status %>
  <%= f.submit %>
<% end %>
<div>
  <%= @user.status.last %>
  <% @user.status.last.comments.each do |comment| %>
  <p>
    <%= comment.comment %><%= link_to "Delete comment", comment, :method => delete, :confirm => "Are you sure?" %>
  </p>
</div>

这应该使您能够:

  1. 每次提交时保存新状态
    您的表单
  2. 能够删除有关状态的评论

您应该澄清a),因为我不知道您想用它做什么。

Honestly I am not sure I understand the question, but let me give it a try.

Let's say you have database full of users. So, we have (notice, only minimal fields are defined):

User model:

# fields name:string
class User < ActiveRecord::Base
  has_many :statuses
end

Status model:

# fields status:string
class Status < ActiveRecord::Base
  belongs_to :user
  has_may :status_comments
end

StatusComment model:

# fields comment:string
class StatusComment < ActiveRecord::Base
  belongs_to :status
end

Then you should have a textfield where you write change your status in view. This in reality creates new status which can be commented on (so you can distinguish it from some other status).

Now let's say you have @user = User.find(params[:id]) in your controller.

Then in your view, you should do something like. I have not tested it, but it should work or at least give you a decent idea what to do.

<h1><%= @user.name %></h1> 
<%= form_for(@user.status.new) do |f| %>
  <%= f.text_field :status, :value => @user.status.last.status %>
  <%= f.submit %>
<% end %>
<div>
  <%= @user.status.last %>
  <% @user.status.last.comments.each do |comment| %>
  <p>
    <%= comment.comment %><%= link_to "Delete comment", comment, :method => delete, :confirm => "Are you sure?" %>
  </p>
</div>

This should enable you to:

  1. save new status every time you submit
    your form
  2. be able to delete comments on status

You should clarify a) since I have no idea what is it you are trying to do with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文