【发布时间】:2014-10-27 11:40:40
【问题描述】:
我正在尝试通过显示模式编辑表单。所以我有多个帖子。
当我点击链接 'Edit' 时,它会显示一个 div 并在其中显示一个重新填充信息的表单。但是,我不知道如何将帖子 ID 传递到它将知道要编辑哪个帖子的表单中。
如果我尝试渲染部分表单,我会收到此错误:
Post::ActiveRecord_Relation:Class 的未定义方法 `model_name'
我的所有信息都在一个名为 Dashboard 的控制器视图中
控制器/dashboards_controller.rb
class DashboardsController < ApplicationController
def index
@profile = Profile.find(current_user)
@profileAll = Profile.all
@post = Post.all
end
def show
end
def edit
@profile =Profile.find(current_user)
@post = @profile.post.find(params[:id])
end
end
controllers/posts_controller.rb
class PostsController < ApplicationController
def create
@profile = Profile.find(current_user)
@post = @profile.post.create(post_params)
redirect_to dashboards_path(current_user)
end
def destroy
@post =Profile.find(params[:profile_id])
@post = profile.post.find(params[:id])
@post.destroy
redirect_to dashboards_path(current_user)
end
def show
@profile =Profile.find(current_user)
@post = @profile.post.find(params[:id])
end
def edit
@profile =Profile.find(current_user)
@post = @profile.post.find(params[:id])
end
def update
@post = profile.post.find(params[:id])
if @post.update(post_params)
redirect_to dashboards_path(current_user)
else
render 'edit'
end
end
private
def post_params
params.require(:post).permit(:title, :content)
end
end
posts/_form2.html.erb
<%= form_for @post do |f| %>
<% 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 %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :content %><br>
<%= f.text_area :content %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
routes.rb
Rails.application.routes.draw do
get 'homepages/index'
resources 'dashboards' do
resources 'posts'
end
#get 'users/sign_in'
resources 'profiles' do
resources 'posts'
end
devise_for :users, :controller => {:registrations => "users/registrations"}
devise_scope :user do
root :to => 'devise/sessions#new'
end
end
仪表板/index.html.erb
<% @post.each do |post| %>
<div class="panel">
<%= @profileAll.find(post.profile_id).name %>
<hr>
<h5><%= post.title %></h5>
<p><%= post.content %></p>
<%# link_to "Edit", edit_dashboard_post_path(current_user,[post.profile, post]) %>
<%= link_to "Edit", {id: @post},'data-reveal-id' => 'edit-post' %>
</div>
<% end %>
<div id="edit-post" class="reveal-modal" data-reveal>
<%= render 'posts/form2' %>
<a class="close-reveal-modal">×</a>
</div>
【问题讨论】:
-
更改此行并尝试一次:
<%= link_to "Edit", {id: post.id},'data-reveal-id' => 'edit-post' %> -
没有工作我得到这个错误:#<:activerecord_relation:0xb5e06a7c> 的未定义方法 `id'
-
试试这个
<%= link_to "Edit", {id: @post},'data-reveal-id' => 'edit-post' %>然后` ` -
感谢 anusha 的帮助,但它现在在我的表单上告诉我这个错误:未定义的方法 `model_name' for Post::ActiveRecord_Relation:Class
-
实际上错误是因为您正在使用此
@post = @profile.post.find(params[:id])检索帖子,但 id 为空,因此请尝试一次remove the above line in dashboard edit method and add these <%= link_to "Edit", edit_dashboard_post_path(current_user,[post.profile, post]),'data-reveal-id' => 'edit-post' %> and then`
标签: ruby-on-rails ruby