I have a list of movie categories displayed on my home page. Along with this, I have displayed the list of ALL the movies currently available in the db. When the user clicks on one of the categories, I would like to re-render the containing the list of ALL movies to show ONLY the list of movies that belong to the category that the user selected.
I use link_to to display the list of categories. The link_to would be routed to a controller function which loads the movies that belong Only to the category selected by the user. Then a js.erb would be invoked with this generated list which would in-turn invoke a Rails partial.
The Rails partial would re-render the complete movie list to display ONLY the movies that belong to the selected category. The javascript and partial are getting invoked all right but the partial fails to re-render the list. Am not sure what I'm missing here.
app/views/movies/index.html.erb
<% #categories.each do |category| %>
<tr><td>
<%= link_to category, show_category_url(:genre => category), :remote => true %> <%end%>
<%if false %>
<%= link_to show_category_url, :remote => true do %>
category
<%end %>
</td></tr>
<% end %>
<div id="#movies_list">
<% #movies.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.category %> </td>
<td><%= movie.rating %></td>
<% if !current_user.nil? %>
<% if movie.user.email == current_user.email %>
<td><%= link_to 'Show', movie, :class => "btn btn-primary"%></td>
<td><%= link_to 'Edit', edit_movie_path(movie), :class => "btn btn-primary" %></td>
<td><%= link_to 'Destroy', movie, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-primary" %></td>
<% end %>
<% end %>
</tr>
<% end %>
</div>
app/views/movies/show.js.erb
$("#movies_list").html("<%= escape_javascript(render("show_category")) %>");
app/views/movies/_show_categories.html.erb
<% #movies_in_category.each do |movie| %>
<tr>
<td><%= movie.title %></td>
<td><%= movie.rating %></td>
<% puts "************** movies/show_category"%>
<% puts movie.title %>
<% if false %>
<td>
<%= form_for #rating do |rating_form| %>
<%= rating_form.number_field :rating, class: 'rating', 'data-size' => 'xs'%>
<%= rating_form.submit 'Add', class: 'btn btn-primary btn-success' %>
<% end %>
</td>
<% end %>
<% if !current_user.nil? %>
<% if movie.user.email == current_user.email %>
<td><%= link_to 'Show', movie, :class => "btn btn-primary"%></td>
<td><%= link_to 'Edit', edit_movie_path(movie), :class => "btn btn-primary" %></td>
<td><%= link_to 'Destroy', movie, method: :delete, data: { confirm: 'Are you sure?' }, :class => "btn btn-primary" %></td>
<% end %>
<% end %>
</tr>
<% end%>
My partial is getting invoked all right but the re-rendering of div movies_list does not happen.
This is my controller code:
class MoviesController < ApplicationController
before_action :set_movie, only: [:show, :edit, :update, :destroy]
#before_action :authenticate_user!
# GET /movies
# GET /movies.json
def index
#movies = Movie.all
$all_movies = #movies
#categories = #movies.uniq.pluck(:category)
#movies_by_category = Hash.new
#categories.each do |category|
#movies_by_category[category] = Movie.where(:category => category).length
end
end
# GET /movies/1
# GET /movies/1.json
def show
respond_to do |format|
format.js {render layout: false}
puts "############## moviescontroller/show_category"
end
end
# GET /movies/new
def new
#movie = current_user.movies.new
end
# GET /movies/1/edit
def edit
end
# POST /movies
# POST /movies.json
def create
#movie = current_user.movies.new(movie_params)
respond_to do |format|
if #movie.save
format.html { redirect_to #movie, notice: 'Movie was successfully created.' }
format.json { render :show, status: :created, location: #movie }
else
format.html { render :new }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /movies/1
# PATCH/PUT /movies/1.json
def update
respond_to do |format|
if #movie.update(movie_params)
format.html { redirect_to #movie, notice: 'Movie was successfully updated.' }
format.json { render :show, status: :ok, location: #movie }
else
format.html { render :edit }
format.json { render json: #movie.errors, status: :unprocessable_entity }
end
end
end
# DELETE /movies/1
# DELETE /movies/1.json
def destroy
#movie.destroy
respond_to do |format|
format.html { redirect_to movies_url, notice: 'Movie was successfully destroyed.' }
format.json { head :no_content }
end
end
def show_category
category_selected = params[:genre]
#all_movies = Movie.all
#movies_in_category = Movie.where(category: category_selected)
puts "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
puts category_selected
puts #movies_in_category.length
end
def login
end
private
# Use callbacks to share common setup or constraints between actions.
def set_movie
#movie = Movie.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def movie_params
params.require(:movie).permit(:title, :category, :rating)
end
end
The reason is,
<div id="#movies_list">
contents goes here..
</div>
You shouldn't add # when defining id attribute for an element, That will be used when selecting an HTML element based on the selector. So remove #
<div id="movies_list">
contents goes here..
</div>
Related
I have this code below which loads the patient data and with each one the update button updates it but on clicking nothing happens, here is the code:
<% emergency_case.patients.each do |patient| %>
<tr>
<%= simple_form_for (:patient),url: patients_edit_path(patient.id) do |f|%>
<td><%=f.input :name ,:input_html => { :value => patient.name},label: false %></td>
<td><%=f.input :IDNumber ,:input_html => { :value => patient.IDNumber},label: false %></td>
<td><%=f.input :age ,:input_html => { :value => patient.age},label: false %></td>
<td><%=f.input :phone ,:input_html => { :value => patient.phone},label: false %></td>
<td><%=f.input :address ,:input_html => { :value => patient.address},label: false %></td>
<td><%=f.input :injury ,:input_html => { :value => patient.injury},label: false %></td>
<td><%= f.collection_select(:state_id, State.all, :id, :state) %></td>
<td><%= f.collection_select(:Act, Act.all, :id, :act) %></td>
<td><%=f.submit %></td>
<% end %>
</tr>
<% end %>
Here is the paitent controller which am sending the form for to make updates on the paitent that is sent:
class PatientsController < ApplicationController
before_action :set_patient, only: [:show, :edit, :update, :destroy]
# GET /patients
# GET /patients.json
def index
#patients = Patient.all
end
# GET /patients/1
# GET /patients/1.json
def show
end
# GET /patients/new
def new
#patient = Patient.new
end
# GET /patients/1/edit
def edit
#patient =Patient.find(params[:id])
end
# POST /patients
# POST /patients.json
def create
#patient = Patient.new(patient_params)
respond_to do |format|
if #patient.save
format.html { redirect_to #patient, notice: 'Patient was successfully created.' }
format.json { render :show, status: :created, location: #patient }
else
format.html { render :new }
format.json { render json: #patient.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /patients/1
# PATCH/PUT /patients/1.json
def update
respond_to do |format|
if #patient.update(patient_params)
format.html { redirect_to #patient, notice: 'Patient was successfully updated.' }
format.json { render :show, status: :ok, location: #patient }
else
format.html { render :edit }
format.json { render json: #patient.errors, status: :unprocessable_entity }
end
end
end
# DELETE /patients/1
# DELETE /patients/1.json
def destroy
#patient.destroy
respond_to do |format|
format.html { redirect_to patients_url, notice: 'Patient was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_patient
#patient = Patient.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def patient_params
params.require(:patient).permit(:name, :isDead, :status, :IDNumber, :emergency_case_id,:state_id,:address,:age,:phone,:injury,:act)
end
end
Several issues to contend with here:
<% emergency_case.patients.each do |patient| %>
<%= content_tag :tr do %>
<%= simple_form_for patient, method: :put do |f|%>
<% attributes = %i(name IDNumber age phone address injury) %>
<% patient.attributes do |attr| %>
<%= content_tag :td, f.input attr, input_html: { value: patient.send(attr)}, label: false %>
<% end %>
<%= content_tag :td, f.state_select :state_id %>
<%= content_tag :td, f.collection_select(:Act, Act.all, :id, :act) %>
<%= content_tag :td, f.submit %>
<% end %>
<% end %>
<% end %>
ALWAYS use snake_case for attributes (IDNumber is baaad umkay)
Check out the state_select gem
Loops are the BEST way to keep forms succinct & efficient
Your form is sending to the edit action -- you need to send to the update action
#4 will answer your question -- patients_edit_path(patient.id)
What you need is to send to the update path: patient_path(patient), method: :put... or simply: patient, method: :put
let simple form do the work for you on the url, method, etc unless you have something that is custom. If this doesn't work please post more info on the error you are getting in your post.
<% emergency_case.patients.each do |patient| %>
<tr>
<%= simple_form_for patient do |f|%>
....#form stuff
<td><%=f.submit %></td>
<% end %>
</tr>
<% end %>
I have a model called Images with an uploader attached to it (Carrierwave). Images belongs to a model called Listing. After creating a listings I'm redirected to the Images index page to upload files (localhost:3000/listings/1/images)
But for some reason every time I create a listing an image it's created at the same time. There's actually no image present but it displays the "delete" link I have for each image.
<span><%= link_to 'DELETE', listing_image_path(#listing, image.id), data: { confirm: 'Are you sure?' }, :method => :delete, :class => 'delete' %></span>
Any help? Thanks.
Listings Controller
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, :except => [:show, :index]
def index
#listings = Listing.order('created_at DESC')
respond_to do |format|
format.html
format.json { render json: #listings }
end
end
def show
end
def new
#listing = Listing.new
#listing.user = current_user
end
def edit
end
def create
#listing = Listing.new(listing_params)
#listing.user = current_user
respond_to do |format|
if #listing.save
format.html { redirect_to listing_images_path(#listing), notice: 'Post was successfully created.' }
else
format.html { render action: 'new' }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #listing.update(listing_params)
flash[:notice] = 'Deal was successfully updated.'
format.html { redirect_to #listing }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
def destroy
#listing.destroy
respond_to do |format|
format.html { redirect_to listings_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
#listing = Listing.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:id, :condition, :description, :nickname, :price, :size, :title, :user_id)
end
end
Listings Form
<%= form_for(#listing, :html => { :class => 'form', :multipart => true }) do |f| %>
<% if #listing.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#listing.errors.count, "error") %> prohibited this listing from being saved:</h2>
<ul>
<% #listing.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= f.label :title %>
<%= f.text_field :title, :required => true %>
</div>
<div>
<%= f.label :price %>
<%= f.text_field :price %>
</div>
<div class="actions">
<%= f.submit 'Submit', :class => 'buyNow' %>
</div>
<% end %>
Images Controller
class ImagesController < ApplicationController
before_action :set_image, only: [:show, :edit, :update, :destroy]
before_filter :load_listing
def index
#images = #listing.images.load
#image = #listing.images.new
end
def new
end
def edit
end
def create
#image = #listing.images.new(image_params)
respond_to do |format|
if #image.save
format.html { redirect_to :back, notice: 'Image was successfully created.' }
format.json { head :no_content }
else
format.html { render action: 'new' }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #image.update(image_params)
format.html { redirect_to (#image.listing), notice: 'Image was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #image.errors, status: :unprocessable_entity }
end
end
end
def destroy
#image = #listing.images.find(params[:id])
#image.destroy
respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_image
#image = Image.find(params[:id])
end
def load_listing
#listing = Listing.find(params[:listing_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:file, :listing_id)
end
end
Images Index
<div>
<%= form_for [#listing, #image], :html => { :class => 'uploadImages', :multipart => true } do |f| %>
<%= f.hidden_field :listing_id %>
<div>
<%= f.label :file, 'Upload Images' %>
<%= f.file_field :file, multiple: true, name: 'image[file]' %>
</div>
<% end %>
</div>
<div id="progress"></div>
<% if #images.present? %>
<ul class="editGallery">
<% #listing.images.each do |image| %>
<li>
<%= image_tag image.file_url(:list) if image.file? %>
<span><%= link_to 'DELETE', listing_image_path(#listing, image.id), data: { confirm: 'Are you sure?' }, :method => :delete, :class => 'delete' %></span>
</li>
<% end %>
</ul>
<% end %>
The problem is this line:
#image = #listing.images.new
That's building a new image for #listing, so when you call #listing.images.each that new image is included in the images array. Check that the image has actually been saved to the database before constructing a delete link for it.
<% #listing.images.each do |image| %>
<% unless image.new_record? %>
<li>
<%= image_tag image.file_url(:list) if image.file? %>
<span><%= link_to 'DELETE', listing_image_path(#listing, image.id), data: { confirm: 'Are you sure?' }, :method => :delete, :class => 'delete' %></span>
</li>
<% end %>
<% end %>
Take a look at this part of your code:
<% if #images.present? %>
<ul class="editGallery">
<% #listing.images.each do |image| %>
<li>
<%= image_tag image.file_url(:list) if image.file? %>
<span><%= link_to 'DELETE', listing_image_path(#listing, image.id), data: { confirm: 'Are you sure?' }, :method => :delete, :class => 'delete' %></span>
</li>
<% end %>
</ul>
I believe your problem is a combination of this and your images controller index action.
When you hit the index action you create a new record #image = #listing.images.new
Now your #listing.images.each call registers on the object that hasn't been saved.
This my first ruby on rails application.
Model Location and Post, Location has many post.I create location as tree structure with ancestry gem.
class Post < ActiveRecord::Base
belongs_to :location, :counter_cache => true
end
class Location < ActiveRecord::Base
include Tree
has_ancestry :cache_depth => true
has_many :posts
end
This my Post Controller
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
#posts = Post.all
end
def show
end
def new
#post = Post.new
end
def edit
end
def create
#post = Post.new(post_params)
respond_to do |format|
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: #post }
else
format.html { render action: 'new' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #post.update(post_params)
format.html { redirect_to #post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #post.errors, status: :unprocessable_entity }
end
end
end
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:name, :location_id)
end
end
Creating Post with location in Post _form.html.erb
<%= simple_form_for #post do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved: </h2>
<ul>
<% #post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :name %>
<%= f.select :location_id, Location.all.at_depth(4) { |l| [ l.name, l.id ] } %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
My question are
First question- f.select :location_id , how display location name, not location id, i am using with simple form
Second question- Post index got error in <%= post.location.name %>
<% #posts.each do |post| %>
<tr>
<td><%= post.name %></td>
<td><%= post.location.name %></td>
<td><%= link_to 'Show', post %></td>
<td><%= link_to 'Edit', edit_post_path(post) %></td>
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %> </td>
</tr>
<% end %>
First question:
Check the simple_form syntax for a dropdown. It is mentioned in the docs and you should be able to get this working by yourself.
Second question:
Does the offending post really have a related location? If it does not have one, it can not display the name of course. To counter these nil errors, use try:
<%= post.location.try(:name) %>
try will call the method name on location only if location is not nil.
For your first question :
Maybe you should read about "options_for_select"
http://guides.rubyonrails.org/form_helpers.html#option-tags-from-a-collection-of-arbitrary-objects
<% cities_array = City.all.map { |city| [city.name, city.id] } %>
<%= options_for_select(cities_array) %>
For your second question:
What is your error ?
Maybe one of your "post.location" is nil.
If so, try:
post.location.name unless post.location.nil?
Hope this can help
How do I get this page to paginate using kaminari? I need help with this badly. I don't know what to do. Just get me some way so that all the posts will only be 15 per page. I want the show page to be paginated not the index page.
This is the website I built.
http://duelingpets.net/maintopics/9/subtopics/4
This is my show page for my subtopics page that is nested under maintopics
<p id="notice"><%= notice %></p>
This pages shows the current topic name of a subtopic
<h1 class="forumheader"><%= #subtopic.topicname %></h1>
<br><!---Gives space to start the narratives--->
<div class="narrativecontainer">
<table>
<tr>
<td><%= #subtopic.topicname %></td>
</tr>
<tr>
<td>by: <%= #subtopic.user.vname %></td>
</tr>
<tr>
<td><pre class="narrative_pre"><%= #subtopic.description %></pre></td>
</tr>
</table>
<br>
<% #subtopic.narratives.each do |narrative| %>
<div class="outer">
<table>
<tr>
<td>re:<%= narrative.subtopic.topicname %></td>
<% if current_user && (current_user.id == narrative.user_id || current_user.admin?)%>
<td><%= button_to 'Edit', edit_subtopic_narrative_path(#subtopic, narrative), method: :get %></td>
<td><%= button_to 'Destroy', [#subtopic, narrative], method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
<tr>
<td>by: <%= narrative.user.vname %><%#= subtopic.description %></td>
</tr>
</table>
</div>
<div class="outer">
<pre class="narrative_pre"><%= narrative.story %></pre>
</div>
<br>
<% end %>
</div>
<br>
<% if current_user %>
<p><%= link_to 'New Narrative', new_subtopic_narrative_path(#subtopic) %></p>
<br>
<% end %>
<p><%= link_to 'Back', tcontainer_maintopic_path(#maintopic.tcontainer_id, #maintopic) %></p>
This my subtopics controller.
class SubtopicsController < ApplicationController
# GET /subtopics
# GET /subtopics.json
#before_filter :load_forum
before_filter :load_topic, :only => [:edit, :update, :show, :destroy]
before_filter :load_maintopic, :only =>[:create, :index, :new]
def index
if current_user && current_user.admin?
#subtopics = #maintopic.subtopics.all
else
render "public/404"
end
end
# GET /subtopics/1
# GET /subtopics/1.json
def show
##subtopic.narratives.page(params[:page])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #subtopic }
end
end
# GET /subtopics/new
# GET /subtopics/new.json
def new
if current_user
#user = current_user.id
#subtopic = #maintopic.subtopics.build
#subtopic.user_id = #user
else
redirect_to root_url
end
end
# GET /subtopics/1/edit
def edit
end
# POST /subtopics
# POST /subtopics.json
def create
if current_user
#user = current_user.id
#subtopic = #maintopic.subtopics.new(params[:subtopic])
#subtopic.user_id = #user
if !(#subtopic.maintopic_id == #maintopic.id) #Prevents a subtopic from being assigned data to a maintopic that doesn't match
redirect_to #maintopic
return
end
#subtopic.created_on = Time.now
#subtopic.save
#if #subtopic.save
# redirect_to #maintopic.subtopic
#else
# render "new";
#end
redirect_to maintopic_subtopic_path(#maintopic, #subtopic)
else
redirect_to root_url
end
end
# PUT /subtopics/1
# PUT /subtopics/1.json
def update
respond_to do |format|
if #subtopic.update_attributes(params[:subtopic])
format.html { redirect_to maintopic_subtopic_path(#subtopic.maintopic_id, #subtopic.id), notice: 'Subtopic was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #subtopic.errors, status: :unprocessable_entity }
end
end
end
#raise
# DELETE /subtopics/1
# DELETE /subtopics/1.json
def destroy
#subtopic.destroy
respond_to do |format|
format.html { redirect_to tcontainer_maintopic_path(#maintopic.tcontainer_id, #maintopic.id) }
format.json { head :no_content }
end
end
private
def load_topic
#subtopic = Subtopic.find(params[:id])
#maintopic = Maintopic.find(#subtopic.maintopic_id)
#content = Maintopic.find(params[:maintopic_id])
if #content.id != #maintopic.id
# raise "I been tampered with and should redirect to the root page"
redirect_to root_url
end
end
def load_maintopic
#maintopic = Maintopic.find(params[:maintopic_id])
end
end
in the Subtopic model add the string:
paginates_per 15
in the controller:
def index
if current_user && current_user.admin?
#subtopics = #maintopic.subtopics.page params[:page]
else
render "public/404"
end
end
and in the index view add:
<%= paginate #subtopics %>
For more information about formatting paginator go to https://github.com/amatsuda/kaminari#helpers
try this in your controller index
`#subtopics = kaminari.paginate_array(#subtopics).page(params[:page]).per(params[:per_page])`
and in your views, edit as
<%= page_entries_info #subtopics %> and also <%= paginate #subtopics, :theme => 'twitter-bootstrap-3' %>
I am implementing an application in which i want to change the settings of the application.
While doing so I am getting an error
no routes matches {:action=>"show", :controller=>"settings", format=>"nil"}
while clicking on the open new settings tab.
my index.html is as follows:-
<h1>Listing settings</h1>
<table class="table table-striped table-bordered">
<tr>
<th>ID</th>
<th>Name</th>
<th>Value</th>
<th>Description</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<% #settings.each do |c| %>
<tr>
<td><%= c.id %> </td>
<td><%= c.name %> </td>
<td><%= c.value %> </td>
<td><%= c.description %> </td>
<td><%= link_to 'Edit', {:action => 'edit', :id => c.id} %> </td>
<td><%= link_to 'Delete', {:action => 'delete', :id => c.id},
:data => { :confirm => "Are you sure you want to delete this value?" } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Setting', {:action => 'new'} %>
My settings controller is as follows:-
class SettingsController < ApplicationController
# GET /setting
# GET /setting.json
def index
#settings = Setting.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #settings }
end
end
# GET /setting/1
# GET /setting/1.json
def show
#setting = Setting.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: #setting }
end
end
# GET /setting/new
# GET /setting/new.json
def new
#setting = Setting.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #setting }
end
end
# GET /setting/1/edit
def edit
#setting = Setting.find(params[:id])
end
# POST /setting
# POST /setting.json
def create
#setting = Setting.new(params[:setting])
respond_to do |format|
if #setting.save
format.html { redirect_to #setting, notice: 'Setting was successfully created.' }
format.json { render json: #setting, status: :created, location: #setting }
else
format.html { render action: "new" }
format.json { render json: #setting.errors, status: :unprocessable_entity }
end
end
end
# PUT /setting/1
# PUT /setting/1.json
def update
#setting = Setting.find(params[:id])
respond_to do |format|
if #setting.update_attributes(params[:setting])
format.html { redirect_to #setting, notice: 'Setting was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: #setting.errors, status: :unprocessable_entity }
end
end
end
# DELETE /setting/1
# DELETE /setting/1.json
def delete
#setting = Setting.find(params[:id])
#setting.deleted = 1
#setting.save
respond_to do |format|
format.html { redirect_to settings_url }
format.json { render :json => { :success => true } }
end
end
end
My new.html is as follows:-
<h1>New settings</h1>
<%= form_for #setting do |f| %>
<% if #setting.errors.any? %>
<div id="errorExplanation">
<h2><%= pluralize(#setting.errors.count, "error") %> prohibited this setting from being saved:</h2>
<ul>
<% #setting.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Id: <%= f.text_field :id %><br>
Name: <%= f.text_field :name %><br>
Values: <%= f.text_field :value %><br>
Description: <%= f.text_field :description %><br>
<% end %>
<%= link_to 'Back', settings_path %>
My routes.rb is as follows:-
Lms::Application.routes.draw do
resources :books do
member do
post 'add'
post 'remove'
end
collection do
get 'add'
get 'list' => "books#index"
post 'get_books'
get 'get_books'
end
end
resources :books
resources :book_transactions
resources :book_issues
resources :book_holds
resources :categories
resources :users
resources :admins
resources :library_locations
resources :lov_values
resources :loan_fines
resources :lov_names
resources :loans_fines do
member do
post 'add'
post 'remove'
end
collection do
get 'add'
get 'list'
post 'get_loans_fines'
get 'get_loans_fines'
end
end
resources :settings do
member do
post 'add'
post 'remove'
end
collection do
get 'add'
get 'list'
post 'get_settings'
get 'get_settings'
end
end
root :to => 'books#index'
match ':controller(/:action(/:id))(.:format)'
The strange part is that when i click on new action it is going/redirecting to the show action..I am a bit confused why this is happening..
Can some one help me with this..
To check your routes you can run
rake routes
to inspect if you have the routes defined correctly. Also I would suggest to use
<%= link_to "New Setting", new_setting_pah %>
For your routes definition you are not defining the routes for other methods than add or remove.
I advise you to read this great tutorial on rails routes