まず、私はプログラミングと Ruby on Rails にまったく慣れていないので、修正点を詳しく説明してください。よろしくお願いします。:-)
ブログ システムで新しい投稿を current_user に接続しようとすると、「unknown attribute: user_id」エラーが発生します。
また、ユーザー (投稿を作成したユーザー) を投稿の作成者として投稿の表示ページに表示したいと考えています。現在、名前を手動で入力していますが、「名前」が「current_user」の「ユーザー名」になるように変更するにはどうすればよいですか?
同様のトピックを検索しようとしましたが、しばらく試してみましたが、自分で問題を解決できませんでした。
app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_filter :authenticate_user!
# GET /posts
# GET /posts.json
def index
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
# GET /posts/1
# GET /posts/1.json
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
# GET /posts/new
# GET /posts/new.json
def new
## @post = Post.new
## @post = current_user.posts.new(params[:post])
@user = User.find(current_user)
@post = @user.posts.new(params[:post])
@post.save
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
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
# PUT /posts/1
# PUT /posts/1.json
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
アプリ/モデル/post.rb
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title, :tags_attributes, :username
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
has_many :comments, :dependent => :destroy
has_many :tags
before_create :username
belongs_to :user
accepts_nested_attributes_for :tags, :allow_destroy => :true,
:reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end
デシベル/schema.rb
ActiveRecord::Schema.define(:version => 20120904152633) do
create_table "comments", :force => true do |t|
t.string "commenter"
t.text "body"
t.integer "post_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
create_table "posts", :force => true do |t|
t.integer "user_id"
t.string "title"
t.text "content"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "tags", :force => true do |t|
t.string "name"
t.integer "post_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "tags", ["post_id"], :name => "index_tags_on_post_id"
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "username"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
アプリ/ビュー/投稿/index.html.erb
(...)
<table>
<tr>
<th style="width: 15%">Name</th>
<th style="width: 20%">Title</th>
<th>Content</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.title %></td>
<td><%= post.content %></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 %>
</table>
(...)
アプリ/ビュー/投稿/_form.html.erb
<% @post.tags.build %>
<%= form_for(@post) do |post_form| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= post_form.label :name %><br />
<%= post_form.text_field :name %>
</div>
<div class="field">
<%= post_form.label :title %><br />
<%= post_form.text_field :title %>
</div>
<div class="field">
<%= post_form.label :content %><br />
<%= post_form.text_area :content %>
</div>
<h2>Tags</h2>
<%= render :partial => 'tags/form',
:locals => {:form => post_form} %>
<div class="actions">
<%= post_form.submit %>
</div>
<% end %>
前もって感謝します!
編集:
私の users_helper.rb は空でしたが、他の場所を読んだ後、これを追加しました:
アプリ/ヘルパー/users_helper.rb
module UsersHelper
alias_method :devise_current_user, :current_user
def current_user
if params[:user_id].blank?
devise_current_user
else
User.find(params[:user_id])
end
end
end
そして、今、私はこのエラーを受け取ります:ルーティングエラー - 未定義のメソッドcurrent_user' for module
UsersHelper'
config/routes.rb
Blog::Application.routes.draw do
devise_for :users
resources :users do
resources :posts
resources :comments
end
resources :posts do
resources :comments
end
resources :users
get "home/index"
get '/users/:id', :to => "users#show", :as => :user
get "/:id", :to => "users#show", :as => :user
root :to => 'home#index'
end