2012-04-19 42 views
0

我试图将链接添加到我的帖子>索引页,它可以让人们直接发送消息(使用简单的私人信息),以邮政的创造者。未定义的方法“网友留言道” 3项目

目前我有一个链接到/ messages/new来做到这一点,但这不起作用 - 当我尝试访问/消息/新时,请在我的应用程序中收到以下错误:

我想将链接替换为/ messages/new(在POSTS> INDEX文件中)下面有一些更好的东西。请告诉我,如果您有任何想法!!

**NoMethodError in Messages#new** 

Showing /Users/fkhalid2008/loand/app/views/messages/new.html.erb where line #1 raised: 

undefined method `user_messages_path' for #<#<Class:0x12a77f198>:0x12a76dce0> 

Extracted source (around line #1): 

1: <% form_for @message, :url => user_messages_path(@user) do |f| %> 
2: <p> 
3:  To:<br /> 
4:  <%= f.text_field :to %> 

其他信息:应用程序本身就像Gumtree.com,用户可以通过它来创建帖子(例如,销售我的车),并且人们回复我的消息(通过Simple Pvt Messages插件)。

谢谢!

费萨尔

信息>新VIEW

<% form_for @message, :url => user_messages_path(@user) do |f| %> 
<p> 
To:<br /> 
    <%= f.text_field :to %> 
    <%= error_message_on @message, :to %> 
</p> 
<p> 
Subject:<br /> 
<%= f.text_field :subject %> 
<%= error_message_on @message, :subject %> 
</p> 
<p> 
    Message<br /> 
    <%= f.text_area :body %> 
     <%= error_message_on @message, :body %> 
</p> 
<p> 
    <%= submit_tag "Send" %> 
</p> 
<% end %> 

消息模型

class Message < ActiveRecord::Base 

is_private_message 

attr_accessor :to 

end 

的routes.rb

Mysalary::Application.routes.draw do 

resources :messages do 
    collection do 
    post :delete_selected 
    end 
    end 

resources :users 
resources :profiles 
resources :pages 
resources :posts 

get "pages/home" 
get "pages/about" 
get "pages/legal" 
get "pages/feedback" 

root :to => 'posts#new' 

end 

留言控制器

class MessagesController < ApplicationController 

before_filter :set_user 

def index 
if params[:mailbox] == "sent" 
    @messages = @user.sent_messages 
else 
    @messages = @user.received_messages 
end 
end 

def show 
@message = Message.read_message(params[:id], current_user) 
end 

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 

def delete_selected 
if request.post? 
    if params[:delete] 
    params[:delete].each { |id| 
     @message = Message.find(:first, :conditions => ["messages.id = ? AND (sender_id = ? OR recipient_id = ?)", id, @user, @user]) 
     @message.mark_deleted(@user) unless @message.nil? 
    } 
    flash[:notice] = "Messages deleted" 
    end 
    redirect_to :back 
end 
end 

private 
def set_user 
@user = User.first 
end 
end 

帖子>索引视图

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

回答

1

你的routes.rb没有定义路径user_messages_path,它很简单:)

如果您想要嵌套的宁静路线,你需要像

resources :users do 
    resources :messages 
end 
+0

有没有什么办法来解决它比嵌套等航线..? – hikmatyar 2012-04-19 10:41:50

+0

我曾经有嵌套的路线,但它导致了很多其他的错误,这就是为什么我改变它... – hikmatyar 2012-04-19 10:42:08

+0

我添加文章>索引视图...这就是/ messages/new链接是...我想 – hikmatyar 2012-04-19 10:46:20

0

只需使用

<% form_for @message do |f| %> 
<% end %> 
+0

的问题尝试过,得到这个错误未定义的方法'error_message_on'为#<#:0x12a36dff0>。 – hikmatyar 2012-04-19 11:10:24

+0

如果我这样做,那么我怎么知道该消息将去正确的用户...我不认为这样会 – hikmatyar 2012-04-19 11:11:36

相关问题