I'm new to rails and have been working on creating a basic survey app to work through nested resources.
Here's what my eventual data structure should look like:
survey
question, with a foreign of survey
answer, with a foreign of question
choice, with a foreign of the user who is taking the survey and answer id which he will select
I was able to create the survey and question structure. But I'm running into some trouble with the answers. It's throwing an error on the last line in my question's show.html.erb file where I'm trying to link_to my new answer.
Here's the question's show.html.erb file:
<div id='question'>
<h2><%= #question.title %></h2>
<% if #question.single_response %>
<p>Single response</p>
<% else %>
<p>Multiple response</p>
<% end %>
</div>
<%= link_to "Edit Question", [:edit, #survey, #question] %>
<%= link_to "Delete Question", [#survey, #question], method: :delete, data: { confirm: "Are you sure you want to delete this question?"} %>
<p>Answers</p>
<ul id='answers'>
<% #question.answers.each do |answer| %>
<li><%= link_to answer.title, [#survey, #question, answer] %></li>
<% end %>
</ul>
<p><%= link_to "Add Answer", new_survey_question_answer_path([#survey,#question]) %></p>
Here's my routes.rb:
SurveyMe::Application.routes.draw do
resources :surveys do
resources :questions do
resources :answers
end
end
devise_for :developers
devise_for :users
I'm pretty sure the issue is the [#survey, #question] piece of the line. Any ideas what I should be putting in there?
Here's the error:
Showing /Users/thomashammond89/Test Survey.me/app/views/questions/show.html.erb where line #18 raised:
No route matches {:action=>"new", :controller=>"answers", :survey_id=>[#<Survey id: 2, title: "Test1", created_at: "2014-02-21 17:35:36", updated_at: "2014-02-21 17:35:36">, #<Question id: 2, title: "Question Test1", single_response: true, survey_id: 2, created_at: "2014-02-21 18:59:57", updated_at: "2014-02-21 18:59:57">], :id=>"2", :question_id=>nil, :format=>nil} missing required keys: [:question_id]
Extracted source (around line #18):
15 <li><%= link_to answer.title, [#survey, #question, answer] %></li>
16 <% end %>
17 </ul>
18 <p><%= link_to "Add Answer", new_survey_question_answer_path([#survey,#question]) %></p>
That should be:
new_survey_question_answer_path(#survey, #question)
Related
I am trying to create a button to delete badges. Except that I do not want to delete the badge, but to delete the subscription to a badge if i can say.
<% #subscribeds.each do |badge| %>
<span class="badge badge-secondary">
<%= link_to 'Delete', controller: 'listdedistribution',id: Listdedistribution.find_by(group_id: badge.id, user_id: current_user.id, origine: "Self-registered").id, action: :destroy, method: :delete, :class => "fas fa-trash-alt fa-lg" %>
<%= badge.name %>
</span>
<% end %>
I get the following error:
ActionView::Template::Error (No route matches {:action=>"destroy", :controller=>"listdedistribution", :id=>18458, :locale=>:en, :method=>:delete}):
Does anybody know how to target a specific records and delete it please?
Thank you
Just check your routes
resources :listdedistribution # in routes
DELETE /listdedistribution/:id(.:format) listdedistribution#destroy
I'm a real newbie at Ruby and Rails, and I've been looking for the solution for two days. I need to submit data from form_tag to action 'create' in my controller to add new entries to database, but looks like I'm doing something terribly wrong, because absolutely nothing happens, and it seems that form_tag doesn't even redirect to needed action.
Here's the page code:
<h1>Todos</h1>
<% #projects.each do |project| %>
<tr>
<h2><%= project.title %></h2>
<% project.todos.each do |todo| %>
<ul style="list-style-type:disc">
<li><%= todo.text %></li>
</ul>
<% end %>
</tr>
<% end %>
<%= form_tag({controller: "mega", action: "create"}, method: "get", remote: true) do %>
<h2>New todo</h2>
<p>
<%= text_field_tag 'text' %>
</p>
<p>
<%= select_tag 'title', options_from_collection_for_select(#projects, 'id', 'title') %>
</p>
<p>
<%= link_to 'CANCEL' %>
<%= link_to 'OK', "", :onclick => "$('#form_id').submit()" %>
</p>
<% end %>
And the controller:
class MegaController < ApplicationController
def index
#projects = Project.all
#todos = Todo.all
end
def update
end
def create
#newTodo = Todo.create(text: params[:text])
#newProject = Project.find_by(title: params[:title])
#newProject.todos << #todo
#newTodo.save
end
end
My routes file. I seriously don't know how it works:
Rails.application.routes.draw do
get 'mega/index'
root 'mega#index'
get 'mega/update'
post 'mega/create'
resources :todos
resources :projects
end
You create resources with a POST request. Never GET.
GET requests should be idempotent - they should not update or alter resources on the server. One very important reason is that they are stored in the browser's history, so pressing the back button will cause unintended consequences for the user.
In Rails flavor MVC instead of tacking the action name on the path of the route you use the HTTP verb to create routes to the correct action:
GET /things things#index
POST /things things#create
I'm not going to attempt to salvage your code (it's deeply flawed) and instead show you how you would solve this the rails way as it is much simpler:
<%= form_for(Todo.new) do |f| %>
<h2>New todo</h2>
<%= f.text_field :text %>
<%= f.collection_select(:project_id, #projects, :id, :title, prompt: true) %>
<%= f.submit %>
<% end %>
This would submit to todos#create - if you want to route it to an unconventional action you can use the url option:
<%= form_for(Todo.new, url: polymorphic_path(controller: 'foo', action: 'bar')) do |f| %>
It's best to learn the rules before you break them.
I have a little problem of link.
I have 2 models nested, one Faqcategoryand Faq.
The route is
resources :faqcategories, :path => 'faqs' do
resources :faqs, :path => 'question'
end
I can display all the "faqcategories" at http://localhost:3000/faqs/
and all the faqcategory as "questions" at http://localhost:3000/faqs/8
But when I want to go on the show of the question at http://localhost:3000/faqs/8/question/1 , it sends me at http://localhost:3000/faqs/1/question/8
I have set up the view like that:
<% #faqs.each do |question| %>
<%= link_to question.title, faqcategory_faq_path(question), class: "btn btn-rose btn-round" %>
<% end %>
In the FaqcateroriesController the "show" is set up like that:
def show
#faqs = #faqcategory.faqs
end
How do you think I can solve that?
I have found the solution.
<% #faqcategory.faqs.each do |question| %>
<%= link_to question.title, faqcategory_faq_path(question.faqcategory_id, question), class: "btn btn-rose btn-round" %>
<% end %>
It was missing "question.faqcategory_id"
<ul>
<% #topic.contents.each do |content| %>
<li>
<%= content.content %> <%= link_to "Edit", edit_content_path(content) %> <br><br>
</li>
<% end %>
</ul>
I need someway to test this using Capybara. I tried typing this in my test file:
test "there is an edit link on the show page" do
click_link "Enter"
click_link "Ruby"
assert find_link("Edit").first.visible?
end
But it winds up giving me a message:
1) Error:
VisitorFindContentTest#test_there_is_an_edit_link_on_the_show_page:
Capybara::Ambiguous: Ambiguous match, found 2 elements matching link "Edit"
test/integration/visitor_find_content_test.rb:44:in `block in <class:VisitorFindContentTest>'
What else can I do? What can be done? Should I do something to make each of my edits link unique? OR is there a capybara test method just that looks for the first appearance of an edit link?
Add an :id or a :class specific to the content as follows:
<%= content.content %> <%= link_to "Edit", edit_content_path(content), id: "content_#{content.id}" %> <br><br>
or
<%= content.content %> <span id: "content_#{content.id}"><%= link_to "Edit", edit_content_path(content) %></span> <br><br>
You can then use this id for clicking the link as follows (with the second option above):
content = Content.first # or some way of finding the required content object
within("#content_#{content.id}") do
click_link("Edit")
end
Good Morning,
I'm having an issues with nested comments. I have a partial which shows these but I want to add a delete snippet at the bottom of .each one.
Here is the partial:
_snippets.html.erb
<% #snippets.each do |snippet| %>
<%= raw(snippet.content) %>
<% if can? :manage, snippet %>
<%= link_to 'delete', book_snippet_path(snippet), :method => :delete %>
<% end %>
<% end %>
Here are my routes:
book_snippets POST /books/:book_id/snippets(.:format) snippets#create
edit_book_snippet GET /books/:book_id/snippets/:id/edit(.:format) snippets#edit
book_snippet PATCH /books/:book_id/snippets/:id(.:format) snippets#update
PUT /books/:book_id/snippets/:id(.:format) snippets#update
DELETE /books/:book_id/snippets/:id(.:format) snippets#destroy
Here is the stack error, showing no route matches update?
No route matches {:action=>"update", :controller=>"snippets", :id=>nil, :book_id=>#<Snippet id: 4, content: "<p>YACHT!</p>\r\n", book_id: 4, created_at: "2013-11-15 09:12:20", updated_at: "2013-11-15 09:12:25", approved: true, user_id: 1>, :format=>nil} missing required keys: [:id]
I know it's probably something stupid I'm missing but would really like some help figuring this one out.
Thanks :)
You are missing book_id . You routes says
DELETE /books/:book_id/snippets/:id(.:format)
needs a book_id in path. So need to pass #book object as well in the arguments.
<%= raw(snippet.content) %>
<% if can? :manage, snippet %>
<%= link_to 'delete', book_snippet_path(#book, snippet), :method => :delete %>
<% end %>