帖子>显示视图中未定义的方法`user'

时间:2012-04-23 07:00:16

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

在帖子中显示此错误>在我尝试加载表单时显示。

表单供用户相互发送消息。感谢任何反馈!

感谢。

NoMethodError in Posts#show

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

undefined method `user' for #<Post:0x12e2ae930>
Extracted source (around line #1):

1: <%= form_remote_tag (:update => 'message', :url => {:controller => 'main', :action => 'send_message', :user_id => @post.user.id}) do %>
2: <br>
3: <br />
4: <br />

POSTS&gt;显示视图

<%= form_remote_tag (:update => 'message', :url => {:controller => 'main', :action => 'send_message', :user_id => @post.user.id}) do %>
<br>
<br />
<br />
<div class="field">
Hello! My name is <%= f.text_field :subject %> and I'm contacting you in response to your ad. I'm interested in learning more so get in touch! Here's my contact details: <%= f.text_field :body %>.
</div>
<button type="submit" class="btn span6 large">Submit</button>
<% end %>

POSTS CONTROLLER

def new
@post = Post.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render :json => @post }
end
end

def edit
@post = Post.find(params[:id])
end

def create
    @post = Post.new(params[:post])

    respond_to do |format|
        if verify_recaptcha && @post.save
            format.html { redirect_to :action=> "index"}
            format.json { render :json => @post, :status => :created, :location => @post }
        else
            format.html { render :action => "new" }
            format.json { render :json => @post.errors, :status => :unprocessable_entity }
        end
    end
end

主要控制器

class MainController < ApplicationController

def send_message
message = Message.new
message.subject = params[:subject]
message.body = params[:message]
message.sender = User.find session[:user]
message.recipient = User.find params[:user_id]
if message.save
  ContactMailer.deliver_message_email message.recipient.email, message.id, request.host
  return redirect_to "/posts"
else
  render :text => "Hmm. Something seems to be wrong...let me look into it"
end

联系MAILER

类ContactMailer&lt;的ActionMailer ::基

def message_email (recipient, id, host)
recipients   [ recipient ]
subject      "Weddoo: You have received a new message"
from         "admin@weddoo.com"
bcc          ["admin@weddoo.com"]
content_type "text/html"
body         :message_id => id, :host => host
sent_on Time.now
end

end
end

的routes.rb

Mysalary::Application.routes.draw do

resources :users do
 resources :messages
   end

resources :profiles
resources :pages
resources :posts

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

root :to => 'posts#new'

end

POST MODEL

class Post < ActiveRecord::Base
attr_accessible :title, :job, :location, :salary

validates :title, :job, :location, :salary, :presence => true 
validates :salary, :numericality => {:greater_than_or_equal_to => 1} 

default_scope :order => 'posts.created_at DESC'
end

2 个答案:

答案 0 :(得分:2)

错误是user模型上没有方法Post。它直接指向第一行。

<%= form_remote_tag (:update => 'message', :url => {:controller => 'main', :action => 'send_message', :user_id => @post.user.id}) do %>

所以我要说

@post.user.id是您的问题陈述,该模型没有方法user但是如果没有看到Post模型,很难确定。

答案 1 :(得分:1)

我认为你错过了加入关系。

class Post < ActiveRecord::Base
  belongs_to :user
end