在选择框中显示所有微博

时间:2017-01-25 18:27:53

标签: ruby-on-rails

以下代码创建一个选择框

<%= select_tag "microposts", options_from_collection_for_select(@microposts, "id", "name"), { :prompt => 'All microposts' } %>

我想在选择框中显示所有微博,但我收到以下错误

NoMethodError in Managments#edit
undefined method `map' for nil:NilClass
Did you mean?  tap

有人可以向我解释如何在选择框中显示所有微博吗?

控制器

class ManagmentsController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]
  before_action :correct_user,   only: :destroy

    def index
     @managments = current_user.managments
     @micropost = current_user.microposts.build
    end

  def show
     @microposts = Micropost.paginate(page: params[:page])
   @managment = Managment.find_by(id: params[:id])
   if !@managment
    raise ActionController::RoutingError.new('Not Found')
   end
   @user = @managment.user

  end

  def new
    @user = User.new
    @managment = Managment.new
  end

  def edit
     @managment = Managment.find(params[:id])
  end

    def create

     @managment = current_user.managments.build(managment_params)
     if @managment.save
      flash[:success] = "Managment created!"
      redirect_to @managment
     else
      @feed_items = current_user.feed.paginate(page: params[:page])
      render 'new'
     end
    end

  def update
     @managment = Managment.find(params[:id])

    if @managment.update(managment_params)
      redirect_to @managment
    else
      render 'edit'
    end
  end

  def destroy
    @managment.destroy
    flash[:success] = "Managment deleted"
    redirect_to managments_path
  end


  private

    def managment_params
      params.require(:managment).permit( 
      :title,  :budget,
      :procent1, :procent2, :procent3, :procent4,
      :procent5, :procent6, :procent7,
      :procent8, :procent9, :procent10,
      :procent11, :procent12, :result1,
      :result2, :result3, :objectivesname1,
      :objectivesname2, :objectivesname3, 
      :lowprocent1, :lowprocent2, :lowprocent3,
      :medprocent1, :medprocent2, :medprocent3,
      :highprocent1, :highprocent2, :highprocent3,
      :lowobjectives1, :lowobjectives2, :lowobjectives3,
      :medobjectives1, :medobjectives2, :medobjectives3,
      :highobjectives1, :highobjectives2, :highobjectives3

      )
    end

    def correct_user
      @managment = current_user.managments.find_by(id: params[:id])
      redirect_to managments_path if @managment.nil?
    end

end

1 个答案:

答案 0 :(得分:0)

问题的答案可以在评论

中找到
def edit
  @managment = Managment.find(params[:id])
  @microposts = Micropost.paginate(page: params[:page])
end