我多年来一直在努力解决这个问题,似乎没有任何相关问题有用。我试图通过隐藏字段收集用户的IP,然后将其显示在适用的消息上。当我尝试实现它时,我在表单页面上收到以下错误。我不确定是否由于更新表或什么问题,但是当我运行Message.all 时,ip值不会出现 这是相关的代码: 运行Messages.all时的控制台:
irb(main):002:0> Message.all
Message Load (2.6ms) SELECT "messages".* FROM "messages"
=> #<ActiveRecord::Relation [#<Message id: 30, subject: "Pears", content: "Pears", post_time: nil, created_at: "2015-07-02 19:49:32", updated_at: "2015-07-02 19:49:32">, #<Message id: 31, subject: "Pears", content: "Pears\r\n", post_time: nil, created_at: "2015-07-14 17:49:12", updated_at: "2015-07-14 17:49:12">, #<Message id: 32, subject: "Hi", content: "Muffin", post_time: nil, created_at: "2015-07-14 17:52:23", updated_at: "2015-07-14 17:52:23">]>
irb(main):003:0>
消息模型的迁移文件:
class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.string :subject
t.string :content
t.string :ip
t.date :post_time
t.timestamps null: false
end
end
end
消息控制器:
class MessagesController < ApplicationController
def index
@messages=Message.all
end
def new
@messages=Message.new
end
def show
@message=Message.find(params[:id])
end
def destroy
@messages=Message.find(params[:id])
@messages.destroy
redirect_to '/'
end
def create
@messages = Message.new(message_params)
if @messages.save
redirect_to '/'
else
render 'new'
end
end
private
def message_params
params.require(:message).permit(:content, :subject, :id, :ip)
end
end
提交消息视图:
<div class="formWrapper">
<%=form_for Message.new do |t|%>
<div class ="Inputs">
<div class="row">
<div class="input-field col s6">
<%=t.text_field :subject, :class => "validate", :length => '25', :placeholder => 'Subject'%><br>
<%=t.text_area :content, :class => "materialize-textarea", :placeholder => "Message"%>
<%=t.hidden_field :ip, :value => request.remote_ip %>
<div class="g-recaptcha" data-sitekey=""></div>
</div>
</div>
<div class="submit">
<%=t.submit "Submit", :class => "btn"%>
</div>
<%end%>
</div>
</div>
索引页面(显示消息的位置):
<div class="main">
<%@messages.each do |t|%>
<div class="cWrap">
<div class="col s12 m6">
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<span class="card-title"><%=link_to t.subject, show_path(t)%></span>
<div class="divider"></div>
<p><%=t.content%></p>
<p><%=t.ip%></p>
</div>
<div class="card-action">
<%=link_to "Delete Message", delete_path(t), :method => 'delete'%>
</div>
</div>
</div>
</div>
<% end %>
</div>
答案 0 :(得分:2)
首先,这是一个非常糟糕的主意,每个人都可以直接在表单中更改IP(例如通过chrome dev工具)...最好在控制器中保存消息之前设置ip:
@message = Message.new(message_params)
@message.ip = request.remote_ip
@message.save
接下来,如果你看一下控制台
irb(main):002:0> Message.all
Message Load (2.6ms) SELECT "messages".* FROM "messages"
=> #<ActiveRecord::Relation [#<Message id: 30, subject: "Pears", content: "Pears", post_time: nil, created_at: "2015-07-02 19:49:32", updated_at: "2015-07-02 19:49:32">, #<Message id: 31, subject: "Pears", content: "Pears\r\n", post_time: nil, created_at: "2015-07-14 17:49:12", updated_at: "2015-07-14 17:49:12">, #<Message id: 32, subject: "Hi", content: "Muffin", post_time: nil, created_at: "2015-07-14 17:52:23", updated_at: "2015-07-14 17:52:23">]>
irb(main):003:0>
对象消息没有 ip 属性!您似乎将 ip 字段添加到已应用的迁移中,请再次回滚和迁移(以创建消息表)或删除 t.string:ip 并创建新的迁移到只需将ip添加到消息表:
def change
add_column :messages, :ip, :string
end
答案 1 :(得分:0)
irb(main):002:0> Message.all
Message Load (2.6ms) SELECT "messages".* FROM "messages"
=> #<ActiveRecord::Relation [#<Message id: 30, subject: "Pears", content: "Pears", post_time: nil, created_at: "2015-07-02 19:49:32", updated_at: "2015-07-02 19:49:32">, #<Message id: 31, subject: "Pears", content: "Pears\r\n", post_time: nil, created_at: "2015-07-14 17:49:12", updated_at: "2015-07-14 17:49:12">, #<Message id: 32, subject: "Hi", content: "Muffin", post_time: nil, created_at: "2015-07-14 17:52:23", updated_at: "2015-07-14 17:52:23">]> irb(main):003:0>
正如我所看到的,您的任何消息中都没有ip
属性。我猜您稍后会在迁移文件中添加该字段,并忘记迁移。