Accessing Redmine controllers through a plugin - ruby-on-rails

I've already asked the question on the Redmine official website but I didn't get any answer, maybe I'll have more chance here.
I'm working on a project and I try to improve an existing plugin for Redmine by adding some features to it to allow the user to upload his Dropbox files in the Redmine documents with a simple form. Redmine already has this possibilty so I would like to use the controller and methods already defined in the Redmine source code.
I have the following code in one of my plugin views:
<% html_title "Reddrop - Sync" %>
<h2>Synchronisation page</h2>
<p>Please choose your file</p>
<%= form_tag({:controller => "documents", :action => "add_attachment", :id => #document}, :multipart => true) do %>
<%= file_field_tag 'attachments[dummy][file]', :id => nil, :multiple => true, :onchange => "addInputFiles(this)" %>
<%= submit_tag(value = "Sync this file with Redmine") %>
<% end %>
I'm calling the "documents" controller and the add_attachment method which are defined in the Redmine source code. When I submit my form I get the following error:
ActionController::RoutingError (No route matches {:controller=>"documents", :action=>"add_attachment", :id=>nil}):
Is it possible to call these controllers/methods through a plugin if they are defined in the Redmine source code?
If yes, maybe could you give some advice to how configure my routes?

See here :id => #document
#document is the variable that contains nil however this should contain some id (like 1,2 or whatever) of required record check it in your controller and once you will fix this issue sure this error will be resolved.

validate route format
rake | rails route
send the expected parameters and the correct namespace. If you already have a #document you could use the form_for.
`<% form_for(#document, url: {action:'add_attachment'} ) do %> `

Related

wrong number of arguments (3 for 2) in form_for method

I'm upgraded to rails 3.2.13 and I'm migrating an old app to run in the new environment (ruby1.9.3). The app ran fine with ruby192 and rails 3.0.0.
I was receiving this error when trying to create a new record (a firefighter)
wrong number of arguments (3 for 2)
And here was my code for my form
<%= form_for :fire_fighter, #fire_fighter, :url => { :action => "create" } do |f| %>
based on reading other posts, they recommend to remove "fire_fighter" but so it would look like this
<%= form_for :#fire_fighter, :url => { :action => "create" } do |f| %>
This did actually allow the page to render but when I tried to enter fill in the text fields and submit or create the record in the database I get an error message that is built into the app that says:
All of the fields are setup as strings.
Oh and obviously i had all this fields filled out before I hit submit. So now I'm just stuck.
Any help would be appreciated thanks.
Although this answer may change when you post your page source code and the rest of the form code, you are trying to create a symbol from an instance variable.
<%= form_for :#fire_fighter, :url => { :action => "create" } do |f| %>
Notice :#fire_fighter. It really should be #fire_fighter. The correct code should then be
<%= form_for #fire_fighter, :url => { :action => "create" } do |f| %>
The reason why you use an instance variable like #fire_fighter is because in your controller there should be something like
def new
#fire_fighter = FireFighter.new
end
that way, the form is directly grabbing the instance variable from the controller onto the form. Symbols don't transverse from controllers to views, but instance variables do, hence the use of #fire_fighter as the first argument in the form_for method.

How do I have a URL with params and a url_for?

I'm just trying to setup excel downloading option for my site. All I want to do is:
<%= link_to 'Export', export_to_excel_path(:param => #item.id), url_for(:format => 'xls') %>
I want to give my user a link to export to excel, with the additional param. I get the following error:
undefined method `stringify_keys' for "/controller/show.xls":String
I need to use url_for because in export_to_excel, I have a
respond_to
in order to respond with a .xls MIME type file. I am using the to_xls gem, and everything works fine except that I'm not able to figure out this link.
All credit to jdoe:
<%= link_to 'Export', export_to_excel_path(:param => #item.id, :format => 'xls') %>

Use of polymorphic_url in action mailer to get a full path

I have nested resources in my rails application which belong to groups. These items all have comments, and as such the comment file looks as follows:
app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :item, :polymorphic => true
...
end
I need an ActionMailer which sends the user a link to the item, nested within its specific group. This however has left me banging my head against the wall as how to get the full path into the URL for the link. This is because url_for, when given a polymorphic object calls polymorphic_url on the object, but does not path the options of :only_path => false as the polymorphic_url doesn't take said option. Even in the non-nested resource (second link) it still only gives a path
Currently my mailer view looks as follows:
views/interaction_mailer/new_comment.html.erb
Hey <%= #user.name %>!<br />
<br />
<%= #comment.user.name %> just commented on the <%= #item_string %> <%= link_to #item.title, polymorphic_path([#item.group, #item]) %> in your group (<%= link_to #item.group.name, #item.group, :only_path => false%>).<br />
As I said, this doesn't give me a full path, but instead just a relative one.
Am I missing something? Is there a way to easily get the full path in this email?
PS my config/environments/development.rb file has the following:
config.action_mailer.default_url_options = {:host => 'localhost', :port => 3000}
I'm not sure if I'm understanding. Why is it a problem if polymorphic_url doesn't accept :only_path? Don't you want the full url in the email?
Why not simply call polymorphic_url([#item.group, #item])?
This should return the full URL, which is what you want, right?
The 2nd link isn't constructed correctly. You're passing the object as the 2nd arg, and only_path as the 3rd, which is being interperted as HTML options. The ActionView url_for helper forces path when using polymorphic routes (the opposite of ActionController's url_for... go figure), but you can simply call polymorphic_url to get at the URL in this case.
<%= link_to #item.group.name, polymorphic_url(#item.group) %>
... should give you the URL.
Actually as it turned out some of the polymorphic_urls were even necessary as that is only necessary when you need to go to a controller for the polymorphic item, not if the item you are getting happens to come from a polymorphic object. As such the code cleans up to the following:
Hey <%= #user.name %>!<br />
<br />
<%= #comment.user.name %> just commented on the <%= #item_string %> <%= link_to #item.title, polymorphic_url([#item.group, #item]) %> in your group (<%= link_to #item.group.name, group_url(#item.group) %>).<br />

routing scope problem with form_for (partial)

Trying to route:
scope :shortcut do
resources :text_elems
end
Using basic scaffold with form partial
*_form.html.erb*
<%= form_for(#text_elem, :shortcut => #shortcut) do |f| %>
...
Problem is: When I call the edit action, the form html shows as:
<form ... action="/25/text_elems/25">
Note: The new action renders the form action correctly:
<form ... action="/home/text_elems">
So it appears that my :shortcut param is getting trumped by the :id param when form_for processes it's block. Now I am able to get the action to correctly route with the :shortcut param if I manually make the :url => {...} in the form_for block, but I would prefer to keep the code dry, plus I want to report this problem to rails if it is indeed a bug.
Can anyone else confirm this as a bug?
Actually, you can pass the values as a full hash, rather than trying to rely on the default to_param (which is what gets called if all you do is pass the #text_elem)
<%= form_for({:id => #text_elem.to_param, :shortcut => #shortcut}) do |f| %>
however, if this is actually a nested-resource, you could also do:
<%= form_for([#shortcut, #text_elem]) do |f| %>
I was having the same issues and none of the above answers helped.
The last answer on this page worked for me though...
https://rails.lighthouseapp.com/projects/8994/tickets/6736-problem-with-scoped-routes-and-form_for-helper

Multiple forms on a single page

I've got an app that's in invite-only beta right now. Problem is, I can't get the invite system to work. :(
On my root page there's a login form (which works just fine), and I'm trying to add a "request invite" form on the same page. I started doing it by putting the form for InviteRequest (ActiveRecord) inside a partial, in the "views" folder for "InviteRequest". The app is definitely calling this partial, but I'm getting the following error:
NoMethodError in User_sessions#new
Showing app/views/invite_request/_new.html.erb where line #2 raised:
undefined method `invite_requests_path' for #<ActionView::Base:0x25b3248>
Extracted source (around line #2):
1: <% #invite_request = InviteRequest.new() %>
2: <% form_for #invite_request do |ir| %>
3: <%= ir.label :email %>
4: <%= ir.text_field :email %>
5: <% end %>
I also read through the "Multiple Models in a Form" section of my trusty copy of "Agile Web Development with Rails", about maybe doing this with a "fieldset" tag, but not sure if this is the right approach.
Thx.
Your form_for #invite_request is trying to figure out where the results from the form should be posted. It does this by looking at the routes and finding one that matches the resource and action needed. In your case I'm guessing you haven't set up routes for InviteRequest. You either need to set up routing for InviteRequest or specify the URL to be posted to in form_for.
form_for #invite_request, :url => { :controller => 'application', :action => 'request_invite' } do |form|
...

Resources