调用Data

时间:2016-11-16 20:52:14

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

我正在为用户使用devise gem。我有一个contents_controller.rb,我想在我的application.html.erb中显示content.name,但是当我这样做时,似乎与Devise有冲突。当我加载页面时,数据会被渲染,但是当我点击登录时,为了访问编辑功能,我得到了NoMethodError in Devise::Sessions#new undefined method `name' for nil:NilClass。我的代码列在下面。

Application.html.erb:

<% @contents.each do |content| %>
 <a class="navbar-brand" ><%= content.name %></a>
<%end%>

Contents_controller.rb:

class ContentsController < ApplicationController
  before_action :find_content, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @contents = Content.all
    @services = Service.all.order("created_at asc")
    @portfolios = Portfolio.all.order("created_at asc")
  end

  def new
    @content = Content.new
  end

  def create
    @content = Content.new content_params

    if @content.save
      redirect_to root_path, notice: "content Created!"
    else
      render 'new', notice: "Sorry content failed to create!!!"
    end
  end

  def edit
  end

  def update
    if @content.update content_params
      redirect_to root_path, notice: "You Updated: #{@content.name}."
    else
      render 'edit'
    end
  end

  def destroy
    @content.destroy
    redirect_to content_path
  end

  private

  def content_params
    params.require(:content).permit(:name, :header_title, :services_title, :slogan_title, :about_title, :about_description, :contact_title, :contact_subtitle, :fb_username, :ig_username, :twitter_username, :in_username, :pin_username)
  end

  def find_content
    @content = Content.find(params[:id])
  end
end

有人可以帮我解释一下为什么会这样,以及如何解决这个问题?

0 个答案:

没有答案