Rails为子表添加单独的视图

时间:2014-09-08 01:22:15

标签: ruby-on-rails ruby ruby-on-rails-4

我正在创建一个基本应用程序,其中用户拥有个人资料并且可以上传最多5张他/她的照片。我创建了用户个人资料页面,我希望允许用户上传不同页面的图片。就像有一个链接添加/编辑照片,然后单击它以转到另一个页面,提交后应重定向回配置文件页面并更新/插入记录。所以,如果我在照片模型/控制器或成员模型/控制器下这样做,我有点困惑。这是我的示例代码,链接如下  我使用回形针进行图片上传     会员观点

 <%= link_to 'Add/Edit Photo' , edit_member_path(current_member.id) %>
 <%= form_for(@member, :remote=> true,  html: {
                                    :multipart => true,
                                    class:"form-light padding-15"}) do |f| %>
   <%= f.label :firstname %>
   <%= f.text_field :firstname, autofocus: true, :class => "form-control",:placeholder   =>"FirstName" %>

   <%= f.label :lastname %>
  <%= f.text_field :lastname,autofocus: true, :class => "form-control",:placeholder =>"LastName"%>

 <% end %> 

class Member < ActiveRecord::Base
 attr_accessible :firstname, :lastname, :user_id, :dob,:gender,:l_country_id,:age,
  :l_state_id,:l_city,:g_country_id,:residency_status,:marital_status,
  :community,:sub_community,:height,:weight,:complexion,:body_type,
     :diet,:smoke,:drink,:education,:working_as,:working_with,:mobile_no,:about_yourself,:disability,:photos_attributes


 has_many :photos
 belongs_to :country
  belongs_to :user
 belongs_to :state
accepts_nested_attributes_for :photos

end


class Photo < ActiveRecord::Base
  belongs_to :member
  has_attached_file :data, :styles => { :thumb => "100x100#",
 :medium => "500x500#",
 :large => "700x700>" },
 :url => "/assets/member/:id/:style/:basename.:extension",
   :path => ":rails_root/public/assets/member/:id/:style/:basename.:extension"

 #validates_attachment_presence :data
 validates_attachment_content_type :data, :content_type => /\Aimage/
 #validates_attachment_size :data, :less_than => 5.megabytes
validates_attachment_content_type :data, :content_type => ['image/jpeg', 'image/png'] 
 attr_accessible :member_id,:data


  end

1 个答案:

答案 0 :(得分:0)

由于单独的页面仅用于照片,我的建议是使用PhotosController。如果您担心,可以使用嵌套资源从网址获取member_id。

http://guides.rubyonrails.org/routing.html#nested-resources