N
N
Nazar Jeveling2014-08-01 18:48:28
JavaScript
Nazar Jeveling, 2014-08-01 18:48:28

How to pass a parameter in Ruby on Rails?

I have the main TODO page of the application, on this page there are several lists with tasks (Projects). At the bottom of each sheet there is a form for adding a new task, I need to pick up the project_id when adding a new task (exactly the sheet in which I clicked the button), and transfer it to the controller that creates a new task for this sheet.

UPD1
I understand what can be done with jQuery , but I don't know how to implement it in a Rails application.

UPD2
I'm new to Ruby on Rails so don't kick too hard!

UPD3
here is my todo_controller

class TodoController < ApplicationController
  def index
  	@projects = Project.order(:id)
  end

  def task_new
  	@task = Task.new
  end

  def task_create
  	@project = Project.first
  	@task = @project.tasks.create(name: params[:name], status: 'on')

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


Now new ones are added only to the first project

Here is my app/views/todo/index.html.erb
<% @projects.each do |project| %>
  <div class='project'>
    <div class='project_title'>
      <%= image_tag('SVG/calendar.svg') %>
      <p><%= project.name %></p>
    </div>
    <div class='task_list'>
      <% project.tasks.each do |task| %>
        <div class='task'>
          <div class='task_status'>
          	<%= task.status %>
          </div>
          <div class='task_name'>
          	<%= task.name %>
          </div>
        </div>
      <% end %>        
    </div>
    <div class='task_form'>
  	  <%= form_tag do%>
   	    <div class='textfield'>
      	  <%= label_tag '' %>
      	  <%= text_field_tag :name, params[:name], :placeholder => 'Enter new task here...' %>
      	</div>
      	<div class='task_add_btn'>
      	  <%= submit_tag 'Add', remote: true %>
      	</div>
      <% end %>
    </div>
  </div>
<% end %>


If so here is a link to github

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2014-08-01
@xo8bit

How do you expect to be answered without asking a specific question? A bit of code for clarity would be nice.
Direct answer to your specific current question: there are many ways. For example, you can pass a get parameter with a request (domain.com/controller/action?para1=value¶m2=value. You can use the rails routing system. By designating a route as resoursec :todos, for example, you have the get 'todos/ route, among others: id' => 'todos#show', so if you pass a url like " domain.com/todos/5 ", then the parameter id = 5 will come (params[:id] # 5)
If you create forms using form_for and there is the Task entity, it should automatically understand which form action to use.If not, then you need to manually pass the value of the sheet in the form action
In general, the code to the studio and try to ask questions more specifically

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question