Messages Controller中未定义的方法`stringify_keys' [英] Undefined method `stringify_keys' in Messages Controller
问题描述
我在尝试提交表单时遇到以下错误(表单显示1个用户想要发送其他用户时)
MessageController中的NoMethodError#创建
未定义的方法`stringify_keys'为:String
Rails.root:/ Users / fkhalid2008 / loand
应用程序跟踪|框架跟踪|全跟踪
app / controllers / messages_controller.rb:31:在`new'
app / controllers / messages_controller.rb:31:在`create'
以下是Messages Controller的相关代码:
def new
@message = Message.new
if params [:reply_to]
@reply_to = @ user.received_messages.find(params [:reply_to])
除非@ reply_to.nil?
@ message.to = @ reply_to.sender.login
@ message.subject =Re:#{@reply_to.subject}
@ message.body =\\\
\ n * Original message * \\\
\\\
#{@reply_to.body}
end
end
end
def create
@message = Message.new(params [:message])
@ message.sender = @user
@ message.recipient = User.find_by_login(params [:message] [:to])
if @ message.save
flash [:notice] =发送消息
redirect_to user_messages_path(@user)
else
render:action => :new
end
end
我该如何解决这个问题?
更多支持代码如下:
消息>新视图(这是Form创建的地方)
<%= form_for @message,:url => messages_path(:user_id => @user)do | f | %GT;
< br>
< br />
< br />
< div class =field>
< label>名称< / label>
< input type =textname =nameid =contact_nameclass =required/>
< / div>
< div class =field>
< label>电子邮件< / label>
< input type =textname =emailid =contact_emailclass =required/>
< / div>
< div class =field>
< label>主题< / label>
< input type =textname =subjectid =contact_subjectclass =required/>
< / div>
< div class =field>
< label>讯息< / label>
< textarea name =messagerows =8cols =25id =contact_messageclass =required max_len,max_length = 1000 />< / textarea>
< / div>
< div class =btn>
<%= submit_tag'发送消息'%>
< / div>
<%end%>
短信模式
class Message< ActiveRecord :: Base
is_private_message
attr_accessor:to
end
用户模型
class User< ActiveRecord :: Base
$ b has_many:posts
has_one:profile
has_private_messages
$ b attr_accessible:电子邮件
validates_presence_of:email
validates_uniqueness_of:email,:message =>嗯,这封电子邮件已经存在了
validates_format_of:email,:with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i,:message => 您好!请使用有效的电子邮件
end
POSTS> INDEX VIEW(这是Form链接的地方)
< table class =table table -striped>
< tbody>
<%@ posts.each do | post | %GT;
< tr>
< td>我是<%= post.title%>结婚<%= post.job%>在<%= post.location%>中,并寻找<%= post.salary%> ;.我的预算是<%= post.salary%>。< / td>
< td> < button class =btndata-toggle =buttononClick =javascript:location.href ='/ messages / new'; />联系与LT; /按钮>< / TD>
< td><%= time_ago_in_words(post.created_at)%>前< / TD>
<! - - /。
< td><%= link_to'显示',发布%>< / td>
< td><%= link_to'编辑',edit_post_path(post)%>< / td>
< td><%= link_to'Destroy',post::confirm => '你确定吗?',:method => :删除%>< / td>
- >
< / tr>
<%end%>
< / tbody>
< / table>
开发日志
在2012年4月22日星期一01:54:57 +0500开始POST/ messages127.0.0.1
通过MessagesController处理#以HTML格式创建
参数:{name=>faisal,commit=>Send Message,authenticity_token=" Qyww9hP6jLmqpQ + ua21FAk9vePju8Gpq39Gpaflf3wE =,utf8=" \342\234\\ ,邮件=>fas@fas.com}
用户负载(0.1ms)SELECT用户。* FROMusers限制1
在17ms内完成500内部服务器错误
NoMethodError(未定义方法`stringify_keys'为hello:String):
app / controllers / messages_controller.rb:31:在`new'
app / controllers / messages_controller.rb:31:在`create'
现在我明白了。
<%= form_for @message,:url =>您已经以完全不一致的方式构建您的表单。 messages_path(:user_id => @user)do | f | %GT;
然后:
< input type =textname =nameid =contact_nameclass =required/>
是什么导致了这个文本字段与消息无关的情况。你是否在最后一个标记中看到 message
的提及?
解决方法很简单:使用助手。
而不是您的标记:
< label>名称< / label>
< input type =textname =nameid =contact_nameclass =required/>
使用助手:
<%= f.label:name%>
<%= f.text_field:name%>
您的字段将被正确命名(例如 message [name]
),你会在你的控制器中得到 params [:message]
。当然,您可以手动制作,但练习表明这不是最好的决定。
为其他字段重复此步骤。
I am getting the following error when I try to submit a form (the form shows up when 1 user wants to message another user)
NoMethodError in MessagesController#create
undefined method `stringify_keys' for "":String
Rails.root: /Users/fkhalid2008/loand
Application Trace | Framework Trace | Full Trace
app/controllers/messages_controller.rb:31:in `new'
app/controllers/messages_controller.rb:31:in `create'
Here is the relevant code from Messages Controller:
def new
@message = Message.new
if params[:reply_to]
@reply_to = @user.received_messages.find(params[:reply_to])
unless @reply_to.nil?
@message.to = @reply_to.sender.login
@message.subject = "Re: #{@reply_to.subject}"
@message.body = "\n\n*Original message*\n\n #{@reply_to.body}"
end
end
end
def create
@message = Message.new(params[:message])
@message.sender = @user
@message.recipient = User.find_by_login(params[:message][:to])
if @message.save
flash[:notice] = "Message sent"
redirect_to user_messages_path(@user)
else
render :action => :new
end
end
How do I fix this?
More supporting code is below:
MESSAGES>NEW VIEW (This is where Form is being created)
<%= form_for @message, :url => messages_path(:user_id => @user) do |f| %>
<br>
<br />
<br />
<div class="field">
<label>Name</label>
<input type="text" name="name" id="contact_name" class="required" />
</div>
<div class="field">
<label>Email</label>
<input type="text" name="email" id="contact_email" class="required" />
</div>
<div class="field">
<label>Subject</label>
<input type="text" name="subject" id="contact_subject" class="required" />
</div>
<div class="field">
<label>Message</label>
<textarea name="message" rows="8" cols="25" id="contact_message" class="required max_len", max_length=1000 /></textarea>
</div>
<div class="btn">
<%= submit_tag 'Send Message' %>
</div>
<% end %>
MESSAGE MODEL
class Message < ActiveRecord::Base
is_private_message
attr_accessor :to
end
USER MODEL
class User < ActiveRecord::Base
has_many :posts
has_one :profile
has_private_messages
attr_accessible :email
validates_presence_of :email
validates_uniqueness_of :email, :message =>"Hmm, that email's already taken"
validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i, :message => "Hi! Please use a valid email"
end
POSTS>INDEX VIEW (This is where Form is being linked to)
<table class="table table-striped">
<tbody>
<% @posts.each do |post| %>
<tr>
<td>I am a <%= post.title %> getting married in <%= post.job %> in <%= post.location %>, and looking for a <%= post.salary %>. My budget is <%= post.salary %>.</td>
<td> <button class="btn" data-toggle="button" onClick="javascript:location.href = '/messages/new';" />Contact</button></td>
<td><%= time_ago_in_words(post.created_at) %> ago.</td>
<!--/.
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
-->
</tr>
<% end %>
</tbody>
</table>
DEVELOPMENT LOG
Started POST "/messages" for 127.0.0.1 at Sun Apr 22 01:54:57 +0500 2012
Processing by MessagesController#create as HTML
Parameters: {"name"=>"faisal", "commit"=>"Send Message", "authenticity_token"=>"Qyww9hP6jLmqpQ+ua21FAk9vePju8Gpq39Gpaflf3wE=", "utf8"=>"\342\234\223", "subject"=>"testing", "message"=>"hello", "email"=>"fas@fas.com"}
User Load (0.1ms) SELECT "users".* FROM "users" LIMIT 1
Completed 500 Internal Server Error in 17ms
NoMethodError (undefined method `stringify_keys' for "hello":String):
app/controllers/messages_controller.rb:31:in `new'
app/controllers/messages_controller.rb:31:in `create'
Now I see. You've built you form in a completely inconsistent way:
<%= form_for @message, :url => messages_path(:user_id => @user) do |f| %>
and then:
<input type="text" name="name" id="contact_name" class="required" />
what leads you to the situation where this text field has nothing to do with the message. Do you see any mention of message
within the last tag?
Solution is simple: use helpers.
Instead of your markup:
<label>Name</label>
<input type="text" name="name" id="contact_name" class="required" />
use helpers:
<%= f.label :name %>
<%= f.text_field :name %>
And your fields will become properly named (like message[name]
) and you'll get in your controller proper params[:message]
. Of course, you can make it manually but practice shows that it isn't the best decision.
Repeat this step for every of your other fields.
这篇关于Messages Controller中未定义的方法`stringify_keys'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!