2016-05-13 81 views
2

我是RoR的新手,我尝试使用后&用户构建经典的网络应用程序。 有一个模型&控制器(在线),允许用户把他的帖子放在公共墙上,并带有此操作的新信息。 我目前正在尝试通过修改模型Onlines来修改与此操作(Onlines)相关联的嵌套窗体。 但我无法访问我的控制器的这个动作,我不明白为什么?从其他控制器访问我的动作编辑

我的代码::

人在线控制器:

class OnlinesController < ApplicationController 
 
    before_action :set_online 
 

 
    def edit 
 
    end 
 

 
    private 
 

 
    def set_online 
 
    @post = Post.find(params[:post_id]) 
 
    @online = Online.find_by(params[:id]) 
 
    end 
 

 
end

柱控制器:

class PostsController < ApplicationController 
 
    before_action :set_online 
 

 

 
    def show 
 
    @online.post_id = @post.id 
 
    end 
 

 
    private 
 

 
    def set_online 
 
    @onlines = Online.find_by(id: params[:id]) 
 
    end 
 

 
end

查看/职位/显示:`

<div class="btn-group" role="group" aria-label="..."> 
 
    <%= link_to '- taked - ', edit_online_path(@online), data: { confirm: 'Confirmer la mise en ligne de #{@title}?' }, class: "btn btn-primary " %> 
 
</div>

查看/人在线/编辑:

<%= simple_form_for([@post, @onlines]) do |f| %> 
 
    <div class="row"> 
 
      <div class="col-md-12"> 
 
       <div id="Order"> 
 
       <%= f.simple_fields_for :orders do |order| %> 
 
       <%= render 'orders_fields', f: order %> 
 
       <%end%> 
 
       <div class="Order_links"> 
 
        <%= link_to_add_association 'Ajouter une part', f, :orders, class: "btn btn-default" %> 
 
       </div> 
 
       </div> 
 
      </div> 
 
      </div> 
 

 
<div class="form-group text-center"> 
 
<%= f.submit "Pusher", class: 'btn btn-success' %> 
 
</div> 
 

 
<% end %>

路线:

Rails.application.routes.draw do 
 
    get 'profiles/show' 
 

 
    mount RailsAdmin::Engine => '/admin', as: 'rails_admin' 
 
    
 
    devise_for :users, :controllers => { registrations: 'registrations' } 
 

 
    resources :posts do 
 
resources :comments 
 
resources :onlines 
 
end 
 

 
    get ':pseudo', to: 'profiles#show', as: :profile 
 
    get ':pseudo/edit', to: 'profiles#edit', as: :edit_profile 
 
    patch ':pseudo/edit', to: 'profiles#update', as: :update_profile 
 
    get ':post_id/online/new', to: 'online#new', as: :new_online 
 
    post ':post_id/online/:id/edit', to: 'onlines#edit', as: :edit_online 
 

 

 
    root 'posts#index'

所以,如果你能引导我成功的这一行动也将是美好的,谢谢!

回答

1

首先,请务必以单数形式(在您的案例中为Online)引用您的模型,因为这正是Rails期望的和控制器的复数形式,正如您所说的那样。注意你的“before_action:set_online”语句,因为它使用'find'方法来定义@post,并且如果param没有被传入,将会导致异常!此外,您的路线只显示一个获取请求,让您访问您的edit_online页面。然后,您需要一条链接到“更新”操作的“发布”路线,在用户提交表单后将数据提交给您的应用程序!提供其余的路线,我会深入了解一下,但也会澄清你的问题。如果您只是试图从一个控制器中的一个动作转到另一个控制器中的另一个动作,那么您正在使用“redirect_to”语句。

+0

感谢您的及时回答 我编辑我的问题,向您展示我所有的路线 我不明白你告诉我关于set_online的所有信息,我是否必须仅限制行动? –

相关问题