【问题标题】:acts_as_taggable issue - Everything looks almost fineact_as_taggable 问题 - 一切看起来都很好
【发布时间】:2014-06-06 21:32:36
【问题描述】:

我只是一步一步地遵循这个other question,但我的应用程序仍然给我一些关于标记的错误(rails 4)

我遇到的错误是:Desktop/hack/app/controllers/jacks_controller.rb:85: syntax error, unexpected end-of-input, expecting keyword_end end ^

我已经按照聊天中的建议重新计划了,但没有任何改变。

参考代码

我的模型之间没有关联,(在我提到的链接中,有关联)

杰克表格

<%= form_for(@jack) do |f| %>
<% if @jack.errors.any? %>
<div id="error_explanation">
  <h2><%= pluralize(@jack.errors.count, "error") %> prohibited this jack from being saved:       </h2>

  <ul>
  <% @jack.errors.full_messages.each do |message| %>
    <li><%= message %></li>
  <% end %>
  </ul>
 </div>
 <% end %>

 <div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
 <div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :tag_list, "Tags (separated by comma)" %><br>
<%= f.text_field :tag_list %>
</div>
<div class="field">
<%= f.label :picture %><br>
<%= f.text_field :picture %>
</div>
<div class="actions">
<%= f.submit %>

jack.rb

class Jack < ActiveRecord::Base
    acts_as_taggable_on :tags

end

路线 Rails.application.routes.draw 做

get 'tagged/index'

root :to => redirect('/jacks')

 get 'about' => "about#info"
 get 'submit' => "jacks#create"


 resources :jacks
match 'tagged', to: 'jacks#tagged', :as => 'tagged', via: 'get'

资源:用户

jack 主动助手

module JacksHelper 
include ActsAsTaggableOn::TagsHelper
end

和控制器

class JacksController < ApplicationController
before_action :set_jack, only: [:show, :edit, :update, :destroy]

# GET /jacks
# GET /jacks.json
def index
if params [:tag]
  @jacks = Jack.tagged_with(params[:tag])
else
  @jacks = Jack.all
end

# GET /jacks/1
# GET /jacks/1.json
def show
end

# GET /jacks/new
def new
@jack = Jack.new
end

# GET /jacks/1/edit
def edit
end

# POST /jacks
# POST /jacks.json
def create
@jack = Jack.new(jack_params)

respond_to do |format|
  if @jack.save
    format.html { redirect_to @jack, notice: 'Jack was successfully created.' }
    format.json { render :show, status: :created, location: @jack }
  else
    format.html { render :new }
    format.json { render json: @jack.errors, status: :unprocessable_entity }
     end
   end
 end

 # PATCH/PUT /jacks/1
 # PATCH/PUT /jacks/1.json
 def update
 respond_to do |format|
  if @jack.update(jack_params)
    format.html { redirect_to @jack, notice: 'Jack was successfully updated.' }
    format.json { render :show, status: :ok, location: @jack }
  else
    format.html { render :edit }
    format.json { render json: @jack.errors, status: :unprocessable_entity }
  end
  end
 end

 # DELETE /jacks/1
 # DELETE /jacks/1.json
def destroy
@jack.destroy
respond_to do |format|
  format.html { redirect_to jacks_url, notice: 'Jack was successfully destroyed.' }
  format.json { head :no_content }
 end
end

def tagged
if params[:tag].present? 
@jacks = Jack.tagged_with(params[:tag])
else 
@jacks = Jack.postall
 end      
 end

 private
  # Use callbacks to share common setup or constraints between actions.
  def set_jack
  @jack = Jack.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def jack_params
  params.require(:jack).permit(:title, :description, :picture, :tag_list)
 end
end

【问题讨论】:

  • 错误出在jacks_controller中,那个控制器是什么样子的?
  • 请分享您的控制器文件:Desktop/hack/app/controllers/jacks_controller.rb

标签: ruby-on-rails acts-as-taggable


【解决方案1】:

索引方法缺少“结束”

def index
  if params[:tag]
    @jacks = Jack.tagged_with(params[:tag])
  else
    @jacks = Jack.all
  end
end

【讨论】:

  • 如果我修复它,在 JacksController#index 中返回 ArgumentError 错误数量的参数(1 为 0)# GET /jacks.json def index if params [:tag] @ jacks = Jack.tagged_with(params[:tag]) else @ jacks = Jack.all
  • @malditojavi 已修复,params 后面不应该有空格
  • 没有空格` # GET /jacks.json def index if params [:tag] @jacks = Jack.tagged_with(params[:tag]) else @jacks = Jack.all`跨度>
  • @malditojavi params 和 [:tag] 之间有一个不应该存在的空格,我在答案中修复了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 2019-08-21
  • 1970-01-01
  • 2021-06-06
相关资源
最近更新 更多