rails error message String can't be coerced into Fixnum - ruby-on-rails

I'm trying to make variable in .each do loop.
<article class='lecture-list' >
<% #lectures.each do |lecture| %>
<div class='lecture-list-wrapper'>
<p class='activity'>
<% name = lecture.id + "th - "+ lecture.title %>
<span><%= link_to name, lecture, class: 'btn-lightgreen' %></span>
<span><%= link_to 'Edit', edit_lecture_path(lecture), class: 'btn-darkyellow' %></span>
<span><%= link_to 'Destroy', lecture, method: :delete, class: 'btn-darkorange', data: { confirm: 'Are you sure?' } %></span></p>
</div>
<% end %>
</article>
<% name = lecture.id + "th - "+ lecture.title %> part makes
String can't be coerced into Fixnum
error. How can I solve it?

Instead of:
<% name = lecture.id + "th - "+ lecture.title %>
Try:
Note: If you want it as variable, ideally use _ underscore not - dash. As code below.
<% name = lecture.id.to_s + "th_"+ lecture.title %>

OJ1987 answer is fine, though I prefer using interpolation in such cases:
<% name = "#{lecture.id}th - #{lecture.title}" %>

Related

How to delete a specific Hash key and value that is nested with the same key with a button link

The goal is to remove a specific url from the files.
The Hash -
[{"id":2,"job_id":1358,"files":[{"url":"/uploads/job_photo/files/2/signature.png"},{"url":"/uploads/job_photo/files/2/zombie.jpg"}],"url":"https://www.americanpatiodriveandwalk.com/job_photos/2.json"},{"photos":["signature.png","zombie.jpg"]}]
I separated each url in files so they each have a button.
The View -
<tbody>
<% #job_photos.each do |job_photo| %>
<% job_photo.files.each_with_index do |file, index| %>
<tr>
<td>
<%= file %>
</td>
<td>
<%= link_to file.to_s, title: 'View', class: 'btn btn-xs btn-info', role: 'button' do %>
<i class="material-icons">search</i>
<% end %>
<%= link_to remove_photo_path(job_photo.id, index), title: 'Delete', class: 'btn btn-xs btn-danger', role: 'button', data: { confirm: 'Are you sure?' } do %>
<i class="material-icons">clear</i>
<% end %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
That is the first link results from the code with 0 for the index -
<a title="Delete" class="btn btn-xs btn-danger" role="button" data-confirm="Are you sure?" href="/job_photos/2/photo/0/remove_photo">
I added the index for the route because I figured I would need it eventually.
The Route -
put '/job_photos/:id/photo/:index/remove_photo', to: 'job_photos#remove', as: :remove_photo
If I didn't fail miserably yet...this is where I fall to pieces I think in the controller -
def remove
job_photo = JobPhoto.find(params[:id])
photo_index = JobPhoto.find(params[:index])
job_photo[:files][photo_index].delete(:url)
job_photo.update!(:files)
flash[:success] = "Photo removed!"
redirect_to request.referer || root_path
end
The part where I try to delete the url from the hash doesn't even make sense...but I put it there so show my struggle.
Thanks for any help!
Figured it out -
def remove
#job_photo = JobPhoto.find(params[:id])
total_photos = params[:index].to_i
if total_photos > 0
photo_index = params[:index].to_i
photos = #job_photo.files
delete_image = photos.delete_at(photo_index)
delete_image.try(:remove!)
#job_photo.files = photos
#job_photo.save
else
#job_photo.destroy
end
flash[:success] = "Photo removed!"
redirect_to request.referer || root_path
end
I am sure it can be cleaned up, but works perfectly.

syntax error, unexpected tIVAR, expecting ')' #attempt.name = params[:content][:attempt_name][:value]

I want to add "mercury" gem, but I got error mentioned in the title.
Here is my method:
def mercury_update
#attempt = Attempt.find(params[:id]
#attempt.name = params[:content][:attempt_name][:value]
#attempt.content = params[:content][:attempt_content][:value]
#attempt.save!
end
and here is my view:
<h1 id="attempt_name" class="mercury-region" data-mercury="full"><%= raw #attempt.name %></h1>
<%= render 'form' %>
<p id="attempt_content" class="mercury-region" data-mercury="full" > <%= raw #attempt.content %> </p>
<p><%= link_to "Edit Page", "/editor" + request.path, id: "edit_link", data: { save_url: mercury_update_attempt_path(#attempt) } %></p>
Close the find parenthesis
#attempt = Attempt.find(params[:id])

Rails carousel producing 'undefined method `image_path' in controller

I'm trying to make a carousel in a Rails app and can't seem to get around a problem I'm having. I'm new to Rails - so it's probably a simple resolution. This produces a 'undefined method `image_path' in my controller. Thanks in advance for any help!
Here is my controller:
def golf
images = ['CreekCoverLarge.jpg', 'CreekWindmillLarge.jpg', 'CreekExtraLarge.jpg', 'CreekExtra2Large.jpg']
#paths = images.map {|name| image_path name}
end
And this is my view:
<div class="item active">
<%= #paths.each do |path| %>
<%= link_to(image_tag(path, class: "img-responsive", size: "600x440"), "#", data: {toggle: "modal", target: "#myModal"}, "onclick"=>"getImage(this)", "data-img-address" => path) %>
<% end %>
</div>
use:
ActionController::Base.helpers.image_path
Although, cant you just loop through images in the view and call image_path there?
Edit:
Example as requested:
Controller:
def golf
#images = ['CreekCoverLarge.jpg', 'CreekWindmillLarge.jpg', 'CreekExtraLarge.jpg', 'CreekExtra2Large.jpg']
end
View:
<div class="item active">
<%= #images.each do |path| %>
<%= link_to(image_path(path, class: "img-responsive", size: "600x440"), "#", data: {toggle: "modal", target: "#myModal"}, "onclick"=>"getImage(this)", "data-img-address" => path) %>
<% end %>
</div>
This is untested, but should work.

How would I go about adding a button id so I can tell which button has been pressed in Rails

I've got this bunch of code
<%= link_to admin_conference_statuses_path(conference_id: #conference.id), class: "btn btn-primary", method: :post, remote: true do %>
<span id="span">Comm invoiced out Venue</span>
<% end %>
<%= link_to admin_conference_statuses_path(conference_id: #conference.id), class: "btn btn-primary", method: :post, remote: true do %>
<span id="span">Cross charged to Client</span>
<% end %>
And I have this in my controller
def create
conference_id = params[:conference_id] #Keep the same
#conference_status = ConferenceStatus.find_by_conference_id(conference_id)#Keep the same
#conference_status = ConferenceStatus.new unless #conference_status#Keep the same
#conference_status.conference_id = params[:conference_id]
#conference_status.invoiced_out_user_id = current_user.id
#conference_status.invoiced_out_datetime = DateTime.now
if #conference_status.save
# Success
else
# Failure
end
end
Now, when one button is pressed it grabs the id and puts it into a database.
How would I go about adding it so that when button 2 (opposed to button 1) is pressed it puts current user id into a column called "cross_charged_user_id"
If you have the answer could you post it and explain what it does, so I know for next time?
Thanks
Sam
You can pass one extra parameter to second link. Then depending on this extra parameter you can assign the current user as cross_charged_user.
The html code look like:
<%= link_to admin_conference_statuses_path(#conference), class: "btn btn-primary", method: :post, remote: true do %>
<span id="span">Comm invoiced out Venue</span>
<% end %>
<%= link_to admin_conference_statuses_path(#conference, cross_site_to_client: true), class: "btn btn-primary", method: :post, remote: true do %>
<span id="span">Cross charged to Client</span>
<% end %>
And the controller just check the params[:cross_site_to_client] and assign the current user
if params[:cross_site_to_client].present?
#conference_status.cross_site_to_client_id = current_user.id
end
Even You can cleanup your code as well
#conference_status = ConferenceStatus.find_or_create_by_conference_id(params[:conference_id])
if params[:cross_site_to_client].present?
#conference_status.cross_site_to_client = current_user
else
#conference_status.invoiced_out_user = current_user
end
#conference_status.invoiced_out_datetime = DateTime.now
#conference_status.save

How do I use the Quicksand jQuery plugin with a Rails AJAX request?

I am trying to use the Quicksand jQuery plugin to decorate the response of an AJAX call in Rails:
Link:
<% Project.services.each_with_index do |service, index| %>
<%= link_to_function(service, "regatherProjects('#{service}', #{index})", :id => "service_link_#{index}") %>
<%= " / " if !index.eql?(Project.services.length - 1) %>
<% end %>
JS:
function regatherProjects(service_name, service_id) {
$.get("/index_project_update", { service: service_name }, function(data) {
$('#home_projects').quicksand($(data), {
adjustHeight: 'dynamic'
});
});
$('#home_services').find('a').removeClass("on");
$('#service_link_' + service_id).addClass("on");
};
Controller:
def index_project_update
if params[:service] && !params[:service].blank?
#projects = Project.highlighted.where(:service => params[:service])
else
#projects = Project.highlighted
end
end
View:
$("#home_projects").replaceWith("<%= j(render('projects')) %>")
Projects:
<div id="home_projects">
<% if #projects.empty? %>
<p>No projects. <%= link_to_function("View All", "regatherProjects('')") %>.</p>
<% else %>
<ul class="all_projects">
<% #projects.each_with_index do |project, index| %>
<li data-id="<%= project.customer.name.underscore.downcase %>_<%= project.name.underscore.downcase %>">
<%= link_to(image_tag(project.project_images.first.image.home), project) %>
</li>
<% end %>
</ul>
<% end %>
</div>
I think the problem is that the data returned by the AJAX request (see below) is not in the format that Quicksand expects it to be in but I'm not sure how to extrapolate the relevant information from the response. The response from my AJAX request is something like:
$("#home_projects").replaceWith("<some><html><that><i><need>")
So, the problem is that it's using the above code as the destination code for the Quicksand call when it should be using the replacement HTML:
<some><html><that><i><need>
The AJAX request works perfectly fine, but I need to integrate Quicksand into here.
I need:
a better way to do this or;
a way to extrapolate the argument for replaceWith() in the aforementioned code to use as the destination code for Quicksand.
If I've omitted any information you think you need to help, please ask.
This is the first question I've asked on Stack Overflow so I feel obligated to apologise for any misusage.
If anyone runs into a similar issue, the following worked for me in Chrome, Firefox, IE9, IE8 and IE7.
Link:
<% Project.services.each_with_index do |service, index| %>
<%= link_to_function(service, "regatherProjects('#{service}', #{index})", :id => "service_link_#{index}") %>
<%= " / " if !index.eql?(Project.services.length - 1) %>
<% end %>
JS:
function regatherProjects(service_name, service_id) {
$.get("/index_project_update.html", { service: service_name }, function(data) {
$('.all_projects').quicksand($(data).find('li'), {
adjustHeight: 'dynamic'
});
});
$('#home_services').find('a').removeClass("on");
$('#service_link_' + service_id).addClass("on");
};
Route:
match '/index_project_update' => 'application#index_project_update', :defaults => { :format => 'html' }
Controller:
def index_project_update
if params[:service] && !params[:service].blank?
#projects = Project.highlighted.where(:service => params[:service])
else
#projects = Project.highlighted
end
render :layout => false
end
View:
<%= render('application/index/projects') %>
Projects:
<div id="home_projects">
<% if #projects.empty? %>
<ul class="all_projects">
<li data-id="project">No projects. <%= link_to_function("View All", "regatherProjects('')") %>.</li>
</ul>
<% else %>
<ul class="all_projects">
<% #projects.each_with_index do |project, index| %>
<li data-id="project_<%= project.id %>">
<%= link_to(image_tag(project.project_images.first.image.home, :alt => (project.customer.name + " - " + project.name), :title => (project.customer.name + " - " + project.name)), project) %>
</li>
<% end %>
</ul>
<% end %>
</div>

Resources