如何检索从rails中的link_to发送的id

时间:2014-10-27 11:40:40

标签: ruby-on-rails ruby

我正在尝试通过reveal-modal编辑表单。所以我有多个帖子。

当我点击链接'修改'时,它会显示 div ,并在里面显示一个重新填充信息的表单。但是,我不知道如何将post id传递给它将知道要编辑哪个帖子的表单。

如果我尝试渲染部分表单,我会收到此错误:

  

Post :: ActiveRecord_Relation的未定义方法`model_name':Class

我的所有信息都在名为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

控制器/ 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 个答案:

答案 0 :(得分: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关于渲染集合。

但是,考虑一下你有数百个帖子的情况。然后,对于每个帖子,渲染部分。这样效率非常低,因此请考虑一个异步请求,它只会加载用户请求的一个帖子。