2014-10-27 57 views
0

我試圖通過揭示模式來編輯表單。所以我有多個職位。如何檢索通過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">&#215;</a> 
</div> 
+1

更改此行並嘗試一次:'<%= link_to「編輯」,{id:post.id},'data-reveal-id'=>'edit-post'%> – anusha 2014-10-27 11:49:26

+0

我沒有工作這個錯誤:未定義的方法'ID'爲# 2014-10-27 12:14:07

+0

試試這個'<%= link_to「編輯」,{id:@post},'data-reveal-id'=>'edit-post '%>'然後是'<%= render'posts/form2',locals:{post:@post}%>' – anusha 2014-10-27 12:24:22

回答

0

嗯,這根本就不是這樣的。首先,您需要在您的控制器@post = Post.all中以及_form2.html.erb內爲要收集的表單渲染表單。這應該是:

# index.html.erb 
<div id="edit-post" class="reveal-modal" data-reveal> 
    <%= render :partial => 'posts/form2', collection: @posts %> 

    <a class="close-reveal-modal">&#215;</a> 
</div> 

#dashboard controller: 
def index 
    #... 
    @posts = Post.all 
end 

_form2.html.erb: 每@post對象實例必須與form2更換。閱讀更多:http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html關於渲染集合。

但是,考慮一個案例,當您有數百個職位。然後,爲每個帖子渲染一個部分。這會非常低效,所以應該考慮一個異步請求,它只會加載用戶請求的一個帖子。

+0

當我替換時,它給了我這個錯誤每個@post的形式爲form2:未定義的局部變量或方法'form2'爲#<#:0xb5c0ac14> – 2014-10-27 12:16:57

+0

向呈現方法添加'partial':'<%= render:partial =>'帖子/ form2',集合:@posts%>'。我已經更新了我的回覆。 – blelump 2014-10-27 12:26:47

+0

嘿謝謝,它不再給我任何錯誤,但它不呈現形式。該div只是空白。 – 2014-10-27 12:46:05