Use table elements

Sorry th etitle of the topic isn't very clear but it's difficult to
summarize my problem!

I made a view without model but I display in table the content of a
variable named '@operations'. So each line corresponds to an operation.
In my table I have also a column with a button for each line. When i
click on the button I want to redirect to an existing page 'Operation'
witch correspond to the operation of the line of the table like in the
index view with the 'edit' or 'delete' button.

But I can't do the same think because I'm not working with a special
model in this view.

Is someone did somethink like this yet?

Fab

You can, but you may need to do it a different way. The first
question is whether you need to use a form, do you need to post data,
or is each button effectively just a link?

Colin

Colin Law wrote in post #1146780:

The first
question is whether you need to use a form, do you need to post data,
or is each button effectively just a link?

Colin

No this table just allow users to show the differents existing
operations and the if they want to modify it they have to click on the
button.

So my question is:

I have
@operations=[operations_with_may_elements_1,operations_with_may_elements_2...]

but on click on one button I want to affect the right operation in my
second variable:

@operation=@operation[n]

where n correspond to the line of the button and then I can reuse
@operation in my other view because my two view are both using the same
controller.

I don't know if I'm really clear?

Colin Law wrote in post #1146780:

The first
question is whether you need to use a form, do you need to post data,
or is each button effectively just a link?

Colin

No this table just allow users to show the differents existing
operations and the if they want to modify it they have to click on the
button.

So my question is:

I have
@operations=[operations_with_may_elements_1,operations_with_may_elements_2...]

but on click on one button I want to affect the right operation in my
second variable:

@operation=@operation[n]

where n correspond to the line of the button and then I can reuse
@operation in my other view because my two view are both using the same
controller.

I don't know if I'm really clear?

I think so. You can use button_to do invoke the action, and pass the
operation number as a parameter or in the url. If you look at the
docs for button_to and google it to find more examples you will surely
find examples that meet your needs.

Colin

Colin Law wrote in post #1146785:

I think so. You can use button_to do invoke the action, and pass the
operation number as a parameter or in the url. If you look at the
docs for button_to and google it to find more examples you will surely
find examples that meet your needs.

Colin

My matter is not how to pass the operation number to an other view but
is how can I recover this number. Indeed my button is not linked with
one operation...
An exemple of what I want to do is the edit button of Articles index's
view, on click we are redirected to the edit view with only the article
of the line of the edit button clicked.

Just set the action on each button to the url that you want invoked,
so something like edit_article_path( article_ id ) where article_id is
the id of the article on that row.

Get it working just using link_to first, then make the link look like
a button if you want to. Note that since the link is not actually
performing an operation, but is just taking you to a different page,
then you want a link not a form button.

Colin

Colin Law wrote in post #1146799:

Just set the action on each button to the url that you want invoked,
so something like edit_article_path( article_ id ) where article_id is
the id of the article on that row.

Get it working just using link_to first, then make the link look like
a button if you want to. Note that since the link is not actually
performing an operation, but is just taking you to a different page,
then you want a link not a form button.

Colin

You misunderstood me, I can't use this because in this view I haven't a
model a use a varible in which I have all operations of my Operation
model!
So can't use edit_article_path too!!

You said that, for example, the link should take you to the edit page
of an article. How does the user know what article that is?

Colin

Colin Law wrote in post #1146811:

You don't redirect to a view using a link or button, you request an
action in a controller, it is important to use the right words or we
will all be confused. It must be particularly difficult when one is
not working in one's native language, I understand that.

So, when the button is clicked you need to request an action in a
controller, and from that you can then show whatever view you like.
So do you know what action you wish to invoke?

Colin

Fab,
You seem to have put several concepts into one description of the "problem".

Perhaps if you break it into its parts like:

• The link/button initiates a request back to the server
  ( link_to("Text", my_view_path(article), remote: true) )

• The config/routes.rb specifies how a request is directed to a particular controller/action

• The action can use the params from the request to determine what to render or where to redirect.

Does that help you either understand the problem or at least indicate which part you really have a question about?

-Rob

Yes you're both right.

My problem is not to redirect or to render and not to config routes.
My problem is the assignment of the variable @operation, how can I
assign the operation which correspond to the right button?

Operations

That is not important. You cannot assign a value to a variable and
then pass it to an action, you must do it using the url of the link
to. The easiest way is to pass it as a parameter to the url, so the
url will look something like
htttp://localhost:3000/operations/some_action?value=73. You can
easily do this with something like link_to("label",
:controller="operations", :action="some_action", :value=73). Then in
the action in the operations controller the variable params[:value]
will have the value 73.

Note, however, that if in the operations controller all you want to do
is redirect to another action in order to edit an article, for
example, which is what I understood you wanted to do a few posts ago,
then don't go to the operations controller at all, just construct the
correct link so that the button takes you straight to the activities
controller edit action so the edit page is displayed when the button
is clicked.

Colin

<%= @operation.id %>

You use @operations.each, just like any other variable. It doesn't matter whether it's a model or not.

Colin Law wrote in post #1146856:

Colin Law wrote in post #1146856:

id_op2 name_op2 num_op2 button
Be careful operation is not a model in this view but a variable. I don't
know how to assign my variable in fonction of the button line?

That is not important. You cannot assign a value to a variable and
then pass it to an action, you must do it using the url of the link
to. The easiest way is to pass it as a parameter to the url, so the
url will look something like
htttp://localhost:3000/operations/some_action?value=73. You can
easily do this with something like link_to("label",
:controller="operations", :action="some_action", :value=73). Then in
the action in the operations controller the variable params[:value]
will have the value 73.

Yes but my question was how can I have the value equal to the number of
the line of the table where the button is?

If you are dynamically generating the table, then you know what index each row of that table is, don't you? And if you are generating the table using a collection of data, then you know the :id (for example, or the color or stock number or any of the parameters of the underlying object) of each item as you draw the table. Adding that value to the link_to generated code is a trivial exercise.

for foo in @foos
<tr>
  <td>foo.bar</td>
  <td>foo.baz</td>
  <td>link_to foo.boo, foo.blarg, :stock_number => foo.id</td>
</tr>
end

Does that help? (Left off all the erb stuff 'cause it's early.)

Walter

Walter Davis wrote in post #1147109:

htttp://localhost:3000/operations/some_action?value=73. You can
easily do this with something like link_to("label",
:controller="operations", :action="some_action", :value=73). Then in
the action in the operations controller the variable params[:value]
will have the value 73.

Yes but my question was how can I have the value equal to the number of
the line of the table where the button is?

If you are dynamically generating the table, then you know what index
each row of that table is, don't you? And if you are generating the
table using a collection of data, then you know the :id (for example, or
the color or stock number or any of the parameters of the underlying
object) of each item as you draw the table. Adding that value to the
link_to generated code is a trivial exercise.

for foo in @foos
<tr>
  <td>foo.bar</td>
  <td>foo.baz</td>
  <td>link_to foo.boo, foo.blarg, :stock_number => foo.id</td>
</tr>
end

Does that help? (Left off all the erb stuff 'cause it's early.)

Walter

I'm sorry walter but I don't really understand what you said, but the
code to display my table is :

<% @defautrec.each do |n| %> <!--Loop to display-->
  <tr>
    <td><%= n.nomdefaut.nom_defaut%></td>
    <td><%= n.repere%></td>
    <td><%= link_to controller: "pages", action: "ajouter" do%><button,
class="btn btn-mini btn-success">Ajouter</button><%end%></td>
  </tr>

But the in the methode ajouter how can I have something like
@myvariable=n or @myvariable=@defautrec[i] where I is the current line
of the table?

<%= link_to controller: "pages", action: "ajouter", id: n %>

But your code is otherwise so messed up that I can't tell exactly what you're trying to do. What is the "do" at then end of the call to link_to? Is the <%end%> after </button> intended to be associated with it? Or is it supposed to be for the @defautrec.each, but is just in the wrong place?

I suspect you're trying to wrap a button in a link, but that's not how HTML works, regardless of whether you're using Rails or not. Do you just want a button that acts like a link??? It that's the case, you want button_to:

<http://stackoverflow.com/questions/12475299/ruby-on-rails-button-to-link-to>
<http://stackoverflow.com/questions/6247491/rails-3-display-link-as-button>

Anyway, you cannot just stick "do" at the end of a function call in order to start a loop. Remember, <%= %> just executes the Ruby code inside and inserts the result into the HTML, and this is not legitimate Ruby:

link_to controller: "pages", action: "ajouter" do

Or, for more clarity, since this is the way it would be parsed:

link_to (controller: "pages", action: "ajouter" do)

Scott Ribe wrote in post #1147132:

But the in the methode ajouter how can I have something like
@myvariable=n or @myvariable=@defautrec[i] where I is the current line
of the table?

<%= link_to controller: "pages", action: "ajouter", id: n %>

And then how can use the id in my methode?

But your code is otherwise so messed up that I can't tell exactly what
you're trying to do. What is the "do" at then end of the call to
link_to? Is the <%end%> after </button> intended to be associated with
it? Or is it supposed to be for the @defautrec.each, but is just in the
wrong place?

I suspect you're trying to wrap a button in a link, but that's not how
HTML works, regardless of whether you're using Rails or not. Do you just
want a button that acts like a link??? It that's the case, you want
button_to:

<http://stackoverflow.com/questions/12475299/ruby-on-rails-button-to-link-to>
<http://stackoverflow.com/questions/6247491/rails-3-display-link-as-button>

Anyway, you cannot just stick "do" at the end of a function call in
order to start a loop. Remember, <%= %> just executes the Ruby code
inside and inserts the result into the HTML, and this is not legitimate
Ruby:

link_to controller: "pages", action: "ajouter" do

Or, for more clarity, since this is the way it would be parsed:

link_to (controller: "pages", action: "ajouter" do)

I'am sorry I have forgotten some code :
in the controller : @defautrec= Defaut.all

In the view
<% @defautrec.each do |n| %>
  <tr>
     <td><%= n.nomdefaut.nom_defaut%></td>
     <td><%= n.repere%></td>
     <td><%= link_to controller: "pages", action: "ajouter" do%><button,
class="btn btn-mini btn-success">Ajouter</button><%end%>
     </td>
  </tr>
<% end %>

I want a button which call a methode in my controller so I think your
right I should replace link_to by button_to.

Walter Davis wrote in post #1147109:

htttp://localhost:3000/operations/some_action?value=73. You can
easily do this with something like link_to("label",
:controller="operations", :action="some_action", :value=73). Then in
the action in the operations controller the variable params[:value]
will have the value 73.

Yes but my question was how can I have the value equal to the number of
the line of the table where the button is?

If you are dynamically generating the table, then you know what index
each row of that table is, don't you? And if you are generating the
table using a collection of data, then you know the :id (for example, or
the color or stock number or any of the parameters of the underlying
object) of each item as you draw the table. Adding that value to the
link_to generated code is a trivial exercise.

for foo in @foos
<tr>
<td>foo.bar</td>
<td>foo.baz</td>
<td>link_to foo.boo, foo.blarg, :stock_number => foo.id</td>
</tr>
end

Does that help? (Left off all the erb stuff 'cause it's early.)

Walter

I'm sorry walter but I don't really understand what you said, but the
code to display my table is :

<% @defautrec.each do |n| %> <!--Loop to display-->
<tr>
   <td><%= n.nomdefaut.nom_defaut%></td>
   <td><%= n.repere%></td>
   <td><%= link_to controller: "pages", action: "ajouter" do%><button,
class="btn btn-mini btn-success">Ajouter</button><%end%></td>
</tr>

But the in the methode ajouter how can I have something like
@myvariable=n or @myvariable=@defautrec[i] where I is the current line
of the table?

Like this:

<% @defautrec.each_with_index do |n, idx| %> <!--Loop to display-->
...
   <td><%= link_to( controller: "pages", action: "ajouter", myvariable: idx) do%><button,

...

Walter