Rails 3的阵列 - 加在分类类别,然后列表项的项目 [英] Rails 3 array - add items with category then list items in category

查看:104
本文介绍了Rails 3的阵列 - 加在分类类别,然后列表项的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我的Rails 3应用程序的一部分,我希望用户能够点击其他配置文件/页面上的链接,并有链接的字符串值被添加到属于该用户的配置文件的数组。

具体来说,我所希望做的是填充的列表:待办事项的每个配置文件,这取决于 TODO 他们点击。这个想法是,每个 TODO 将下降两类内:内部和外部。所以,点击链接将推动的TODO 值,以内部或外部。那么用户的个人资料会显示一个列表:待办事项内外,并计算总的待办事项为用户个人资料。

由于我初学编程,我得到了一些帮助,在这里SO有关设置此功能;然而,我需要一些帮助完成它。我不能完全似乎所有的点连接起来。我已经建立了一个连接模型,但我不能添加待办事项的字符串值,然后列出/指望它在配置文件中。这里是我的code:

profile.rb型号:

 类档案< ActiveRecord的::基地
  belongs_to的:用户
  accepts_nested_attributes_for:用户  的has_many:profile_todos
  的has_many:待办事项,:通过=> :profile_todos  高清add_todo_inside_item(项目)
    self.profile_todos.build:类别=> '内'
  结束  高清add_todo_outside_item(项目)
    self.profile_todos.build:类别=> '外'
  结束
结束

todo.rb型号:

 类藤< ActiveRecord的::基地
  belongs_to的:简介
结束

profile_todo.rb型号:

 类ProfileTodo< ActiveRecord的::基地
  belongs_to的:简介
  belongs_to的:待办事项
结束

_create_todos.rb迁移:

 类CreateTodos< ActiveRecord的::迁移
  高清self.up
    CREATE_TABLE:待办事项做| T |
      t.string:名称      t.timestamps
    结束
  结束

_create_profile_todos.rb迁移:

 类CreateProfileTodos< ActiveRecord的::迁移
  高清self.up
    CREATE_TABLE:profile_todos做| T |
      t.string:类别
      t.integer:PROFILE_ID      t.timestamps
    结束
  结束

清单用户的配置文件的待办事项:

 < D​​IV ID =待办事项>
  < P><%= @ p​​rofile.first_name%GT;&安培; NBSP;具有&安培; NBSP;<%=复数(@ profile.profile_todos.count,目标)%>< / P>
  < D​​IV ID =待办事项列表>
    < D​​IV CLASS =todos_inside>
      &所述p为H.;&下;%= @ p​​rofile.profile_todos(:类别= GT;内%GT;&下; / P>
    < / DIV>
    < D​​IV CLASS =todos_outside>
      &所述p为H.;&下;%= @ p​​rofile.profile_todos(:类别= GT;外部%GT;&下; / P>
    < / DIV>
  < / DIV>
< / DIV>

add_item为@ profile.todos:

 <立GT;<%=的link_to#:类=> 按钮白'做%GT;<%= @ user.profile.stringtoadd%GT;<%结束%GT;< /李>


解决方案

由于@socjopata提到的,你会需要某种控制器的动作管理​​创造和你的 ProfileTodo 档案大楼。既然你已经创建了一个 ProfileTodo 加入的模式,继续创建一个 ProfileTodosController

更多关于控制器:
http://guides.rubyonrails.org/action_controller_overview.html

的link_to 标记应然后对创建操作的远程调用。为了让一切都正常工作,你很可能需要同时与 PROFILE_ID topic_id 为了使正确的RESTful的交易,这意味着你将不得不提供参数哈希你的的link_to 标记,如果您使用url_options从而可以获得有点混乱。

看传入url_options:
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

最后,要创建一个新的 ProfileTodo 的记录,所以我认为这将是工作更好的说明正在做,如果你使用了的form_for 已经隐藏了 PROFILE_ID topic_id 字段标签。您也可以通过供他们做出形式偏远导轨:遥控=>真正

假设你做一个伴随控制器和添加RESTful资源到你的的config / routes.rb中文件,表单为每个单独的议题将是这个样子:

 <%=的form_for(profile_todo_path,:遥控=>真)并| F | %GT;
  <%= f.hidden_​​field_tag​​:PROFILE_ID,:价值=> @ user.profile.id%GT;
  <%= f.hidden_​​field_tag​​:topic_id,:价值=> topic.id%GT;
  &所述;%= f.submit_tag topic.name%GT;
 <%结束%GT;

您可以随时样式表单,所以如果你只是想显示一个链接,这应该是可行的:)

As part of my Rails 3 app, I want the User to be able to click on links on other profiles/pages and have the string value of the link be added to an array belonging to that User's profile.

Specifically, what I am looking to do is populate a list of :todos for each profile depending on which todo they click. The idea is that each todo will fall within one of two categories: inside and outside. So clicking the links will push the value of the todo to either inside or outside. Then the User's profile will display a list of :todos inside and outside, and count the total of todos for that User's profile.

Since I'm a beginner to programming, I got some help here on SO about setting this up; however I need some help finishing it. I can't quite seem to connect all the dots. I've set up a join model but am not able to add the todo's string value, then list/count it in the profile. Here is my code:

profile.rb Model:

class Profile < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user

  has_many :profile_todos
  has_many :todos, :through => :profile_todos

  def add_todo_inside_item(item)
    self.profile_todos.build :category => 'inside'
  end

  def add_todo_outside_item(item)
    self.profile_todos.build :category => 'outside'
  end
end

todo.rb Model:

class Todo < ActiveRecord::Base
  belongs_to :profile
end

profile_todo.rb Model:

class ProfileTodo < ActiveRecord::Base
  belongs_to :profile
  belongs_to :todo
end

_create_todos.rb Migration:

class CreateTodos < ActiveRecord::Migration
  def self.up
    create_table :todos do |t|
      t.string :name

      t.timestamps
    end
  end

_create_profile_todos.rb Migration:

class CreateProfileTodos < ActiveRecord::Migration
  def self.up
    create_table :profile_todos do |t|
      t.string :category
      t.integer :profile_id

      t.timestamps
    end
  end

Listing the todos in a User's Profile:

<div id="todos">
  <p><%= @profile.first_name %>&nbsp;has&nbsp;<%= pluralize(@profile.profile_todos.count, 'goal') %>.</p>
  <div id="todo-list">
    <div class="todos_inside">
      <p><%= @profile.profile_todos(:category => 'inside' %>.</p>
    </div>
    <div class="todos_outside">
      <p><%= @profile.profile_todos(:category => 'outside' %>.</p>
    </div>
  </div>
</div>

add_item to @profile.todos:

<li><%= link_to "#", :class => 'button white' do %><%= @user.profile.stringtoadd %><% end %></li>

解决方案

As @socjopata mentioned, you're going to need some sort of controller action to manage the creation and building of your ProfileTodo records. Since you already created a ProfileTodo join model, go ahead and create a ProfileTodosController.

More on controllers: http://guides.rubyonrails.org/action_controller_overview.html

Your link_to tag should then make a remote call to the create action. In order to get everything to work properly, you'd most likely need to supply the controller with both the profile_id and topic_id in order to make the correct RESTful transaction, which means you'll have to supply a parameter hash to your link_to tag, which can get kinda messy if you use the url_options.

Look at passing in url_options: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

Ultimately, you are creating a new ProfileTodo record, so I think it would be a better description of the work being done if you used a form_for tag that has hidden fields for the profile_id and the topic_id. You can also make forms remote in rails by supplying them with :remote => true

Assuming you make an accompanying controller and add RESTful resources to your config/routes.rb file, your form for each individual topic would look something like this:

<%= form_for(profile_todo_path, :remote=> true) do |f| %>
  <%= f.hidden_field_tag :profile_id, :value => @user.profile.id %>
  <%= f.hidden_field_tag :topic_id, :value => topic.id %>
  <%= f.submit_tag topic.name %>
 <% end %>

You can always style your forms, so if you only want a link to be displayed, that should be doable :)

这篇关于Rails 3的阵列 - 加在分类类别,然后列表项的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆