有人可以帮我解决我的小问题吗?实际上我有Admin模型,它有很多图像存储在Image模型中。我通过Admin模型在Image表中存储一些图像有点问题.I我在下面给出了我的代码。请帮我解决这个问题。
请检查我的以下代码:
<div class="name-div">
Name:<%= @admin.email %>
</div>
<% if signed_in? %>
<div class="button-div">
<a href="/sessions/destroy"><button type="button" class="styled-button-10">LOG OUT</button></a>
</div>
<% end %>
<h1>WEBSITE USER DETAILS</h1>
<table class="gradienttable">
<tr>
<th><p>User id</p></th>
<th><p>User name</p></th>
<th><p>User email</p></th>
<th><p>User image</p></th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><p><%= post.id %></p></td>
<td><p><%= post.name %></p></td>
<td><p><%= post.email %></p></td>
<td><p><%= image_tag(post.picture_url, :width => 90,:height => 90 ) if post.picture.present? %></p></td>
<td><p><%= link_to 'Delete', posts_deluser_path(:id => post.id), :confirm => 'Are you sure?',
:method => :delete %> </p></td>
</tr>
<% end %>
</table>
<center><h1>Add content</h1></center>
<table class="img-table">
<tr>
<th>Add 1st image</th>
<th>Add 2nd image</th>
<th>Add 3rd image</th>
<th>Add 4th image</th>
<th>Add 5th image</th>
</tr>
<%= form_for :images,:url => {:action => "addimage",:controller => "images",:id => @admin.id } do |f| %>
<tr>
<td><%= f.file_field :picture1 %></td>
<td><%= f.file_field :picture2 %></td>
<td><%= f.file_field :picture3 %></td>
<td><%= f.file_field :picture4 %></td>
<td><%= f.file_field :picture5 %></td>
<td><%= f.submit "ADD" %></td>
<td id="addimg"><button type="button" style="cursor:pointer;">Update</button></td>
</tr>
<% end %>
</table>
<table class="update-image">
<tr>
<th>1st image</th>
<th>2nd image</th>
<th>3rd image</th>
<th>4th image</th>
<th>5th image</th>
</tr>
<%= form_for :images,:url => {:action => "updateimage",:controller => "images" } do |f| %>
<tr>
<td><%= f.file_field :picture1 %></td>
<td><%= f.file_field :picture1 %></td>
<td><%= f.file_field :picture1 %></td>
<td><%= f.file_field :picture1 %></td>
<td><%= f.file_field :picture1 %></td>
</tr>
<% end %>
</table>
class ImagesController < ApplicationController
def addimage
@admin=Admin.find(params[:admin_id])
@images=@admin.images.create(params[:images])
redirect_to :action => 'adminpage',:controller => 'posts'
end
def updateimage
end
end
Blogpost::Application.routes.draw do
root :to => "posts#index"
get "posts/login" => "posts#login"
get "posts/show" => "posts#show"
post "posts/logincreate" => "posts#logincreate"
get "posts/admin" => "posts#admin"
post "sessions/adminlogin" => "sessions#adminlogin"
get "posts/adminpage" => "posts#adminpage"
delete "posts/deluser" => "posts#deluser"
get "sessions/destroy" => "sessions#destroy"
get "posts/content" => "posts#content"
post "images/addimage" => "images#addimage"
post "images/updateimage" => "images#updateimage"
resources :posts do
resources :comments
end
end
class Image < ActiveRecord::Base
belongs_to :admin
attr_accessible :picture1, :picture2, :picture3, :picture4, :picture5
mount_uploader :picture ,PictureUploader
end
class Admin < ActiveRecord::Base
attr_accessible :email, :password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
has_many :images
end
class CreateImages < ActiveRecord::Migration
def change
create_table :images do |t|
t.string :picture1
t.string :picture2
t.string :picture3
t.string :picture4
t.string :picture5
t.references :admin
t.timestamps
end
add_index :images, :admin_id
end
end
请检查上述代码,并尝试通过管理模型帮助我将图像存储在数据库中。提前谢谢。
答案 0 :(得分:0)
修改您的代码,如下所示
<%= form_for :images,:url => {:action => "addimage",:controller => "images" } , :html => { :multipart => true } do |f| %>
<tr>
<td><%= f.file_field :picture1 %></td>
<td><%= f.file_field :picture2 %></td>
<td><%= f.file_field :picture3 %></td>
<td><%= f.file_field :picture4 %></td>
<td><%= f.file_field :picture5 %></td>
<td><%= f.hidden_field :admin_id, :value => @admin.id %></td> #add the admin Id
</tr>
<% end %>
class Image < ActiveRecord::Base
belongs_to :admin
attr_accessible :picture1, :picture2, :picture3, :picture4, :picture5, :admin_id
mount_uploader :picture ,PictureUploader
end