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

On 22 May 2014 13:32, Fab F. [email protected] wrote:

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

Colin L. 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:

@[email protected][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 L. 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.

On 22 May 2014 14:09, Fab F. [email protected] wrote:

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

On 22 May 2014 14:47, Fab F. [email protected] wrote:

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 L. 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 L. wrote in post #1146811:

On 22 May 2014 16:00, Fab F. [email protected] 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.

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

On 22 May 2014 18:17, Fab F. [email protected] wrote:

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

On 22 May 2014 16:00, Fab F. [email protected] wrote:

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

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?

On 2014-May-22, at 15:39 , Colin L. [email protected] wrote:

different think my button has to redirect to an Operation view but it is
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

On May 23, 2014, at 12:41 AM, Fab F. [email protected]
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 R.
[email protected]
http://www.elevated-dev.com/
(303) 722-0567 voice

On 23 May 2014 07:41, Fab F. [email protected] 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.

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

Colin L. wrote in post #1146856:

On 23 May 2014 07:41, Fab F. [email protected] 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?

On May 26, 2014, at 4:28 AM, Fab F. 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

foo.bar foo.baz link_to foo.boo, foo.blarg, :stock_number => foo.id end

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

Walter

Walter D. wrote in post #1147109:

On May 26, 2014, at 4:28 AM, Fab F. 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

foo.bar foo.baz link_to foo.boo, foo.blarg, :stock_number => foo.id 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| %>

<%= n.nomdefaut.nom_defaut%> <%= n.repere%> <%= link_to controller: "pages", action: "ajouter" do%>Ajouter<%end%>

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

Scott R. wrote in post #1147132:

On May 26, 2014, at 8:00 AM, Fab F. [email protected] wrote:

But the in the methode ajouter how can I have something like
@myvariable=n or @[email protected][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 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| %>

<%= n.nomdefaut.nom_defaut%> <%= n.repere%> <%= link_to controller: "pages", action: "ajouter" do%>Ajouter<%end%> <% 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.

On May 26, 2014, at 10:00 AM, Fab F. wrote:

the line of the table where the button is?

foo.bar

code to display my table is :
@myvariable=n or @[email protected][i] where I is the current line
of the table?

Like this:

<% @defautrec.each_with_index do |n, idx| %>

<%= link_to( controller: "pages", action: "ajouter", myvariable: idx) do%><button,

Walter

On May 26, 2014, at 9:09 AM, Fab F. [email protected] wrote:

And then how can use the id in my methode?

Like any other parameter.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs