Use table elements

76 views
Skip to first unread message

Fab Forestier

unread,
May 22, 2014, 8:32:14 AM5/22/14
to rubyonra...@googlegroups.com
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

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
May 22, 2014, 8:36:24 AM5/22/14
to rubyonra...@googlegroups.com
On 22 May 2014 13:32, Fab Forestier <li...@ruby-forum.com> wrote:
> 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.

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

Fab Forestier

unread,
May 22, 2014, 9:09:36 AM5/22/14
to rubyonra...@googlegroups.com
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

unread,
May 22, 2014, 9:22:36 AM5/22/14
to rubyonra...@googlegroups.com
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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/055a783a0ac7abd440b5ab8f3557ec7c%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

Fab Forestier

unread,
May 22, 2014, 9:47:41 AM5/22/14
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
May 22, 2014, 10:37:02 AM5/22/14
to rubyonra...@googlegroups.com
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

Fab Forestier

unread,
May 22, 2014, 11:00:28 AM5/22/14
to rubyonra...@googlegroups.com
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!!

Colin Law

unread,
May 22, 2014, 11:25:33 AM5/22/14
to rubyonra...@googlegroups.com
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

Fab Forestier

unread,
May 22, 2014, 1:17:11 PM5/22/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1146811:
No by exemple I understand the action of the button but it's a totally
different think my button has to redirect to an Operation view but it is
not an edit view it's a view created by me, and this operation has to be
the operation in the same line in the table with the button.

It's difficult for me to explain in english I'am sorry !

Colin Law

unread,
May 22, 2014, 3:39:50 PM5/22/14
to rubyonra...@googlegroups.com
On 22 May 2014 18:17, Fab Forestier <li...@ruby-forum.com> wrote:
> Colin Law wrote in post #1146811:
>> On 22 May 2014 16:00, Fab Forestier <li...@ruby-forum.com> wrote:
>>
>> 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
>
> No by exemple I understand the action of the button but it's a totally
> different think my button has to redirect to an Operation view but it is
> not an edit view it's a view created by me, and this operation has to be
> the operation in the same line in the table with the button.

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

Rob Biedenharn

unread,
May 22, 2014, 4:13:39 PM5/22/14
to rubyonra...@googlegroups.com
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

Fab Forestier

unread,
May 23, 2014, 2:41:28 AM5/23/14
to rubyonra...@googlegroups.com
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
------------------------------------------------------------------
id_op1 name_op1 num_op1 button
------------------------------------------------------------------
id_op2 name_op2 num_op2 button
------------------------------------------------------------------
id_op3 name_op3 num_op3 button
------------------------------------------------------------------

To display this table I use a loop to display each element of each
operation contained in the variable '@operations'.
The when I click on the first button I wish to assign the first
operation in the variable '@operation' annd then call a method in the
controller but it's not my problem.

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?

Colin Law

unread,
May 23, 2014, 3:31:18 AM5/23/14
to rubyonra...@googlegroups.com
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

Scott Ribe

unread,
May 23, 2014, 8:25:30 AM5/23/14
to rubyonra...@googlegroups.com
On May 23, 2014, at 12:41 AM, Fab Forestier <li...@ruby-forum.com> wrote:

> 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?

<%= @operation.id %>

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

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Fab Forestier

unread,
May 26, 2014, 4:29:08 AM5/26/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1146856:
> On 23 May 2014 07:41, Fab Forestier <li...@ruby-forum.com> wrote:
>> 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?

Walter Lee Davis

unread,
May 26, 2014, 8:30:35 AM5/26/14
to rubyonra...@googlegroups.com
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
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/383c77eeae62a42d017cdc8c88fdd811%40ruby-forum.com.

Fab Forestier

unread,
May 26, 2014, 10:00:47 AM5/26/14
to rubyonra...@googlegroups.com
Walter Davis wrote in post #1147109:
> On May 26, 2014, at 4:28 AM, Fab Forestier wrote:
>
>>> 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?

Scott Ribe

unread,
May 26, 2014, 10:58:45 AM5/26/14
to rubyonra...@googlegroups.com
On May 26, 2014, at 8:00 AM, Fab Forestier <li...@ruby-forum.com> wrote:

> 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)

Fab Forestier

unread,
May 26, 2014, 11:10:20 AM5/26/14
to rubyonra...@googlegroups.com
Scott Ribe wrote in post #1147132:
> On May 26, 2014, at 8:00 AM, Fab Forestier <li...@ruby-forum.com> wrote:
>
>> 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 Lee Davis

unread,
May 26, 2014, 11:15:16 AM5/26/14
to rubyonra...@googlegroups.com
Like this:

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

...

Walter


> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2a0b12eb5a4af2ba52535cfb429419bc%40ruby-forum.com.

Scott Ribe

unread,
May 26, 2014, 11:19:59 AM5/26/14
to rubyonra...@googlegroups.com, Fab Forestier
On May 26, 2014, at 9:09 AM, Fab Forestier <li...@ruby-forum.com> wrote:

> And then how can use the id in my methode?

Like any other parameter.

Colin Law

unread,
May 26, 2014, 2:09:40 PM5/26/14
to rubyonra...@googlegroups.com
On 26 May 2014 16:09, Fab Forestier <li...@ruby-forum.com> wrote:
> Scott Ribe wrote in post #1147132:
>> On May 26, 2014, at 8:00 AM, Fab Forestier <li...@ruby-forum.com> wrote:
>>
>>> 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?

A long time ago I seem to remember suggesting that you *work right
through* railstutorial.org and that then you would understand the
basics of rails and would not have to ask for answers to simple
questions. I believe your replied that you had looked through the
tutorials. Once again I request that you please take a few days out
and work right through it, including doing all the exercises. In the
long run you will save yourself and us a lot of time.

Colin
Reply all
Reply to author
Forward
0 new messages