質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

1回答

247閲覧

1:nにおいてedit,destroyコマンドがパス出来ない

KOO_

総合スコア58

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2019/01/26 11:20

現在、プログラミングで1:nのeditとdestroyを実装しております。

その中で、editとdestroyのコマンドがlinkから上手くページ推移しません。

原因は確定的ではありませんが、以下のようにアソシエーションを記述しております。

company

1class Company < ApplicationRecord 2 has_many :invoices 3 has_one :small 4 has_one :carrier 5 belongs_to :user 6 validates :user_id, presence: true 7end

user

1class User < ApplicationRecord 2 has_one :company 3 # Include default devise modules. Others available are: 4 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 5 devise :database_authenticatable, :registerable, 6 :recoverable, :rememberable, :validatable 7 8end

invoice

1class Invoice < ApplicationRecord 2 belongs_to :company 3end

invoicecontroller

1 before_action :load_company 2 before_action :load_invoice, only: [:edit,:update,:show,:destroy, :report] 3 before_action :authenticate_user! 4 5 def edit 6 end 7 8 def update 9 if @invoice.update(invoice_params) 10 redirect_to company_invoices_path(@company, @invoice) 11 else 12 render 'edit' 13 end 14 end 15 16 def destroy 17 @invoice = @company.invoices.find(params[:id]) 18 @invoice.destroy 19 redirect_to company_invoices_path(@company) 20 end 21

invoiceindex

1 <td><%= invoice.sup_company %></td> 2 <td><%= link_to '編集', edit_company_invoice_path(@company, @invoice), class: "command square_btn" %></td> 3 <td><%= link_to '削除', 4 company_invoice_path(@company,@invoice) , 5 method: :delete, 6 class: 'command square_btn', 7 data: { confirm: '本当に削除しますか?'} %></td> 8 <td><%= link_to 'ダウンロード', report_company_invoice_path(@company, invoice, format: 'pdf'), class: 'command square_btn' %></td>

上記のようにプログラミングしており、URLは

http://192.168.33.10:3000/companies/7/invoices/%23%3CInvoice::ActiveRecord_Relation:0x007f8d975af538%3E/edit

となり、

エラー分は

Started GET "/companies/7/invoices/%23%3CInvoice::ActiveRecord_Relation:0x007f8d975af538%3E/edit" for 192.168.33.1 at 2019-01-26 11:00:58 +0000 Cannot render console from 192.168.33.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by InvoicesController#edit as HTML Parameters: {"company_id"=>"7", "id"=>"#<Invoice::ActiveRecord_Relation:0x007f8d975af538>"} Company Load (2.5ms) SELECT "companies".* FROM "companies" WHERE "companies"."id" = ? LIMIT ? [["id", 7], ["LIMIT", 1]] ↳ app/controllers/invoices_controller.rb:8 Invoice Load (0.6ms) SELECT "invoices".* FROM "invoices" WHERE "invoices"."id" = ? LIMIT ? [["id", 0], ["LIMIT", 1]] ↳ app/controllers/invoices_controller.rb:12 Rendering errors/error_404.html.erb within layouts/application Rendered errors/error_404.html.erb within layouts/application (1.9ms) Rendering layouts/_head.html.erb Rendered layouts/_head.html.erb (155.5ms) Rendering layouts/_header.html.erb Rendered layouts/_header.html.erb (0.4ms) User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/views/layouts/application.html.erb:17 Rendering layouts/_footer.html.erb Rendered layouts/_footer.html.erb (0.5ms) Completed 404 Not Found in 200ms (Views: 189.5ms | ActiveRecord: 3.3ms)

となります。

おそらくuser_idが影響しているかと思うのですが、どのように記述すれば解決出来ますでしょうか?

よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

@invoiceがインスタンスではなくコレクションのようです。

invoices#indexアクションで@invoiceが単一オブジェクトであるかを確認してみてください。

投稿2019/02/24 11:52

ykp_yk

総合スコア654

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問