有关 Rails link_to 和 post 方法的帮助

发布于 2024-11-04 02:50:01 字数 868 浏览 1 评论 0原文

我需要帮助将学生分配到批次..他们是多对多的关系。

        <tbody>
            <% Batch.all.each do |b|%>
            <tr>
                <td><%= b.course.name%></td>
                <td><%= b.name %></td>
                <td><%= b.section_name%></td>
                <td><%= link_to "Add", student_batch_students_path(@student, :batch_id=> b.id), :method=> :post%></td>
            </tr>
            <%end%>


        </tbody>

在我的控制器中

def create
    @batch_student = BatchStudent.new(params[:batch_student])
    @batch_student.save    
  end

我的路线

  resources :students do
    resources :batch_students
  end

resources :batches

但是在我的数据库中它使用student_id和batch_id作为null创建它

I need help assigning students to batches.. they are in a many to many relation.

        <tbody>
            <% Batch.all.each do |b|%>
            <tr>
                <td><%= b.course.name%></td>
                <td><%= b.name %></td>
                <td><%= b.section_name%></td>
                <td><%= link_to "Add", student_batch_students_path(@student, :batch_id=> b.id), :method=> :post%></td>
            </tr>
            <%end%>


        </tbody>

In my controller

def create
    @batch_student = BatchStudent.new(params[:batch_student])
    @batch_student.save    
  end

My routes

  resources :students do
    resources :batch_students
  end

resources :batches

But on my database it creates it with student_id and batch_id as null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请恋爱 2024-11-11 02:50:01

您正在更新现有批次,但不是创建,因此您应该向 update 操作发出 PUT 请求

<td><%= link_to "Add", student_batch_students_path(@student, :batch_id => b.id), :method=> :post %></td>


def create
  @student = Student.find(params[:id])
  @batch   = Batch.find(params[:batch_id])
  @batch_student = BatchStudent.new(:params[:batch_student])
  @batch_student.student = @student
  @batch_student.batch = @batch
  @batch_student.save
end

You are updating exist batch, but not creating, so you should make PUT request to update action

<td><%= link_to "Add", student_batch_students_path(@student, :batch_id => b.id), :method=> :post %></td>


def create
  @student = Student.find(params[:id])
  @batch   = Batch.find(params[:batch_id])
  @batch_student = BatchStudent.new(:params[:batch_student])
  @batch_student.student = @student
  @batch_student.batch = @batch
  @batch_student.save
end
香草可樂 2024-11-11 02:50:01

params 哈希不包含 :batch_student 哈希,因为您不是从表单提交。参数应该类似于 {"student_id" =>; 1、“batch_id”=> 1、“方法”=> “发布”}

因此,修改您的创建操作如下:

def create
  @batch_student = BatchStudent.new(params)
  @batch_student.save    
end

# or, a shorter version
def create
  @batch_student = BatchStudent.create(params)
end 

使用 new 的优点是您可以执行 if @batch_student.save 来检查错误。

我希望这有帮助。

The params hash doesn't contain a :batch_student hash because you are not submitting from a form. The params has should look something like {"student_id" => 1, "batch_id" => 1, "method" => "post"}.

So, modify your create action as follows:

def create
  @batch_student = BatchStudent.new(params)
  @batch_student.save    
end

# or, a shorter version
def create
  @batch_student = BatchStudent.create(params)
end 

The advantage of using new is you can do a if @batch_student.save to check for errors.

I hope this helps.

魔法少女 2024-11-11 02:50:01

参数和http方法应该在一起 {:batch_id=> b.id, :method=> :帖子}

<%= link_to "Add", student_batch_students_path(@student), {:batch_id=> b.id, :method=> :post} %>

The parameters and the http method should be together {:batch_id=> b.id, :method=> :post}

<%= link_to "Add", student_batch_students_path(@student), {:batch_id=> b.id, :method=> :post} %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文