首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我只能编辑我的第一篇文章

我只能编辑我的第一篇文章
EN

Stack Overflow用户
提问于 2017-01-31 01:00:40
回答 1查看 41关注 0票数 1

我目前正面临一个奇怪的问题。我正在尝试将基本操作实现到ajax窗口中。我的问题是,当我试图编辑一个牧场(像一个帖子),它总是呈现我的第一个牧场的编辑。

这是我的密码:

(牧场管理员)

代码语言:javascript
复制
 before_action :set_ranch, only: [:show, :edit, :update, :destroy]

  def index
    @ranches = current_user.ranches
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_ranch
      @ranch = Ranch.find(params[:id])
    end

(牧场/索引)

代码语言:javascript
复制
  <%- @ranches.each do |ranch| %>

    <div class="box panel panel-default">
      <h2><%=link_to ranch.name, ranch%></h2>
      <div class="image_wrapper">
        <%= link_to (image_tag "ranch.jpg"), ranch %>
      </div>
      <button type='button' class='btn btn-default' data-toggle='modal' data-target='#MyEditRanch' aria-hidden="true" remote="true" style="float:right; margin-top: 10px;">
      <i class="fa fa-cog"></i>

      </button>
    </div>  


    <!-- modal Edit -->
    <div id='MyEditRanch' class='modal fade' role='dialog' aria-hidden="true">
      <div class="modal-dialog">
        <div class='content'>
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
      <h3 class="modal-title">Edit <%=ranch.name%></h3>
          </div>
          <div class="modal-body">
            <%= render 'form2', ranch: ranch%>

          </div>
        </div>
      </div>
    </div>
    <!-- /modal Edit -->
  <%end%>

(&我的_form2)

代码语言:javascript
复制
<%= simple_form_for(ranch) do |f| %>
  <%= f.error_notification %>
  <% if ranch.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(ranch.errors.count, "error") %> prohibited this ranch from being saved:</h2>

      <ul>
      <% ranch.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="col-lg-6">
    <div class="input-group" style=" margin-left: 70px;">

      <div class="input" style="width: 400px; margin-top: -10px; text-align: center;" >
        <%= f.input :name, label: false, style:"margin:0px;", placeholder: "Name of your ranch"%>
      </div><!-- /btn-group -->

    </div><!-- /input-group -->

      <div class="form-actions" style="max-width: 300px">
        <%= f.button :submit, style:"text-align:center; margin-left: 70px;" %>
      </div>

      <%= link_to "Delete", ranch_path(ranch), method: :delete, data: {confirm: "Are you sure that you want to delete this ranch ?"}, class: "btn btn-danger", style:"margin-left: 140px;" %>
<% end %>

  </div>

所以如果你有什么建议来解决这件事的话,不客气!!

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-31 02:53:37

在HTML中,In在页面上必须是唯一的。

他们肯定只有一个..。

这意味着只有一个div具有id MyEditRanch.有时候浏览器能让你逃脱.但是如果你有JS使用这个id (例如切换一个模态)..。它仍然会在页面上寻找id的第一个实例来切换.只有他第一次。

如果你想展示多个牧场-每一个都有自己的编辑模式.那么,你真的必须给每个人自己独特的id来切换。

在按钮和编辑模式中将牧场的标识附加在上面。例如

代码语言:javascript
复制
<button type='button' data-toggle='modal' data-target='#edit-ranch-<%= ranch.id %>' ...

<!-- and later -->
<div id='#edit-ranch-<%= ranch.id %>'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41948114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档