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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Ruby on Rails 5

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

Q&A

0回答

135閲覧

ログインユーザーが作成したコンテンツの中で1番若いIDを指定したい:vueのrouter.pushで

kojimahideoooo

総合スコア7

Vue.js

Vue.jsは、Webアプリケーションのインターフェースを構築するためのオープンソースJavaScriptフレームワークです。

Ruby on Rails 5

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

0グッド

0クリップ

投稿2019/12/21 03:01

■ ID指定の書き方がわからない

  • ボタンを押した時、指定したページのログインユーザーが作ったものの中で1番IDが若いものを指定したい
  • ”ログインユーザーが作成した、1番若いIDという指定の仕方がわからない”です
  • これをVueの $router_pushの仕組みの中でやりたい

■ 参考にしているVueのコードです。これの
> this.$router.push({ name: 'EmployeeDetailPage', params: { id: e.id } });

の部分をアレンジしたいです。
このコードだと、『作成したコンテンツのID』がリンクになります。

やりたいこととしては

  • 僕が書いているコードだと、employeeの中に"user"のデータを持っています。
  • 「User」 テーブルがあり、「Employee」テーブルと紐づいてます
  • User = hasmany_emplooyee
  • Employee = belongs_to user
  • 今ログインしているユーザーの、EmployeeのデータIDが1番若いもの を指定したいです
  • です。
<script> import axios from 'axios'; export default { data: function () { return { employee: { name: '', department: '', gender: '', birth: '', joined_date: '', payment: '', note: '' }, errors: '' } }, methods: { createEmployee: function() { axios .post('/api/v1/employees', this.employee) .then(response => { let e = response.data; this.$router.push({ name: 'EmployeeDetailPage', params: { id: e.id } }); }) .catch(error => { console.error(error); if (error.response.data && error.response.data.errors) { this.errors = error.response.data.errors; } }); } } } </script>

params:{id:e.id}

の部分を工夫?か、ページを表示する先で工夫するのかな・・・・と思っています。
が、ちょっとイメージついてないです。わかるかた教えていただけるととても嬉しいです????

APIの部分も貼っておきます

EmployeesController

class Api::V1::EmployeesController < ApiController before_action :set_employee, only: [:show] # 拾えなかったExceptionが発生したら500 Internal server errorを応答する rescue_from Exception, with: :render_status_500 # ActiveRecordのレコードが見つからなければ404 not foundを応答する rescue_from ActiveRecord::RecordNotFound, with: :render_status_404 def index employees = Employee.select(:id, :name, :department, :gender) render json: employees end def show render json: @employee end def create employee = Employee.new(employee_params) if employee.save render json: employee, status: :created else render json: { errors: employee.errors.full_messages }, status: :unprocessable_entity end end private def set_employee @employee = Employee.find(params[:id]) end def employee_params params.fetch(:employee, {}).permit(:name, :department, :gender, :birth, :joined_date, :payment, :note).merge({ user: current_user }) end def correct_user employee = Employee.find(params[:id]) if current_user != employee.user redirect_to root_path end end def render_status_404(exception) render json: { errors: [exception] }, status: 404 end def render_status_500(exception) render json: { errors: [exception] }, status: 500 end end

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問