【问题标题】:How to retrieve id sent from a link_to in rails如何检索从rails中的link_to发送的id
【发布时间】: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">&#215;</a>
 </div>

【问题讨论】:

  • 更改此行并尝试一次:&lt;%= link_to "Edit", {id: post.id},'data-reveal-id' =&gt; 'edit-post' %&gt;
  • 没有工作我得到这个错误:#<:activerecord_relation:0xb5e06a7c> 的未定义方法 `id'
  • 试试这个&lt;%= link_to "Edit", {id: @post},'data-reveal-id' =&gt; 'edit-post' %&gt; 然后` `
  • 感谢 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 &lt;%= link_to "Edit", edit_dashboard_post_path(current_user,[post.profile, post]),'data-reveal-id' =&gt; 'edit-post' %&gt; and then `

标签: ruby-on-rails ruby


【解决方案1】:

嗯,它根本不是那样工作的。首先,您在控制器@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 关于渲染集合。

但是,考虑一个有数百个帖子的情况。然后,对于每个帖子,都会呈现一个部分内容。那将是非常低效的,因此请考虑一个异步请求,该请求将仅加载用户请求的一篇帖子。

【讨论】:

  • 当我将表单中的每个 @post 替换为 form2 时,它给了我这个错误: undefined local variable or method `form2' for #:0xb5c0ac14>
  • 在渲染方法中添加partial&lt;%= render :partial =&gt; 'posts/form2', collection: @posts %&gt;。我已经更新了我的回复。
  • 嘿,谢谢,它不再给我任何错误,但它没有呈现表单。 div 是空白的。
  • 你确定你在数据库中有一些帖子吗?
  • 是的,我确实有一些帖子。我可以在没有表格的情况下输出它们。最让我担心的是它甚至根本不显示表单元素。它只是一个没有任何内容的 div。没有输入元素或标签元素。再次感谢您的支持。
猜你喜欢
  • 1970-01-01
  • 2023-03-21
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多