我試圖通過揭示模式來編輯表單。所以我有多個職位。如何檢索通過rails中的link_to發送的ID
當我點擊鏈接「編輯」就會露出一個DIV和顯示形式的內部與該信息重新填充。但是,我不知道如何將帖子ID傳遞到表單中,它將知道要編輯哪個帖子。
如果我嘗試呈現局部形式我得到這個錯誤:
undefined method `model_name' for Post::ActiveRecord_Relation:Class
我所有的資料都被稱爲儀表板
控制器/ 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
個
控制器/ 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
帖/ _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「編輯」,{id:post.id},'data-reveal-id'=>'edit-post'%> – anusha 2014-10-27 11:49:26
我沒有工作這個錯誤:未定義的方法'ID'爲# –
2014-10-27 12:14:07
試試這個'<%= link_to「編輯」,{id:@post},'data-reveal-id'=>'edit-post '%>'然後是'<%= render'posts/form2',locals:{post:@post}%>' – anusha 2014-10-27 12:24:22