缺失:路由定义Rails上的操作键

时间:2017-11-25 02:56:31

标签: ruby-on-rails

我收到以下错误:

Missing :action key on routes definition, please check your routes.

对于这条路线

resources :groups do
  post '/groups/:id/add', on: :member
end

我已经看过其他几个SO答案,但我找不到能帮到我的答案。缺少action的内容?

2 个答案:

答案 0 :(得分:0)

这不是定义路线的正确方法。实际上,路由器的成员/集合接受一个键值对来生成路由。在定义member函数时,它将像:

resources :groups do
  member do
    post :add
  end
end

它会生成如下路线:groups/1/add

如果您使用collection,则会生成:/groups/add

resources :groups do
  collection do
    post :add
  end
end

希望你明白这一点。

您可以为此特定操作定义特定路线,如下所示:

match "/groups/:id/add" => "groups#add", via: :post

答案 1 :(得分:0)

resources :groups do post '/groups/:id/add', on: :member, action: "add" end 在这里表示控制器操作。如在

resources :groups do
  post :add, on: :member
end

Rails无法推断路径中的操作。但是你可以改为定义动作,rails会自动找出路径:

'use strict';
import React, { Component } from 'react';
import {
  AppRegistry,
  Dimensions,
  StyleSheet,
  Text,
  TouchableHighlight,
  View
} from 'react-native';
import Camera from 'react-native-camera';

class camera_app extends Component {

  constructor(props) {
    super(props);
    this.state = {
      showCamera: true,
    };
  }

  renderCamera = () => {
    if(this.state.showCamera) {
      return (
        <Camera
          ref={(cam) => {
            this.camera = cam;
          }}
          style={styles.container}
          aspect={Camera.constants.Aspect.fill}
          onBarCodeRead={this._onBarCodeRead}>
        </Camera>
      );
    } else {
      return (
        <View></View>
      );
    }
  }

  render() {
    return (
      this.renderCamera()
    );
  }

  _onBarCodeRead = (e) => {
    this.setState({showCamera: false});
    alert("Barcode Found!",
          "Type: " + e.type + "\nData: " + e.data);
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
    backgroundColor: "transparent",
  },
});

AppRegistry.registerComponent('rn_camera', () => camera_app);