mardi 10 mars 2015

How can I resolve NoMethodError using Ruby on rails?

Please Can anybody help me to resolve this following error.


Error



NoMethodError in PostsController#index

protected method `wp_parse_options' called for #<Class:0x1f26fc0>
Rails.root: C:/Site/datagem

Application Trace | Framework Trace | Full Trace
app/controllers/posts_controller.rb:7:in `index'


Please check my code snippets which are described below.


views/posts/index.html.erb



<%= form_tag posts_path, :method => 'get', :id => "posts_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<div id="posts"><%= render 'posts' %></div>
<% end %>

<%= link_to 'New Post', new_post_path %>


controller/posts_controller.rb



class PostsController < ApplicationController
# GET /posts
# GET /posts.json
helper_method :sort_column, :sort_direction
def index
@posts = Post.all
@posts = Post.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end

# GET /posts/1
# GET /posts/1.json
def show
@post = Post.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end

# GET /posts/new
# GET /posts/new.json
def new
@post = Post.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end

# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end

# POST /posts
# POST /posts.json
def create
@post = Post.new(params[:post])

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# PUT /posts/1
# PUT /posts/1.json
def update
@post = Post.find(params[:id])

respond_to do |format|
if @post.update_attributes(params[:post])
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

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post = Post.find(params[:id])
@post.destroy

respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private

def sort_column
Post.column_names.include?(params[:sort]) ? params[:sort] : "name"
end

def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end


helpers/application_helpers.rb



module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end
end


Please check the codes and try to resolve this soon.


Aucun commentaire:

Enregistrer un commentaire