我正在创建一个'标签'使用act-as-taggable-on创建引脚时用户输入的字段。我已经运行了迁移并编辑了form_html.erb,pins_controller.rb和pin.rb。
标签似乎没有保存,当我打开一个帖子进行编辑时,它们已经显示出来了。
这是我的form_html.erb
update a2
inner join a1 on a1.id = a2.id
set a2.num = a1.num
我的pins_controller.rb
<%= form_for @pin, html: { multipart: true } do |f| %>
<% if @pin.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@pin.errors.count, "error") %> prohibited this pin from being saved:</h2>
<ul>
<% @pin.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %><br>
<%= f.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :date %><br>
<%= f.date_field :date, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :tags, "Tags (separated by commas)"%><br />
<%= f.text_field :tags_list%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我的pin.rb
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 8)
end
def show
end
def new
@pin = current_user.pins.build
end
def edit
end
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
redirect_to @pin, notice: 'Pin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Pin was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@pin.destroy
redirect_to pins_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_pin
@pin = Pin.find(params[:id])
end
def correct_user
@pin = current_user.pins.find_by(id: params[:id])
redirect_to pins_path, notice: "Not authorized to edit this pin" if @pin.nil?
end
# Never trust parameters from the scary internet, only allow the white list through.
def pin_params
params.require(:pin).permit(:description, :image, :date, :tags)
end
end
为什么我似乎无法保存/输入标签?
答案 0 :(得分:0)
我意识到我需要在pin.rb和pin_controller.rb中使用tag_list,这解决了这个问题!