开发者

Objects Being Attached to their Creator Question

I am working on a rails application where one user class named (submitters) are able to login and once they are logged in they create videos. My videos controller is here:

class VideosController < ApplicationController
  def index
    @videos = Video.find :all

  end

  def new
    @submitter = current_submitter
    @video = @submitter.videos.build
  end

  def create
    @submitter = current_submitter
    @video = @submitter.videos.build(params[:video])
    if @video.save
      @video.convert
      flash[:notice] = 'Video has been uploaded'
      redirect_to :action => 'index'
    else
      render :action => 'new'
    end
  end

   def show
    @video = Video.find(params[:id])
  end

 def destroy
    @video = Video.find(params[:id])
    @video.destroy
    flash[:notice] = "Successfully deleted the video."
    redirect_to root_url
  end

  def update_date
    @video = Video.find(params[:id])
    @video.update_attributes(params[:video])
      flash[:notice] = "Successfully added a launch date!"
     redirect_to @video


  end
end

As you can probably see, I am trying to construct the controller so that when a video is created, it is created as belonging to the submitter who upload the video (via the video new view). I am using a auth system with a current_submitter method written in the application controller.

Now it lets me upload a video fine when I am logged in as a submitter. The trouble for me is working out how to display information in my view. If I want to display some columns with information about the video and then others with information about the submitter who uploaded the video, how do I go about doing that from the controller (index action), into the index view. My current view which does not work in below:

 <% title "Films Submitted" %>

<table>
  <tr>
    <th>Title</th>
    <th>Film Type</th>
    <th>Premiere</th>
    <th>Company</th>
    <th>Name</th>

   </tr>
   <% for video in @videos do %>
    <tr>
       <td><%= link_to video.title, video %></td>
       <td><%= video.film_type %></td>
    <% if video.premiere == "true" %>
       <td>Premiere</td>
    <% else %>
       <td><%= %></td>
    <% end %>  
       <td><%= video.submitter.company %></td>
       <td><%= video.submitter.name %></td>
      <td><%= link_to "Delete", video, :confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% end %>
</table>
<br><br>
<%= link_to "Upload a Video", new_video_path %>

Any suggestions or tips from rails developers would be much appreciative... I am new and trying to learn.

Video Model:

class Video < ActiveRecord::Base
belongs_to :submitter
    has_attachment :content_type => :video, 
    开发者_Python百科             :storage => :file_system, 
                 :max_size => 50.megabytes


end

Submitter Model:

class Submitter < ActiveRecord::Base

  acts_as_authentic

has_many :videos

end

Schema:

  create_table "videos", :force => true do |t|
    t.string   "title"
    t.text     "description"
    t.string   "state"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "content_type"
    t.integer  "size"
    t.string   "filename"
    t.string   "film_type"
    t.boolean  "premiere",       :default => false
    t.date     "preferred_date"
    t.text     "reason"
    t.integer  "submitter_id"
    t.date     "actual_date"
  end

  create_table "submitters", :force => true do |t|
    t.string   "name"
    t.string   "company"
    t.string   "email"
    t.string   "username"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "video_id"
  end


I think you've probably only setup your associations in one direction. You already have:

class Submitter < ActiveRecord::Base
  has_many :videos
end

But you also need to reciprocate that relationship in the video model:

class Video < ActiveRecord::Base
  belongs_to :submitter
end

This will create the submitter method in your video model, that references the submitter it belongs to.

UPDATE:

After you updated your question with the model info, I noticed one small thing: You have a video_id column on your submitters table that doesn't need to be there. Only the "belongs_to" side of the association needs a foreign key. But this isn't causing your problem.

I think you might have a video in your database without a submitter! In the index action, since you're not checking for a valid submitter before you start using it, if even a single video record is missing a submitter_id, it'll break the view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜