Rails: pass 2d array to link_to

I have a controller querys

with an action send_file

. querys_controller.rb

def send_file
    send_data(params[data], :filename => "query.txt")
end

      

in html.erb I have:

<%=link_to "send data", :controller=>"querys", :action=>"send_file", :data=>@mat, method: :post%>

      

Clicking on "send data" rails shows me a "Bad Request" because @mat is a 2 dimensional array and it seems that link_to cannot send such a structure. How can I send my matrix to the controller?

@mat:  
[["1681", "", "02.05.1955"], ["1680", "", "02.03.1936"], ["1679", "", "26.11.1938"], ["1692", "", "15.05.1958"]]

      

+3


source to share


3 answers


@Tonja it seems very strange to me what you are doing. First you generate the array somewhere, and then pass it again using the browser back to your application. And then you send it back to the browser in plain text? You shouldn't pass this array to the user, but keep it on the server. Just save it to the database. Do not give data to the user only when absolutely necessary. Your current implementation also forces you to do quite a bit of validation on the passed data.

In the controller method that generates html.erb, store the @mat instance somewhere and get the post id. Pass this link_to id and use this data from DB as a parameter to call send_data.

(A little tip: don't actually use an identifier that isn't safe, but use a random value. Or better yet: pass nothing at all if you can attach a value to current_user).

You are getting the wrong request because Rails does not understand the format you are asking for. And that's because you collect the URL yourself. Use a call like

link_to "send file", send_file_querys_path(format: :txt)

      

or even



button_to ....

      

if it is a POST operation.

You can get valid routes using "rake routes". This makes your application more robust.

I hope my answers will help you refactor your code. If what you are doing is the right way, then @m_x gave you the correct pointers.

Respectfully,

Hugo

+1


source


when passing url arguments as a hash, you must separate them from the rest of the arguments for link_to

:

<%
  = link_to "send data", 
            {controller: "querys", action: "send_file", data: @mat}, 
            method: :post
%>

      

However, this will pass data as query parameters. If you really want to pass data as POST parameters (for example, if it @mat

contains a lot of data), you will need to use a form tag with a hidden field and a submit button.

IMO a more efficient approach would be to pass the parameters needed to populate @mat

, repopulate the variable on the server side, and use it in send_file

.

The best practice in this regard is to use the capabilities of rails format

, for example:

def some_action
  @mat = populate_mat(params)
  respond_to do |format|
    format.html
    format.txt do
      send_data(@mat, :filename => "query.txt")
    end
  end
end

      



(note: you probably have to register the mime type txt

in for this config/initializers/mime_types.rb

)

then in some_action.html.erb

:

<% = link_to "send data", {format: :txt}, class: 'whatever' %>

      

It will just add an extension .txt

at the end of the current url that fits nicely with the REST paradigm (one endpoint per resource with an extension indicating the view you want).

You can also pass the parameter to format

any url helper like:

<%= link_to "root", root_path(format: :txt), class: 'whatever' %>

      

0


source


Try the following:

link_to("Send data", send_file_querys_path(data: @mat), method: :post) 

      

0


source







All Articles