2016-05-13 57 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的所有信息,我是否必須僅限制行動? –