vendredi 20 février 2015

How can i save values in database using rails association?

Can anybody please help me to resolve my small issue ?Actually i have Admin model and it has many images which will be stored in Image model.I am facing little bit problem to store some images in Image table via Admin model.I am giving my codes below.Please help me to resolve this issue.


Please check my below codes:


views/posts/adminpage.html.erb:



<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>


controller/images_controller.rb



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


routes.rb



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


images.rb



class Image < ActiveRecord::Base
belongs_to :admin
attr_accessible :picture1, :picture2, :picture3, :picture4, :picture5
mount_uploader :picture ,PictureUploader
end


admin.rb



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


db/migrate/20150220093517_create_images.rb



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


Please check the above codes and try to help me to store images in database via Admin model.Thanks in advance.


Aucun commentaire:

Enregistrer un commentaire