Rails -TypeError: can't cast ActionController::Parameters to text

前端 未结 1 2011
野性不改
野性不改 2021-01-21 14:48

I\'m developing a website using jQuery Preview to fetch the title, description or favction_url of any link.

jQuery Preview: https://github.com/embedly/jquery-preview/

1条回答
  •  抹茶落季
    2021-01-21 15:26

    In your create action you're trying to assign link_params hash as value for content which expects text.

    With the call to Link.new(...) passing in attributes you are mass assigning attributes, and with Rails4 strong parameters you need to add all the attributes to the permit list that you will be mass assigning.

    Update the create and link_params method definition as follows:

    # app/controllers/links_controller.rb
    
    def create
        @link = Link.new(link_params)
    
        if @link.save
            redirect_to root_path
        else
            render :new
        end
    end
    
    private
    
    def link_params
        params.require(:link).permit(:content, :title, :description, :favicon_url)
    end
    

    Update: Merge certain attributes from the parameter hash and merge them to params[:link]

    # app/controllers/links_controller
    
    private
    
    def link_params
      # Select parameters you need to merge
      params_to_merge = params.select { |k, v| ['title', 'description', 'favicon_url'].include?(k) }
      params.require(:link).permit(:content).merge(params_to_merge)
    end
    

    0 讨论(0)
提交回复
热议问题