从:var变为@var - 产生问题并解决问题

时间:2013-05-02 12:07:16

标签: ruby-on-rails forms render

可以在以下问题上提供一些帮助。我的帖子内容表格不起作用。我已经列出了下面的所有相关代码。

从@video_post调整到:video_post,打破表单,因为表单仅适用于主页。

Routing Error

No route matches [POST] "/users/1"

使用@video_post - 表单在主页上工作,但网站在其他地方中断。

undefined method `model_name' for NilClass:Class

有人可以帮我解决这个问题吗?

非常感谢。

我在共享/表单中有此表单 - _form.html.erb

  <%= form_for(@video_post) do |f| %>
    <%= f.text_field :video_title, placeholder: "Video Title"  %>
    <%= f.text_area :video_description, placeholder: "Description" %>
    <%= f.text_field :video_url, placeholder: "URL" %>
    <%= f.submit "Post", class: "submit" %>
  <% end %>

我使用:/ / h1>从/users/_new.html.erb渲染
<%= render :partial => 'shared/form', :video_posts => @video_posts %>

使用以下命令从application.html.erb呈现:

<%= render :partial => 'users/new', :locals => { :user => @user ||= User.new } %>

视频帖子控制器

class VideoPostsController < ApplicationController

  before_filter :signed_in_user


  def create
    @video_post = current_user.video_posts.build(params[:video_post])
    if @video_post.save
      flash[:success] = "Video posted!"
      redirect_to root_path
    else
      render 'static_pages/home'
    end
  end

  def destroy

  end


end

静态页面控制器

class StaticPagesController < ApplicationController


  def home
    @video_post = current_user.video_posts.build if signed_in?
  end


end

VideoPost模型

class VideoPost < ActiveRecord::Base
  attr_accessible :video_category, :video_description, :video_title, :video_url
  belongs_to :user

  validates :user_id,           presence: true
  validates :video_title,       presence: true
  validates :video_description, presence: true, length: { maximum: 140 }
  validates :video_url,         presence: true

  default_scope order: 'video_posts.created_at DESC'

end

用户模型

class User < ActiveRecord::Base
  attr_accessible :comment, :email, :password, :password_confirmation, :username
  has_secure_password 
  has_many :video_posts, dependent: :destroy

  before_save { |user| user.email = user.email.downcase }
  before_save :create_remember_token 


  validates :username, presence: true, length: { maximum: 30 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
                    uniqueness: { case_sensitive: false }

  validates :password, length: { minimum: 6 }
  validates :password_confirmation, presence: true

  private

  def create_remember_token
    self.remember_token = SecureRandom.urlsafe_base64
  end
end

使用form_for时的堆栈跟踪 - @ video-post

app/views/shared/_video_post_form.html.erb:3:in `_app_views_shared__video_post_form_html_erb__4602377331012207168_70262656586760'
app/views/users/_new.html.erb:3:in `_app_views_users__new_html_erb___487495860206913900_70262669645280'
app/views/layouts/application.html.erb:25:in `_app_views_layouts_application_html_erb___3991259861222421382_70262670169940'

使用时:video_post

表格不起作用或给我

Routing Error

No route matches [POST] "/users/1"

谢谢!

0 个答案:

没有答案