Rails: Analyze User changes on a page to insert to Database
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?
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:
- save new status every time you submit your form
- 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.
精彩评论