我正在构建一个应用程序,允许人们通过填写表单来与老师签到。我有用户和"签到"我打电话给Pins。我创建了表单,但由于某种原因,它没有呈现用户填写的答案。请注意,用户电子邮件显示在视图中,但不显示在表单中输入的任何内容。
我做错了什么?
_form.html.erb
<%= form_for(@pin) 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= label_tag(:questions, "You can free-write anything you would like to share here, or you can respond to one or more of the prompts below.") %>
<%= f.text_field :questions, class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag(:questions, "You can write about your own actions or thoughts here.") %>
<%= f.text_area :questions, class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag(:questions, "You can write about the actions of another person here.") %>
<p>Please include the names of anyone that was there</p>
<%= f.text_area :questions, class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag(:questions, "If you have a question, comment, or message for your teacher, please write it here.") %>
<%= f.text_area :questions, class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag(:questions, "Are you considering another action? You can write about it here.") %>
<%= f.text_area :questions, class: "form-control" %>
</div>
<div class="form-group">
<%= f.submit "submit", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
show.html
p id="notice"><%= notice %></p>
<p>
<strong>Description:</strong>
<%= @pin.questions %>
</p>
<% if @pin.user == current_user %>
<%= link_to 'Edit', edit_pin_path(@pin) %>
<% end %>
<%= link_to 'Back', pins_path %>
index.html.erb for pins(checkin)
<h1>All your checkins are listed below</h1>
<h3>Checkin</h3>
<% @pins.each do |pin| %>
<p><%= pin.questions %></p>
<p><%= pin.user.email if pin.user %></p>
<%= link_to 'Show', pin %>
<% if pin.user == current_user %>
<%= link_to 'Edit', edit_pin_path(pin) %>
<%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
<% end %>
<br>
<% if user_signed_in? %>
<%= link_to 'New Checkin', new_pin_path %>
<% end %>
Schema.rb
ActiveRecord::Schema.define(version: 20141219162930) do
create_table "pins", force: true do |t|
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.text "questions"
t.integer "user_id"
end
add_index "pins", ["user_id"], name: "index_pins_on_user_id"
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
编辑:
这是我的引脚控制器。我很确定我并没有将这些参数列入白名单。正确。如果有人知道正确的方式,请分享。
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
def index
@pins = Pin.all
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: 'Checkin was successfully created.'
else
render action: 'new'
end
end
def update
if @pin.update(pin_params)
redirect_to @pin, notice: 'Checkin 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(:questions),permit(:id,:user_id,:questions)
end
end
答案 0 :(得分:2)
您使用的是哪种版本的导轨?如果它是导轨4,您是否已将控制器中的引脚列入白名单? http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html