Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
663 views
in Technique[技术] by (71.8m points)

ruby-on-rails - 行未添加到表,但未给出错误(Row not added to table, but no error given)

I'm new to Rails.

(我是Rails的新手。)

I have a table called TeamMemberships that has 2 foreign keys: one to Users and one to Teams .

(我有一个名为TeamMemberships的表,该表具有2个外键:一个给Users ,一个给Teams 。)

I want to add a TeamMembership element to the TeamMemberships table from the show.html.erb page for Teams such that the Team foreign key is the Team that called the action and the User will be determined by a form.

(我想从show.html.erb页面将TeamTeamMembership元素添加到TeamsTeamMemberships表中,以使Team外键是调用该操作的Team ,而User将由表单确定。)

When I try to add the TeamMembership , the page redirects as expected, however, no table element is added and no error message is given.

(当我尝试添加TeamMembership ,页面将按预期方式重定向,但是,未添加任何表元素,并且未给出错误消息。)

What could be causing this?

(是什么原因造成的?)

Here are my routes:

(这是我的路线:)

Rails.application.routes.draw do
  resources :instructors
  get 'static_pages/home'
  get 'static_pages/adminLanding'

  resources :audiences
  resources :comments
  resources :scores
  resources :projects
  resources :team_memberships
  resources :team_types
  resources :teams
  resources :users
  Rails.application.routes.draw do
    resources :teams do
      resources :team_memberships
    end
  end
  root 'static_pages#home'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

Here is the link from the teams/show.html.erb page:

(这是来自teams/show.html.erb页面的链接:)

<%= link_to 'Add a student', new_team_team_membership_path(@team.id) %>

Here is the new method in the TeamMemberships controller:

(这是TeamMemberships控制器中的new方法:)

  def new
    @team_membership = TeamMembership.new(team_id: params[:team_id])
    @users = User.all

  end

Here is the team_memberships/new.html.erb file for the form:

(这是表单的team_memberships/new.html.erb文件:)

<h1>New Team Membership</h1>

<%= form_for @team_membership do |f| %>
  <%= f.select :user_id, options_for_select(@users.collect{ |student| [student.first_name + ' ' +student.last_name, student.id]}) %>
    <%= f.submit %>
  <% end %>

<%= link_to 'Back', team_memberships_path %>

Here is the create method of the TeamMemberships controller:

(这是TeamMemberships控制器的create方法:)

  def create
    @users = User.all
    @team_membership = TeamMembership.new(team_membership_params)

    respond_to do |format|
      if @team_membership.save
        format.html { redirect_to @team_membership, notice: 'Team membership was successfully created.' }
        format.json { render :show, status: :created, location: @team_membership }
      else
        format.html { render :new }
        format.json { render json: @team_membership.errors, status: :unprocessable_entity }
      end
    end
  end

Thanks!

(谢谢!)

  ask by astronautical_22 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are not displaying errors in the view, add this to your new.html.erb and style it later according to your needs

(您没有在视图中显示错误,请将其添加到new.html.erb ,然后根据需要设置样式)

<% if @team_membership.errors.any? %>
  <ul>
    <% @team_membership.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
  </ul>
<% end %>

Try it once!

(尝试一次!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...