消息控制器中未定义的方法`stringify_keys'

时间:2012-04-21 20:10:10

标签: ruby-on-rails ruby-on-rails-3 forms nomethoderror

当我尝试提交表单时出现以下错误(当1个用户想要向其他用户发送消息时,表单会显示)

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'

以下是来自消息控制器的相关代码:

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

我该如何解决这个问题?

更多支持代码如下:

消息>新视图(这是创建表格的地方)

<%= 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 %>

消息模型

class Message < ActiveRecord::Base

is_private_message

attr_accessor :to

end

用户模型

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&gt; INDEX VIEW(这是表格链接的地方)

<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>

开发日志

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'

3 个答案:

答案 0 :(得分:6)

现在我明白了。你以一种完全不一致的方式建立了你的形式:

<%= form_for @message, :url => messages_path(:user_id => @user) do |f| %>

然后:

<input type="text" name="name" id="contact_name" class="required" />

是什么导致您遇到此文本字段与邮件无关的情况。你看到最后一个标签中有message的提及吗?

解决方案很简单:使用助手。

而不是你的标记:

<label>Name</label>
<input type="text" name="name" id="contact_name" class="required" />

使用助手:

<%= f.label :name %>
<%= f.text_field :name %>

您的字段将被正确命名(如message[name]),您将进入正确的控制器params[:message]。当然,您可以手动制作,但练习表明这不是最好的决定。

对每个其他字段重复此步骤。

答案 1 :(得分:2)

我猜想发生的事情是params[:message]不是Rails所期望的哈希。你绝对应该遵循jdoe的建议并发布你的表格代码b / c我会怀疑它是不对的。

此外,您还可以在开发日志中查看您在应用中发布的数据。发布那个。它看起来像是:

Started POST "/message" for 127.0.0.1 at 2012-04-19 16:49:22 -0400
  Processing by MessagesController#create as HTML
  Parameters: {"key"=>"value"}

“参数”是您想要查看的内容。

答案 2 :(得分:1)

当您致电Message.new(params[:message])时,params[:message]需要是包含消息属性的哈希。在您的表单中,因此在日志文件中显示的帖子中,params[:message]是一个字符串。

更改

<textarea name="message" rows="8" cols="25" id="contact_message" class="required max_len", max_length=1000 /></textarea>

<textarea name="message[message]" rows="8" cols="25" id="contact_message" class="required max_len", max_length=1000 /></textarea>

或更好

<%= f.text_area :message, :rows => 9, :cols => 25, :class => "required max_len", :max_length => 1000 %>

但是 - 您的表单中会有很多其他错误很快就会遇到 - 例如,没有message[to]参数,但您希望控制器中存在一个错误。我建议阅读一些Rails教程,例如this one