Could not find a valid mapping for nil - ruby-on-rails

So, I'm using carrier wave to allow users to upload profile images. Now ultimately, I'd like the user to be able to click on the image and upload a new one on the fly in the profile.html.haml page.
So, I've tried the below adding a form with a put action to let the user update the image on the fly without having to go to the edit.html.haml page.
However, I'm being hit with this error:
Could not find a valid mapping for nil
Extracted source (around line #8):
<%= form_for(#user, :url => registration_path(#user), :html => { :method => :put }, :html => {:multipart => true}) do |f| %>
<%= link_to (image_tag current_user.profile_image_url(:thumb).to_s), edit_account_path(current_user)%>
<% end %>
Where did I go wrong?
Alternatively I've tried this which might be totally off:
<%= link_to (image_tag current_user.profile_image_url(:thumb).to_s), f.file_field :profile_image%>
which gave this error:
Sites/Friendiose-master/app/views/home/landing_welcome.html.erb:9: syntax error, unexpected tSYMBEG, expecting ')'
...l(:thumb).to_s), f.file_field :profile_image);#output_buffer...
Thanks in advanced.

So firstly, what do you think this error might me. Most common errors ever:
Sites/Friendiose-master/app/views/home/landing_welcome.html.erb:9: syntax error, unexpected tSYMBEG, expecting ')'
...l(:thumb).to_s), f.file_field :profile_image);#output_buffer...
I'm not going to give you full answer but teach you to think how you can debug it.
https://github.com/carrierwaveuploader/carrierwave
That is the documentation for carrierwave. When showing the image it says the following:
<%= form_for #user, :html => {:multipart => true} do |f| %>
<p>
<label>My Avatar</label>
<%= image_tag(#user.avatar_url) if #user.avatar? %>
<%= f.file_field :avatar %>
<%= f.hidden_field :avatar_cache %>
</p>
<% end %>
One tip and this is not me being sarcastic but get used to reading alot and googling even more. I haven't used paperclip but I can see the issues from 2 mins googling.
Ruby on Rails using link_to with image_tag
There is how to create a link with an image.
From that link look at the difference between yours:
<%= link_to (image_tag current_user.profile_image_url(:thumb).to_s), edit_account_path(current_user)%>
and the correct way:
<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>
What does the error say you are missing?
It is not always great to give people an answer straight away but the keys on how to figure it out.

Related

DragonFly Image Not Displaying

I am currently trying to get image uploading to work on Rails 4. On my development server when I upload a file it only displays on the page as a string of text, and I get the following error in my console:
identify: no decode delegate for this image format `' # error/constitute.c/ReadImage/562.
[2019-12-10 13:35:01] ERROR Dragonfly::FunctionManager::UnableToHandle: None of the functions registered with #<Dragonfly::Encoder:0x007f92df90d1d8> were able to deal with the method call encode(<Dragonfly::TempObject pathname=#<Pathname:/Users/me/projects/boss/public/system/dragonfly/dev,:jpg). You may need to register one that can.
I have tried reinstalling ImageMagick and the delegates but I have observed no difference.
Here is the code for the display and the upload.
<%= image_tag #person.photo.jpg.url %>
<%= form_for #person, :html => {:multipart => true} do |f| %>
<div class="form-group">
<%= f.file_field :photo %>
</div>
<% end %>
Here is the current output:

Trouble with Concatenation in Form

I am trying to generate dynamic CSS ids for javascript purposes, and the following code is giving me an error:
<%= form_for #item, html: { multipart: true } do |f| %>
<%= f.fields_for :item_images do |builder| %>
<%= image_tag builder.object.image.url(:large), :class => "cropbox", 'data-id' => builder.object.id %>
<% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
<%= builder.text_field attribute, :id => attribute + builder.object.id %>
<% end %>
<% end %>
<% end %>
I know I'm not concatenating attribute and builder.object.id properly, but I've tried everything with no luck. I get this error message:
undefined method `+' for :crop_x:Symbol
Appreciate any help, thanks!
What is your expected result?
Can you p out what '''attribute''' is within your expression?
Also, have you tried .concat instead of '+'?
Away from my desk, but my suggestion is something like:
Example:
attribute.concat(builder.object.id)
Maybe need to convert attribute.to_i or .to_s into a responsive type or create a different dynamic CSS id solution

Rails feedback form: How to get the url for the current page?

I am working with David Francisco's rails feedback plugin. The plugin works fine, except for one thing - I would need the feedback form to submit to the database the url for the page where the feedback form was used. Does anyone know how to do this?
The view that would need to send the current url, in addition to the currently sent information:
<h4>Feedback</h4>
<p>Please leave us feedback, it's really appreciated.</p>
<%= form_for #feedback, :as => :feedback, :url => feedback_index_path, :html => { :id => "feedback_form" } do |f| -%>
<%= f.hidden_field 'user_id', :value => current_user.id %>
<% unless #error_message.blank? %>
<p class="error">
<%=h #error_message %>
</p>
<% end %>
<p>
<%= f.label 'subject' %>
<%= f.select 'subject', ['Problem', 'Suggestion', 'Question', 'Other'] %>
</p>
<p>
<%= f.label 'comment' %><br />
<%= f.text_area 'comment', :rows => 10, :cols => 30 %>
</p>
<p><%= f.submit 'Send' %></p>
<% end -%>
Currently, feedback_index_path is always '/feedback', the url for the form.
Thank you,
Alexandra
You can use request.referer in your controller to get the path of the page that called your action. request.referer returns a full url but you can parse it with the URI module:
URI(request.referer).path
I see some people have suggested request.fullpath but this is actually the path of the action that's processing the request (in your case /feedbacks/new) and not the path where the form was submitted from.
If request.fullpath doesn't work incorrectly, you can make a quick hack, storing page url in data-attributes of page, and on submit get current url by jQuery from that attributes.
But it is a hack, just to make it working.
BTW how do you render feedback form?
feedback_index_path is a routes helper method that will always return the same thing. In your case /feedback.
Look here for info on accessing the current URL in both Rails 2 and 3.

carrierwave doesn't render, path exists but image doesn't show up

ive been trying to render a picture from carrierwave. i believe it is uploaded correctly because when i view the page source, i see
<img alt="Photo_44" src="/uploads/user/image/59/Photo_44.jpg" />
however by clicking on that src url, i get
No route matches [GET] "/uploads/user/image/59/Photo_44.jpg"
im using the default settings for carrierwave. the image does correctly get uploaded to my image column in my users table and locally, the path
sasha/Desktop/rails_projects/myproject/public/uploads/user/image/59/Photo_44.jpg
exists as well. however it won't display correctly. ive been following the railscasts
http://railscasts.com/episodes/253-carrierwave-file-uploads?autoplay=true
and reading
https://github.com/jnicklas/carrierwave
but i cant seem to figure out what is wrong. where i upload the image is here
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name),
:html => { :method => :put, :multipart => true }) do |f| %>
<%= devise_error_messages! %>
<%= render '/shared/fields', object: f.object, f: f %>
<%= f.submit "Save changes", :class => "btn btn-large btn-primary", :style =>"display:block;" %>
<div class="edit_avatar" >
<%= f.file_field :image %>
</div>
<% end %>
and im trying to render it by...
<%= image_tag #user.image_url.to_s %>
what am i doing wrong?
help would be appreciated = )
thank you
Try set config.serve_static_assets = true if you haven't yet, should help.

Rails file_field is not returning original_filename

I have the below form in my view:
<% form_for(#filedata, :url=>{ :controller=>"filedatas", :action=>"create"}, :multipart => true) do |f| %>
<label>Select File</label> : <%#= f.file_field :file %>
<%= file_field_tag 'uploadedFile' %>
<%= f.submit 'Upload' %>
</p>
<% end %>
I've commented out the f.file_field but I have tested on both and both give me the same problem. They just return the name of the file and I get a string. methods like .path and .original_filename cause an error.
In my list of parameters I get:
"uploadedFile"=>"test"
(the name of my file is test.txt)
Anyone have any suggestions?
Sorry, I may have misunderstood your original question. It looks like you have an error in your form_for call. It should be:
<% form_for(#filedata, :url=>{ :controller=>"filedatas", :action=>"create"}, :html => {:multipart => true}) do |f| %>
You were missing the ":html =>{}" part. Also, you can shorten it down like this:
<%= form_for #filedata, :html => {:multipart => true} do |f| %>
This is happening because the contents of the file aren't stored in the attribute like other fields. File contents are stored in the POST data, and have to be retrieved. Extra steps are required.
This article explains one way to do it manually. But a better approach is to use a plugin like thoughtbot's paperclip to handle this for you.

Resources